图像闲置一段时间后,UIPanGestureRecognizer 在移动之前卡住

发布于 2024-11-28 03:51:03 字数 2884 浏览 1 评论 0原文

因此,我正在制作一个简单的 iPhone 应用程序,可让您移动/缩放已导入视图中的图像。我使用 UIGestureRecognizers 和下面的 @paulsolt 代码来完成此任务。效果很好。我遇到的唯一问题是,当我在一段时间内没有触摸屏幕或执行任何其他操作后去移动对象时,在它开始平稳移动之前会出现轻微的卡顿。

有谁知道如何解决这个问题?

    - (void)addGestureRecognizersToView:(UIView *)theView {
            theView.userInteractionEnabled = YES;  // Enable user interaction

            UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
            [panGesture setMaximumNumberOfTouches:2];
            [panGesture setDelegate:self];
            [theView addGestureRecognizer:panGesture];
            [panGesture release];


            UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];
            [theView addGestureRecognizer:pinchGesture];
            [pinchGesture release];
        }


    - (void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer {

            if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
                // Reset the last scale, necessary if there are multiple objects with different scales
                lastScale = [gestureRecognizer scale];
            }

            if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || 
                [gestureRecognizer state] == UIGestureRecognizerStateChanged) {

                CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:@"transform.scale"] floatValue];

                // Constants to adjust the max/min values of zoom
                const CGFloat kMaxScale = 1.5;
                const CGFloat kMinScale = 1.0;
                const CGFloat kSpeed = 0.75;

                CGFloat newScale = 1 -  (lastScale - [gestureRecognizer scale]) * (kSpeed); 
                newScale = MIN(newScale, kMaxScale / currentScale);   
                newScale = MAX(newScale, kMinScale / currentScale);
                CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
                [gestureRecognizer view].transform = transform;

                lastScale = [gestureRecognizer scale];  // Store the previous scale factor for the next pinch gesture call  
            }
        }

    - (void)handlePanGesture:(UIPanGestureRecognizer *)gestureRecognizer {

        UIView *myView = [gestureRecognizer view];

        CGPoint translate = [gestureRecognizer translationInView:[myView superview]];


        if ([gestureRecognizer state] == UIGestureRecognizerStateChanged || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
            [myView setCenter:CGPointMake(myView.center.x + translate.x, myView.center.y + translate.y)];
        [gestureRecognizer setTranslation:CGPointZero inView:[myView superview]];
        }
    }

So I'm making a simple iPhone app that lets you move / scale images that have been imported in a view. I'm using UIGestureRecognizers with @paulsolt 's code below to accomplish this. It works great. The only problem I'm having is when I go to move an object after not having touched the screen or performed any other actions for a while, there is a slight hiccup before it starts moving smoothly.

Does anyone know how to fix this problem?

    - (void)addGestureRecognizersToView:(UIView *)theView {
            theView.userInteractionEnabled = YES;  // Enable user interaction

            UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
            [panGesture setMaximumNumberOfTouches:2];
            [panGesture setDelegate:self];
            [theView addGestureRecognizer:panGesture];
            [panGesture release];


            UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinchGesture:)];
            [theView addGestureRecognizer:pinchGesture];
            [pinchGesture release];
        }


    - (void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer {

            if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
                // Reset the last scale, necessary if there are multiple objects with different scales
                lastScale = [gestureRecognizer scale];
            }

            if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || 
                [gestureRecognizer state] == UIGestureRecognizerStateChanged) {

                CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:@"transform.scale"] floatValue];

                // Constants to adjust the max/min values of zoom
                const CGFloat kMaxScale = 1.5;
                const CGFloat kMinScale = 1.0;
                const CGFloat kSpeed = 0.75;

                CGFloat newScale = 1 -  (lastScale - [gestureRecognizer scale]) * (kSpeed); 
                newScale = MIN(newScale, kMaxScale / currentScale);   
                newScale = MAX(newScale, kMinScale / currentScale);
                CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
                [gestureRecognizer view].transform = transform;

                lastScale = [gestureRecognizer scale];  // Store the previous scale factor for the next pinch gesture call  
            }
        }

    - (void)handlePanGesture:(UIPanGestureRecognizer *)gestureRecognizer {

        UIView *myView = [gestureRecognizer view];

        CGPoint translate = [gestureRecognizer translationInView:[myView superview]];


        if ([gestureRecognizer state] == UIGestureRecognizerStateChanged || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
            [myView setCenter:CGPointMake(myView.center.x + translate.x, myView.center.y + translate.y)];
        [gestureRecognizer setTranslation:CGPointZero inView:[myView superview]];
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

束缚m 2024-12-05 03:51:03

也许因为你这样做了 [panGesture setMaximumNumberOfTouches:2];,它必须确保当前操作在移动视图之前不会捏合?

Maybe because you do this [panGesture setMaximumNumberOfTouches:2];, it has to ensure that the current action is not pinching before moving your view?

南城追梦 2024-12-05 03:51:03

当图像选择器选择 iPhone 相机拍摄的照片时,就会发生这种情况。对于其他图像,它似乎工作得很好。不知道如何解决这个问题。

This is happening when the imagepicker selects a photo taken by the iPhone camera. For other images it seems to work fine. Not sure how to fix this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文