首页 > 其他 > 详细

UI_拖动View

时间:2015-07-03 14:03:24      阅读:152      评论:0      收藏:0      [点我收藏+]

方法一

在touchesMoved中

     // 获取到触摸的手指
     UITouch *touch = [touches anyObject]; // 获取集合中对象
     // 获取开始时的触摸点
     CGPoint previousPoint = [touch previousLocationInView:self];
     // 获取当前的触摸点
     CGPoint latePoint = [touch locationInView:self];
     // 获取当前点的位移量
     CGFloat dx = latePoint.x - previousPoint.x;
     CGFloat dy = latePoint.y - previousPoint.y;
     // 获取当前视图的center
     CGPoint center = self.center;
     // 根据位移量修改center的值
     center.x += dx;
     center.y += dy;
     // 把新的center赋给当前视图
     self.center = center;

方法二

#pragma mark - 重写方法
#pragma mark 触摸开始
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    self.startPoint = [touch locationInView:self];

    NSLog(@"%s",__FUNCTION__);
}

#pragma mark - 触摸移动
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%s",__FUNCTION__);

    // 实现touchview随手势移动

    UITouch *touch = [touches anyObject];
    self.endPoint = [touch locationInView:self];
    CGFloat x = _endPoint.x - _startPoint.x;
    CGFloat y = _endPoint.y - _startPoint.y;

    CGPoint center = self.center;

    center.x += x;
    center.y += y;
    self.center = center;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

UI_拖动View

原文:http://blog.csdn.net/yadong_zhao/article/details/46740311

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!