源码

首页 » 归档 » 源码 » iOS两种要领限制拖拽手势的范围-ios学习从入门到精通尽在姬长信

iOS两种要领限制拖拽手势的范围-ios学习从入门到精通尽在姬长信

分享最热门的ios资讯

UIGestureRecognizer是一个界说基本手势的抽象类,具体包罗:
  1、拍击UITapGestureRecognizer (任意次数的拍击)
  2、向里或向外捏UIPinchGestureRecognizer (用于缩放)
  3、摇动或者拖拽UIPanGestureRecognizer (拖动)
  4、擦碰UISwipeGestureRecognizer (以任意偏向)
  5、旋转UIRotationGestureRecognizer (手指朝相反偏向移动)
  6、长按UILongPressGestureRecognizer (长按)
本文主要使用到的是拖拽手势:UIPanGestureRecognizer

限制要领:

-(void)doMoveAction:(UIPanGestureRecognizer *)recognizer{
    // Figure out where the user is trying to drag the view. 
    CGPoint translation = [recognizer translationInView:self.view];
    CGPoint newCenter = CGPointMake(recognizer.view.center.x+ translation.x,
                                    recognizer.view.center.y + translation.y);//    限制屏幕范围:
    newCenter.y = MAX(recognizer.view.frame.size.height/2, newCenter.y);
    newCenter.y = MIN(self.view.frame.size.height - recognizer.view.frame.size.height/2,  newCenter.y);
    newCenter.x = MAX(recognizer.view.frame.size.width/2, newCenter.x);
    newCenter.x = MIN(self.view.frame.size.width - recognizer.view.frame.size.width/2,newCenter.x);
    recognizer.view.center = newCenter;
    [recognizer setTranslation:CGPointZero inView:self.view];
}

也可以使用UITouch来实现:
当手指接触到屏幕,不管是单点触摸还是多点触摸,事件都会开始,直到用户所有的手指都离开屏幕。期间所有的UITouch工具都被包罗在UIEvent事件工具中,由法式分发给处理者。事件记录了这个周期中所有触摸工具状态的变化。
限制要领如下:

BOOL isMove;
CGPoint legend_point;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    isMove = NO;
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    if (CGRectContainsPoint(V2.frame, point)) {
        legend_point = [touch locationInView:V2];
        isMove = YES;
    }
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesMoved:touches withEvent:event];
    if (!isMove) {
        return;
    }
    @autoreleasepool {
        UITouch *touch = [touches anyObject];
        CGPoint point = [touch locationInView:self.view];
        //转化成相对的中心
        point.x += V2.frame.size.width/2.0f - legend_point.x;
        point.y += V2.frame.size.height/2.0f - legend_point.y;
//        限制范围
        if (point.x < V2.frame.size.width / 2.0f) {
            point.x = V2.frame.size.width / 2.0f;
        }
        if (point.y < V2.frame.size.height / 2.0f) {
            point.y = V2.frame.size.height / 2.0f;
        }

        if (point.x > self.view.frame.size.width - V2.frame.size.width / 2.0f) {
            point.x = self.view.frame.size.width - V2.frame.size.width / 2.0f;
        }
        if (point.y > self.view.frame.size.height - V2.frame.size.height / 2.0f) {
            point.y = self.view.frame.size.height - V2.frame.size.height / 2.0f;
        }
        V2.center = point;

    }
}

效果如图:

1314486-f9cb2fdb7b35d2b4.gif

用意志战胜身体的惰性!

(0)

本文由 姬長信 创作,文章地址:https://blog.isoyu.com/archives/1454.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:9 月 30, 2016 at 11:59 上午

关键词:

热评文章

发表回复

[必填]

我是人?

提交后请等待三秒以免造成未提交成功和重复