美文网首页
拖动手势使用(UIPanGestureRecognizer)

拖动手势使用(UIPanGestureRecognizer)

作者: 绛紫哟 | 来源:发表于2017-06-08 17:09 被阅读372次

拖动手势创建

    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<Main_Screen_Height/3)
    {
        recognizer.view.center = CGPointMake(center.x, Main_Screen_Height/3);

    }
    else
    {
        recognizer.view.center = CGPointMake(center.x, center.y+translation.y);

    }
    
    [recognizer setTranslation:CGPointZero inView:recognizer.view.superview];
//下面注释内容为滑行设置
//    if (recognizer.state == UIGestureRecognizerStateEnded) {
//        //计算速度向量的长度,当他小于200时,滑行会很短
//        CGPoint velocity = [recognizer velocityInView:recognizer.view.superview];
//        CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
//        CGFloat slideMult = magnitude / 200;
////          NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult); //e.g. 397.973175, slideMult: 1.989866
//
//        //基于速度和速度因素计算一个终点
//        float slideFactor = 0.1 * slideMult;
//        CGPoint finalPoint = CGPointMake(center.x + (velocity.x * slideFactor), center.y + (velocity.y * slideFactor));
//        //限制最小[cornerRadius] 和最大边界值 [self.view.bounds.size,width - cornerRadius],以免拖动出屏幕界限
//        finalPoint.x = MIN(MAX(finalPoint.x, cornerRadius), Main_Screen_Width - cornerRadius);
//        finalPoint.y = MIN(MAX(finalPoint.y, cornerRadius), Main_Screen_Height - cornerRadius);
//        
//        //使用UIView动画 使view滑行到终点
//        [UIView animateWithDuration:slideFactor * 2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
//            recognizer.view.center = finalPoint;
//        } completion:nil];
//        
//    }
    
//根据view拖动位置,修改其他view的相关信息    
_pullTopY = recognizer.view.center.y-20;
    paperCV.frame = CGRectMake(0, 64, Main_Screen_Width, Main_Screen_Height-114);
    layout.sectionInset = UIEdgeInsetsMake(_pullTopY-64+40, 0, 0, 0);
    layout.itemSize = CGSizeMake(Main_Screen_Width, Main_Screen_Height-50-_pullTopY-40);
//    [self setPullTopY:recognizer.view.center.y-20];

}

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

相关文章

网友评论

      本文标题:拖动手势使用(UIPanGestureRecognizer)

      本文链接:https://www.haomeiwen.com/subject/psjtqxtx.html