简体   繁体   English

使用摇动手势重置UIView的框架而无需对开始值进行硬编码

[英]Reset UIView's frame with a pan gesture without hardcoding start values

I have a UIPanGestureRecognizer that is attached to a UIView. 我有一个附加到UIView的UIPanGestureRecognizer。 What I am trying to do is essentially drag and drop into a bucket. 我要做的基本上是拖放到存储桶中。 If the user lets go of the UIView and it's not in the bucket, I want it to animate back to its original start location. 如果用户放开了UIView并且它不在存储桶中,则我希望它动画返回其原始开始位置。 My selector that gets called for my UIPanGestureRecognizer is: 我的UIPanGestureRecognizer被调用的选择器是:

- (void)handleDragDescriptionView:(UIPanGestureRecognizer *)gestureRecognizer {
    UIView *panViewPiece = [gestureRecognizer view];
    CGRect originalFrame = CGRectMake(946, 20, 58, 30);

    CGPoint translation = [gestureRecognizer translationInView:panViewPiece];
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan || gestureRecognizer.state == UIGestureRecognizerStateChanged) {
        panViewPiece.center = CGPointMake(panViewPiece.center.x + translation.x, panViewPiece.center.y + translation.y);

        [gestureRecognizer setTranslation:CGPointZero inView:panViewPiece.superview];
    }
    else if (gestureRecognizer.state == UIGestureRecognizerStateCancelled) {
        [UIView animateWithDuration:0.25 animations:^{
            self.dragDescriptionView.frame = originalFrame;
        }];
    }
    else {
        [UIView animateWithDuration:0.25 animations:^{
            self.dragDescriptionView.frame = originalFrame;
        }];
    }

}

I was wondering if I could do this without the originalFrame at the top of the method. 我想知道如果没有方法顶部的originalFrame我是否可以做到这一点。 I originally had 我原来有

CGRect originalFrame = _dragDescriptionView.frame; 

instead of the hardcoding, but it doesn't snap back. 而不是硬编码,但不会返回。 Probably because I'm updating that value as I am dragging. 可能是因为我在拖动时正在更新该值。 I don't particularly like hardcoding values, but I wasn't sure if there was a way around this. 我不特别喜欢对值进行硬编码,但是我不确定是否有解决方法。 Thanks! 谢谢!

You really only need to track the view's original center. 您实际上只需要跟踪视图的原始中心。 Make originalCenter an instance variable and only set it when the gesture begins: originalCenter为实例变量,并仅在手势开始时进行设置:

@implementation MyViewController {
    CGPoint _originalCenter;
}

- (void)handleDragDescriptionView:(UIPanGestureRecognizer *)gestureRecognizer {
    UIView *panViewPiece = [gestureRecognizer view];

    switch (gestureRecognizer.state) {
        case UIGestureRecognizerStateBegan:
            _originalCenter = panViewPiece.center;
            break;

        case UIGestureRecognizerStateChanged: {
            CGPoint translation = [gestureRecognizer translationInView:panViewPiece.superview];
            panViewPiece.center = CGPointMake(panViewPiece.center.x + translation.x, panViewPiece.center.y + translation.y);
            [gestureRecognizer setTranslation:CGPointZero inView:panViewPiece.superview];
            break;
        }

        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateCancelled: {
            [UIView animateWithDuration:0.25 animations:^{
                panViewPiece.center = _originalCenter;
            }];
            break;
        }

        default:
            break;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM