如何降低捏拉缩放UIGestureRecognizer的速度

时间:2014-03-05 07:44:49

标签: ios objective-c uipinchgesturerecognizer

我创建的UIGestureRecognizer非常像 this one

- (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 = 2.0;
        const CGFloat kMinScale = 1.0;

        CGFloat newScale = 1 -  (lastScale - [gestureRecognizer scale]); 
        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  
    }
}

这可以按预期工作,但我的客户希望它对触摸敏感。如何降低收缩的速度(向内和向外),使其以约80%的默认速度放大?

3 个答案:

答案 0 :(得分:0)

您是否尝试scale当前值为80%。

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

    CGFloat maxScale = 2.0;
    CGFloat currentScale = [gestureRecognizer scale];
    currentScale = 0.8 * currentScale;   //80% of scaling

    if(currentScale < 0.8)
        currentScale = 0.8;

    if(currentScale > maxScale)
        currentScale = maxScale;

    [gestureRecognizer view].transform = CGAffineTransformScale([[gestureRecognizer view] transform], currentScale, currentScale);

}

答案 1 :(得分:0)

试试这个。

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

    if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
        if (CGRectIsEmpty(self.initalFrame)) {
            self.initalFrame = gestureRecognizer.view.frame;
            // store view's original frame and never change it
        }

        if (self.preScale == 0.f) {
            self.preScale = 1.f; 
        }

        gestureRecognizer.scale = self.preScale;
        // gestureRecognizer and view should share the same scale state at the beginning
    }

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

        const CGFloat kMaxScale = 2.0;
        const CGFloat kMinScale = 1.0;

        CGFloat newScale = gestureRecognizer.scale;

        newScale = (newScale - self.preScale) * 0.8 + self.preScale;

        newScale = MIN(newScale, kMaxScale);
        newScale = MAX(newScale, kMinScale);


        CGRect newFrame = self.initalFrame;
        newFrame.size.height *= newScale;
        newFrame.size.width *= newScale;
        gestureRecognizer.view.frame = newFrame;
        self.preScale = newScale;
    }

}

要点

  • 使用框架来实现规模。
  • 更改为scale变量的慢速/速度缩放。

答案 2 :(得分:0)

原来,缩放的过快速度实际上是链接答案代码中的一个错误。可悲的是,这实际上并没有回答我的实际问题(我仍然想要一个答案!),但它确实有助于解决我客户的问题。

请注意底部添加的行将gestureRecognizer重置为1:

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

        ...

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

        gestureRecognizer.scale = 1;
        // ^ added this line
    }
}