当使用autolayout约束时,UIView不能通过UIpangesture平滑拖动

时间:2016-05-05 10:44:30

标签: objective-c uiview autolayout uipangesturerecognizer ios-autolayout

**我想使用UIpangesture垂直拖动视图。由于我的项目包括自动布局,我创建了4个布局约束[top,right,bottom,left]并根据topspace约束创建了一个出口。当手势识别时,topspaceConstraint的常量是更改视图orgin Y的chnage。代码有效,但拖动不顺畅。我怎样才能顺利**

-(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 个答案:

答案 0 :(得分: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];
  }
}