如何降低捏缩放 UIGestureRecognizer 的速度

Posted

技术标签:

【中文标题】如何降低捏缩放 UIGestureRecognizer 的速度【英文标题】:How to reduce velocity of pinch-zoom UIGestureRecognizer 【发布时间】:2014-04-07 03:31:16 【问题描述】:

我创建了一个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% 左右缩放?

【问题讨论】:

您是否尝试手动设置您的scrollViewzoomScale @NicolasBonnet 不幸的是,这个特定的没有包裹在滚动视图中,所以这样做是不可行的:| 【参考方案1】:

你有没有试过把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);


【讨论】:

我相信这也可以,但是为什么要裁剪您的比例值,我认为没有必要这样做。 裁剪 Scale 值,因为它会限制您的缩放属性。 好的,抱歉,我已经快速阅读了原始代码并错过了那部分。 不,这根本不起作用 - 有一些非常奇怪的相反效果:|【参考方案2】:

试试这个。

- (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;
    


点是

使用框架实现缩放。 将比例变量更改为慢/速缩放。

【讨论】:

尝试了很多次,但似乎没有任何效果。【参考方案3】:

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

注意底部添加的将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
    

【讨论】:

哦,是的,我的代码中没有添加那行,我的错误。 @HimanshuJoshi 人力资源部。现在你让我想到了——也许你的建议在这行之后起作用。打算试试看。

以上是关于如何降低捏缩放 UIGestureRecognizer 的速度的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Flutter 小部件测试中执行缩放(捏缩放)多指手势?

在objective-c中捏缩放后如何删除顶部和底部空间?

如何限制将捏缩放图像移动到图像边框?

如何使用 Ionic Capacitor 启用捏缩放?

Apple Photos collectionview 捏缩放如何工作?

如何防止在 Safari 移动浏览器上捏缩放(滚动)?