gpt4 book ai didi

ios - UIPinchGestureRecognizer 向内捏 "slower"

转载 作者:行者123 更新时间:2023-12-01 17:10:07 26 4
gpt4 key购买 nike

我在我的应用程序中使用 UIPinchGestureRecognizer 来放大 View (是的,我不使用 UIScrollView 是有原因的)。当我用手指向外捏合时, View 会按预期放大,如果我在不将手指从屏幕上移开的情况下反向捏合,它也会向右缩放。但是,如果我通过向内捏来启动缩放,则 View 缩放的速度会明显变慢。我猜这是因为 UIPinchGestureRecognizer 的工作原理 - 向外捏时 UIPinchGestureRecognizer 的比例为 >1,向内捏时为 <1。不幸的是,我不知道如何在我的代码中准确地反射(reflect)这一点。

- (IBAction)didDetectPinchGesture:(id)sender {
UIPinchGestureRecognizer *gestureRecognizer = (UIPinchGestureRecognizer *)sender;

CGFloat scale = [gestureRecognizer scale];

switch ([gestureRecognizer state]) {
case UIGestureRecognizerStateBegan:
_lastScale = [gestureRecognizer scale];
break;

case UIGestureRecognizerStateChanged:

CGFloat currentScale = [[self.imageView.layer valueForKeyPath:@"transform.scale"] floatValue];

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

CGFloat newScale = 1 - (_lastScale - scale); // new scale is in the range (0-1)
newScale = MIN(newScale, kMaxScale / currentScale);
newScale = MAX(newScale, kMinScale / currentScale);

NSLog(@"%f", newScale);

CGAffineTransform transform = CGAffineTransformScale([self.imageView transform], newScale, newScale);
self.imageView.transform = transform;

_lastScale = scale; // Store the previous scale factor for the next pinch gesture call
break;

default:
_lastScale = [gestureRecognizer scale];
break;
}
}

最佳答案

一个非常简单的解决方案是重置 gestureRecognizer scale完成后返回 1:

    ...
default:
_lastScale = [gestureRecognizer scale];
// Add this:
[gestureRecognizer setScale:1];
break;
}

关于ios - UIPinchGestureRecognizer 向内捏 "slower",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11170350/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com