美文网首页
手势平滑拖动控件

手势平滑拖动控件

作者: Demonboy | 来源:发表于2017-07-04 11:52 被阅读28次
    - (void) doHandlePanAction:(UIPanGestureRecognizer *)paramSender {
    CGPoint point = [paramSender translationInView:self.view];
    
    CGPoint newCenter = CGPointMake(paramSender.view.center.x + point.x, paramSender.view.center.y + point.y);//限制屏幕范围:
    
    newCenter.y = MAX(paramSender.view.height/2, newCenter.y);
    
    newCenter.y = MIN(self.view.height - paramSender.view.heigh /2 - 49, newCenter.y);
    
    newCenter.x = MAX(paramSender.view.width/2, newCenter.x);
    
    newCenter.x = MIN(self.view.width - paramSender.view.width/2, newCenter.x);
    
    if(paramSender.state == UIGestureRecognizerStateChanged) {
    
    paramSender.view.center = newCenter;
    
    [paramSender setTranslation:CGPointMake(0, 0) inView:self.view];
    
    }
    
    if (paramSender.state == UIGestureRecognizerStateEnded) {
    
    CGPoint tempPoint = newCenter;
    
    if (newCenter.x < screenW/2.0) {
    
    newCenter.x = paramSender.view.width/2;
    
    } else {
    
    newCenter.x = self.view.width- paramSender.view.width/2;
    
    }
    
    float seconds = ((fabs(tempPoint.x - newCenter.x))/screenW/2.0);
    
    [UIView animateWithDuration:seconds animations:^{
    
    paramSender.view.center = newCenter;
    
    }];
    }
    }
    

    相关文章

      网友评论

          本文标题:手势平滑拖动控件

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