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 ...
随机推荐
- NLP Deep Learning
http://blog.csdn.net/itplus http://blog.csdn.net/zouxy09/article/details/8775360
- 百度Site App的uaredirect.js实现手机访问,自动跳转网站手机版
以下为代码,可放置在网站foot底部文件,或者haead顶部文件,建议将代码放在网站顶部,这样可以实现手机访问立即跳转! <script src="http://siteapp.bai ...
- request获取请求头和请求数据
package cn.itcast.request; import java.io.IOException; import java.io.InputStream; import java.io.Pr ...
- 【iCore3 双核心板】DEMO 1.0 测试程序发布
iCore3 Demo V1.0 程序说明 一.概要 本资料包包含5个文件夹: 1.“arm”里是 icore3上 arm的程序包,开发环境为 KEIL 5.17: 2.“fpga”里是 icore3 ...
- Codeigniter 3.0 相关文档 part one
分页配置项 http://stackoverflow.com/questions/18418900/codeigniter-pagination-config-without-repeating-wi ...
- 一步一步来做WebQQ机器人-(二)(第一次登陆)
// 预计会有这些步骤,当然某些步骤可能会合并: 验证码 第一次登陆 第二次登陆 保持在线和接收消息 获取好友和群列表 发送消息 变成智能的(*゚∀゚*) webqq的登陆,分为2步,本文主要讲第一次 ...
- Android WebView常见问题及解决方案汇总
Android WebView常见问题解决方案汇总: 就目前而言,如何应对版本的频繁更新呢,又如何灵活多变地展示我们的界面呢,这又涉及到了web app与native app之间孰优孰劣的争论. 于是 ...
- Java学习-048-插件应用之 Find Bugs
FindBugs 是一个静态分析工具,它可以检查类或者 JAR 文件,将字节码与一组缺陷模式进行对比以发现可能的问题,使用 FindBugs 可以在不实际运行程序的情况对软件进行分析.使用时最好将字节 ...
- java中nextLine()和next()的区别
>概述 在实现字符窗口的输入时,我个人更喜欢选择使用扫描器Scanner,它操作起来比较简单.我发现用Scanner实现字符串的输入有两种方法,一种是next(),一种nextLine(),但是 ...
- 【转】PHP 位运算应用口诀
位运算应用口诀 清零取位要用与,某位置一可用或 若要取反和交换,轻轻松松用异或 移位运算 要点 1 它们都是双目运算符,两个运算分量都是整形,结果也是整形. 2 "<<" ...