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 ...
随机推荐
- jQuery判断元素是否存在方法总结
在jquery中判断元素是否存在我们可使用$("#div").length > 0)来判断了,意思就是判断元素长度了,如果没有肯定是不存在的哦,下面我来介绍介绍. 使用传统j ...
- 关闭SSMS的事务自动提交,改为手动提交
SQLServer 2005-2008-2012使用Oracle时,默认是手动提交.而SQLServer2005中,默认是自动提交,但是SQLServer支持配置. 方法: 用SSMS连接到SQL S ...
- Windows内核 基本汇编指令
1)用VS2010新建Win32 Console Application,工程名为ACECore,工程建立完成后得到打开文件ACECore.cpp,代码如下: #include "stdaf ...
- 使用 JavaScript 实现链表
代码: <!doctype html> <html lang="en"> <head> <meta charset="UTF-8 ...
- Objective-C 在Categroy中创建属性(Property)
Objective-c中category是不能直接创建属性的,这时候我们要用到Objc的runtime来实现 用到的方法有两个 一个是get方法 一个set方法 //get方法objc_getAsso ...
- Tomcat热部署方法(3种)【转】
热部署是指在你修改项目BUG的时候对JSP或JAVA类进行了修改在不重启WEB服务器前提下能让修改生效.但是对配置文件的修改除外! 1.直接把项目web文件夹放在webapps里. 2.在tomcat ...
- 浅谈Entity Framework 增删改查和事务操作
1.增加对象 DbEntity db = new DbEntity(); //创建对象实体,注意,这里需要对所有属性进行赋值(除了自动增长主键外),如果不赋值,则会数据库中会被设置为NULL(注意是否 ...
- iis 部署 webapi2.0 访问报错解决
本机安装的VS2013 开发环境,在IIS部署WebApi2.0时,应用程序池并没有.NET4.5的选项. 网上搜索一番得知: 1..NET 4.5本质上还是4.0,属于递增式的更新,所以对IIS 来 ...
- centOS安装nginx
下载源码 wget http://nginx.org/download/nginx-1.10.1.tar.gz git clone https://bitbucket.org/nginx-goodie ...
- iOS10 app连接不上网络的问题
http://jingyan.baidu.com/article/29697b917f2069ab20de3c33.html