拖动手势使用(UIPanGestureRecognizer)

拖动手势创建

    UIPanGestureRecognizer * panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panMoveGesture:)];
    [topPaperView addGestureRecognizer:panGesture];

拖动事件(以下示例为上下拖动,上下左右拖动及滑行见注释部分)

//拖动手势
-(void)panMoveGesture:(UIPanGestureRecognizer *)recognizer
{
    if (_isResponseGesture == NO) {
        return;
    }
    //视图前置操作
    [recognizer.view.superview bringSubviewToFront:recognizer.view];
    CGPoint center = recognizer.view.center;
    CGFloat cornerRadius = recognizer.view.frame.size.width/2;
    CGPoint translation = [recognizer translationInView:self.view];
//    recognizer.view.center = CGPointMake(center.x+translation.x, center.y+translation.y);(上下左右滑动)-------
//限制上下滑动的最大和最小距离(限制只能上下滑动)
    if (center.y+translation.y-20>Main_Screen_Height/3*2)
    {
        recognizer.view.center = CGPointMake(center.x, Main_Screen_Height/3*2+20);

    }
    else if (center.y+translation.y-20

参考文章:http://www.cnblogs.com/bolin-123/p/5393370.html

你可能感兴趣的:(拖动手势使用(UIPanGestureRecognizer))