最近遇到一个问题,用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+拖动的更多相关文章

  1. iOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry)

    iOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry) 随着iPhone6/6+设备的上市,如何让手头上的APP适配多种机型多种屏幕尺寸变得尤为迫 ...

  2. IOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry) 转载

    http://blog.csdn.net/he_jiabin/article/details/48677911 随着iPhone6/6+设备的上市,如何让手头上的APP适配多种机型多种屏幕尺寸变得尤为 ...

  3. 代码方式使用AutoLayout (NSLayoutConstraint + Masonry)

    随着iPhone6/6+设备的上市,如何让手头上的APP适配多种机型多种屏幕尺寸变得尤为迫切和必要.(包括:iPhone4/4s,iPhone5/5s,iPhone6/6s,iPhone 6p/6ps ...

  4. iOS开发之Masonry框架源码深度解析

    Masonry是iOS在控件布局中经常使用的一个轻量级框架,Masonry让NSLayoutConstraint使用起来更为简洁.Masonry简化了NSLayoutConstraint的使用方式,让 ...

  5. CSharpGL(39)GLSL光照示例:鼠标拖动太阳(光源)观察平行光的漫反射和镜面反射效果

    CSharpGL(39)GLSL光照示例:鼠标拖动太阳(光源)观察平行光的漫反射和镜面反射效果 开始 一图抵千言.首先来看鼠标拖动太阳(光源)的情形. 然后是鼠标拖拽旋转模型的情形. 然后我们移动摄像 ...

  6. Android开发学习之路-RecyclerView滑动删除和拖动排序

    Android开发学习之路-RecyclerView使用初探 Android开发学习之路-RecyclerView的Item自定义动画及DefaultItemAnimator源码分析 Android开 ...

  7. 【原】Masonry+UIScrollView的使用注意事项

    [原]Masonry+UIScrollView的使用注意事项 本文转载请注明出处 —— polobymulberry-博客园 1.问题描述 我想实现的使用在一个UIScrollView依次添加三个UI ...

  8. 【Android】 修复ijkPlayer进行m3u8 hls流播放时seek进度条拖动不准确的问题

    项目中使用的播放器是ijkPlayer,发现播放切片特点的hls流(m3u8格式的视频)拖动seekBar的时候会莫名的跳转或者seek不到准确的位置,发现网友也遇到了同样的问题,ijk的开发者也说明 ...

  9. 自己封装的一个原生JS拖动方法。

    代码: function drag(t,p){ var point = p || null, target = t || null, resultX = 0, resultY = 0; (!point ...

随机推荐

  1. 使用context来传递数据,一个context是一系列变量

    页面设计工作和python代码分离,所以我们引用模板来实现这个功能. 一.模板实例 下面是一个模板的实例: [python]<html><head><title>O ...

  2. Leetcode | Linked List Cycle I && II

    一.判断链表是否存在环,办法为: 设置两个指针(fast, slow),初始值都指向头,slow每次前进一步,fast每次前进二步,如果链表存在环,则fast必定先进入环,而slow后进入环,两个指针 ...

  3. 【iCore3 双核心板】例程十:RTC实时时钟实验——显示日期和时间

    实验指导书及代码包下载: http://pan.baidu.com/s/1jHuZcnc iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  4. NOJ 1074 Hey Judge(DFS回溯)

    Problem 1074: Hey Judge Time Limits:  1000 MS   Memory Limits:  65536 KB 64-bit interger IO format: ...

  5. cordova-sqlite-plugin常用数据库操作

    ionic项目需要做本地缓存,研究了一番,总结出了常用的数据库操作: 1.新建数据库,用angularJS+ionic: //声明一个数据库对象 var db = null; //事件devicere ...

  6. 【转】oracle回闪操作

    在9i上执行的操作 查询test表中记录select * from test;删除test表中记录delete from test;获得过去的会话exec dbms_flashback.disable ...

  7. C#网络编程之--TCP协议(一)

    TCP 是面向连接的传输协议 面向连接,其实就好比,A打电话给B,如果B接听了,那么A和B之间就的通话,就是面向连接的 TCP 是全双工的传输协议 全双工,这个理解起来也很简单,A打电话给B,B接听电 ...

  8. centos同步北京时间

    yum install ntp ntpdate #ntpdate -u 202.120.2.101 //写入硬件 #hwclock -w 以下是国内常见的NTP服务器 ntp.sjtu.edu.cn ...

  9. JQuery-属性

    // attr能访问到的都是html里面的样式,诸如内联样式.外部样式和外联样式该方法访问不到 $('#div1').width('400px') // 这个用来改样式css $("#div ...

  10. Spark1.6.2 java实现读取txt文件插入MySql数据库代码

    package com.gosun.spark1; import java.util.ArrayList;import java.util.List;import java.util.Properti ...