开发者

UIPanGestureRecognizer gets stuck before moving after image is idle for a bit

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 - [gestureRecogn开发者_开发百科izer 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]];
        }
    }


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜