Masonry+拖动
最近遇到一个问题,用Masonry写的布局;
拖动其中某个view,拖动方法按传统的写成如下形式。如果view中的label更改text值,拖动之后的view就会回到最初被设定的位置。
- (void)objectDidDragged:(UIPanGestureRecognizer *)paramSender { if (paramSender.state != UIGestureRecognizerStateEnded && paramSender.state != UIGestureRecognizerStateFailed){
//通过使用 locationInView 这个方法,来获取到手势的坐标
CGPoint location = [paramSender locationInView:paramSender.view.superview];
paramSender.view.center = location;
}
}
经试验后,拖动方法需改为如下所示:
//
// ViewController.m
// PanGesTest
//
// Created by Vivien on 16/9/18.
// Copyright © 2016年 Vivien. All rights reserved.
//
#import "Masonry.h"
#import "ViewController.h" @interface ViewController ()
{ NSTimer *timer ;
int count; CGPoint panPoint;
}
@property (strong, nonatomic) UIView *panView;
@property (strong, nonatomic) UILabel *countLabel;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. _panView = [[UIView alloc]init];
_panView.backgroundColor = [UIColor grayColor];
[self.view addSubview:_panView]; UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(objectDidDragged:)];
//限定操作的触点数
[panGR setMaximumNumberOfTouches:];
[panGR setMinimumNumberOfTouches:];
//将手势添加到draggableObj里
[_panView addGestureRecognizer:panGR]; [_panView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(self.view).offset();
make.left.mas_equalTo(self.view).offset();
make.width.mas_equalTo();
make.height.mas_equalTo();
}]; _countLabel = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];
[_countLabel setText:@""];
[_countLabel setTextColor:[UIColor redColor]];
[_countLabel setTextAlignment:NSTextAlignmentCenter];
[_countLabel setFont:[UIFont systemFontOfSize:]];
[_panView addSubview:_countLabel]; [_countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.mas_equalTo(_panView);
make.height.mas_equalTo();
}]; timer = [NSTimer scheduledTimerWithTimeInterval: target:self selector:@selector(countAdd) userInfo:nil repeats:YES];
[timer fire]; count = ;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (void)objectDidDragged:(UIPanGestureRecognizer *)sender { if (sender.state == UIGestureRecognizerStateBegan) {
panPoint = [sender locationInView:_panView]; NSLog(@"panPoint:%@",NSStringFromCGPoint(panPoint));
}
if (sender.state != UIGestureRecognizerStateEnded && sender.state != UIGestureRecognizerStateFailed)
{
CGPoint inViewLoction = [sender locationInView:self.view];//sender.view.superview
CGPoint location = [sender translationInView:sender.view.superview]; NSLog(@"locationInView:%@,translationInView:%@",NSStringFromCGPoint(inViewLoction),NSStringFromCGPoint(location));
// sender.view.center = inViewLoction;
[_panView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo();
make.height.mas_equalTo();
make.left.mas_equalTo(inViewLoction.x-panPoint.x);
make.top.mas_equalTo(inViewLoction.y-panPoint.y);
// make.left.mas_equalTo(0).offset(inViewLoction.x-panPoint.x);
// make.top.mas_equalTo(0).offset(inViewLoction.y-panPoint.y);
}]; NSLog(@"className:%@",NSStringFromClass([sender.view.superview class]));
[sender setTranslation:CGPointZero inView:self.view];
}
} - (void)countAdd
{
count ++;
[_countLabel setText:[NSString stringWithFormat:@"%d",count]];
} @end
Masonry+拖动的更多相关文章
- iOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry)
iOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry) 随着iPhone6/6+设备的上市,如何让手头上的APP适配多种机型多种屏幕尺寸变得尤为迫 ...
- IOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry) 转载
http://blog.csdn.net/he_jiabin/article/details/48677911 随着iPhone6/6+设备的上市,如何让手头上的APP适配多种机型多种屏幕尺寸变得尤为 ...
- 代码方式使用AutoLayout (NSLayoutConstraint + Masonry)
随着iPhone6/6+设备的上市,如何让手头上的APP适配多种机型多种屏幕尺寸变得尤为迫切和必要.(包括:iPhone4/4s,iPhone5/5s,iPhone6/6s,iPhone 6p/6ps ...
- iOS开发之Masonry框架源码深度解析
Masonry是iOS在控件布局中经常使用的一个轻量级框架,Masonry让NSLayoutConstraint使用起来更为简洁.Masonry简化了NSLayoutConstraint的使用方式,让 ...
- CSharpGL(39)GLSL光照示例:鼠标拖动太阳(光源)观察平行光的漫反射和镜面反射效果
CSharpGL(39)GLSL光照示例:鼠标拖动太阳(光源)观察平行光的漫反射和镜面反射效果 开始 一图抵千言.首先来看鼠标拖动太阳(光源)的情形. 然后是鼠标拖拽旋转模型的情形. 然后我们移动摄像 ...
- Android开发学习之路-RecyclerView滑动删除和拖动排序
Android开发学习之路-RecyclerView使用初探 Android开发学习之路-RecyclerView的Item自定义动画及DefaultItemAnimator源码分析 Android开 ...
- 【原】Masonry+UIScrollView的使用注意事项
[原]Masonry+UIScrollView的使用注意事项 本文转载请注明出处 —— polobymulberry-博客园 1.问题描述 我想实现的使用在一个UIScrollView依次添加三个UI ...
- 【Android】 修复ijkPlayer进行m3u8 hls流播放时seek进度条拖动不准确的问题
项目中使用的播放器是ijkPlayer,发现播放切片特点的hls流(m3u8格式的视频)拖动seekBar的时候会莫名的跳转或者seek不到准确的位置,发现网友也遇到了同样的问题,ijk的开发者也说明 ...
- 自己封装的一个原生JS拖动方法。
代码: function drag(t,p){ var point = p || null, target = t || null, resultX = 0, resultY = 0; (!point ...
随机推荐
- bug1
1从相册中获取图片,低版本可以,高版本不行.看见抛出 Bitmap too large to be uploaded into a texture 原来是高版本的android,机子好点,相机就好点, ...
- C#抽象类、抽象方法、虚方法
定义抽象类和抽象方法: abstract 抽象类特点: 1.不能初始化的类被叫做抽象类,它们只提供部分实现,但是另一个类可以继承它并且能创建它们的实例 2.一个抽象类可以包含抽象和非抽象方法,当一个类 ...
- 【iCore3 双核心板】例程三十:U_DISK_IAP_FPGA实验——更新升级FPGA
实验指导书及代码包下载: http://pan.baidu.com/s/1jH1TiKY iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- ssh增加密匙登录
使用要创建登录密匙的账号登录 生成密匙 #ssh-keygen -t rsa 生成时提示输入密码,如果不输入,则直接回车即可,如果输入,将在无密匙登陆时要求输入该密码 进入生成目录.ssh #cd ~ ...
- TP框架知识点
- Mac OS 电脑播放 iPhone音乐
http://apple.stackexchange.com/questions/6173/can-i-play-audio-from-my-iphone-on-my-mac Simple, and ...
- ECSHOP 用户中心 我的订单前台显视订单每张商品图片及收货人
先在网站根目录打开文件 includes/lib_transaction.php 查找代码 $sql = "SELECT order_id, order_sn, order_status, ...
- Python学习【第一篇】Python简介
Python简介 Python前世今生 Python是著名的“龟叔”Guido van Rossum在1989年圣诞节期间,为了打发无聊的圣诞节而编写的一个编程语言. 现在,全世界差不多有600多种编 ...
- 无法识别的属性“targetFramework”的解决方法
本文导读:网站发布后,在IIS中浏览的时候出现以下异常:无法识别的属性“targetFramework”,请注意属性名称区分大小写.出现这个问题是由IIS配置该站点的.NET Framework 版本 ...
- Python之路-python(rabbitmq、redis)
一.RabbitMQ队列 安装python rabbitMQ module pip install pika or easy_install pika or 源码 https://pypi.pytho ...