使用自动布局约束时,UIView 无法通过 UIpangesture 顺利拖动

Posted

技术标签:

【中文标题】使用自动布局约束时,UIView 无法通过 UIpangesture 顺利拖动【英文标题】:UIView not dragging smoothly by UIpangesture when use autolayout constraint 【发布时间】:2016-05-05 10:44:29 【问题描述】:

**我想使用 UIpangesture 垂直向上拖动视图。由于我的项目包括自动布局,我创建了 4 个布局约束 [上、右、下、左] 并从顶部空间约束创建了一个出口。当手势识别时,topspaceConstraint 的常量是 chnage 来改变视图原点 Y。代码有效,但拖动不顺畅。我怎样才能使它顺利**

-(void)gestureAction:(UIPanGestureRecognizer *)gesture

    CGFloat targetY = 0;
    if(gesture.state == UIGestureRecognizerStateBegan)
    
        self.panCoord = [gesture locationInView:gesture.view];

    
    CGPoint newCoord = [gesture locationInView:gesture.view];
    float dY = newCoord.y-self.panCoord.y;
    float newOriginY = (gesture.view.frame.origin.y+dY);

    dispatch_async(dispatch_get_main_queue(), ^(void)
                self.propertyDetailContentViewTopConstraint.constant = newOriginY;
            );
    if (gesture.state == UIGestureRecognizerStateEnded) 

        if (gesture.view.frame.origin.y*0.8 < 100) 
            targetY = 0;
        else if (gesture.view.frame.origin.y*0.8 < 250)
            targetY = 250;
        else
            targetY = MAIN_SCREEN_HEIGHT;
        

        [self setPropertyDetailContentViewTopConstraintTop:targetY];
    



-(void)setPropertyDetailContentViewTopConstraintTop:(CGFloat)top

    [self.view layoutIfNeeded];

    self.propertyDetailContentViewTopConstraint.constant = top;
    [UIView animateWithDuration:0.5
                     animations:^
                         [self.view layoutIfNeeded];
                     ];

【问题讨论】:

【参考方案1】:

@Rokon 请使用以下代码可能对您有所帮助。请使用您的视图而不是“DrawImageView”

 -(void)moveViewWithGestureRecognizer:(UIPanGestureRecognizer *)panGestureRecognizer


    NSUInteger touches = panGestureRecognizer.numberOfTouches;

    CGPoint translation = [panGestureRecognizer translationInView:self.view];


    self.DrawImageView.center = CGPointMake(self.DrawImageView.center.x + translation.x,
                                         self.DrawImageView.center.y + translation.y);
   [panGestureRecognizer setTranslation:CGPointMake(0, 0) inView:self.view];

   if (panGestureRecognizer.state == UIGestureRecognizerStateEnded) 

    CGPoint velocity = [panGestureRecognizer velocityInView:self.view];
    CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
    CGFloat slideMult = magnitude / 200;
    //  NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult);

    float slideFactor = 0.1 * slideMult; // Increase for more of a slide
    CGPoint finalPoint = CGPointMake(self.DrawImageView.center.x + (velocity.x * slideFactor),
                                     self.DrawImageView.center.y + (velocity.y * slideFactor));
    finalPoint.x = MIN(MAX(finalPoint.x, 0), self.view.bounds.size.width);
    finalPoint.y = MIN(MAX(finalPoint.y, 0), self.view.bounds.size.height);

    [UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^
        self.DrawImageView.center = finalPoint;
     completion:nil];
    // [self.frontImageView setAlpha:1.0];
  

【讨论】:

以上是关于使用自动布局约束时,UIView 无法通过 UIpangesture 顺利拖动的主要内容,如果未能解决你的问题,请参考以下文章

空 UIView 上的自动布局约束无法按预期工作

自动布局中的比例距离约束

向添加到 UIWindow 的视图添加约束时,为啥无法设置自动布局约束?

程序化自动布局、约束和 UIView 运行时

在 Swift 运行时更改自动布局约束的 UIView 的框架

如何使用自动布局在 Swift 中为可重用的 UIView 设置约束?