【问题标题】:iPhone iOS how to make UIRotationGestureRecognizer and UIPinchGestureRecognizer to work together to scale and rotate a UIView with subviews?iPhone iOS 如何使 UIRotationGestureRecognizer 和 UIPinchGestureRecognizer 协同工作以缩放和旋转带有子视图的 UIView?
【发布时间】:2012-04-02 19:47:54
【问题描述】:

我正在我的应用程序中实现拖放/调整大小/旋转标签。到目前为止,除了UIRotationGestureRecognizer 手势外,一切正常。更具体地说,它不适用于UIPinchGestureRecognizer 手势。

通常这两个手势竞争两个手指触摸,所以我并行运行它们。以下是手势识别器调用的两种方法。

在做旋转手势时,视图围绕它的中心疯狂地旋转,它的高度和宽度变化如下:高度变成宽度,宽度慢慢变成高度。最终,视图消失了。

在视图中,我有另一个自动调整大小的视图。通常,捏合手势也会自动调整子视图的大小,但在这种情况下,带有自动调整大小掩码的子视图会消失。子视图具有高度和宽度弹簧以及左/上支柱。

我做错了什么?如何使用手势调整 UIView 的大小和缩放比例?

所有委托方法和连接均已正确设置。 我需要了解如何处理识别器应用缩放和旋转的顺序。

//makes 2 gesture recognizers behave together
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
    return YES;
}

- (IBAction)handleRotationFrom:(id)sender {
    NSLog(@"Gesture rotation %.1f", rotationGestureRecognizer.rotation);

//attempt to continuously rotate the label, starting with a remembered rotation    

    float rotation = atan2(activeCompanionLabelView.transform.b, activeCompanionLabelView.transform.a);
    NSLog(@"existing rotation %.1f", rotation);

//    rotation = rotation<0?(2*M_PI)-fabs(rotation):rotation;
    rotation +=rotationGestureRecognizer.rotation;

    NSLog(@"*** gesture rotation %.1f sum: %.1f, saved: %.1f",rotationGestureRecognizer.rotation, rotation, activeCompanionLabelView.savedRotation);
    activeCompanionLabelView.transform = CGAffineTransformMakeRotation((rotation));
    activeCompanionLabelView.savedRotation = rotation;
}

- (IBAction)handlePinch:(id)sender {
    NSLog(@"pinch %.2f", pinchGestureRecognizer.scale);

//resize, keeping the origin where it was before

    activeCompanionLabelView.frame = CGRectMake(activeLabelContainerFrame.origin.x, activeLabelContainerFrame.origin.y, activeLabelContainerFrame.size.width*pinchGestureRecognizer.scale, activeLabelContainerFrame.size.height*pinchGestureRecognizer.scale);    



}

【问题讨论】:

    标签: iphone objective-c ios uiview uigesturerecognizer


    【解决方案1】:

    如果您希望两个 gestureRecognisers 并行(同时)运行您的 view 应该实现 &lt;UIGestureRecognizerDelegate&gt;

    另外,你应该让它成为gestureRecognizers的代表。

    rotationGestureRecognizer.delegate=self;
    pinchGestureRecognizer.delegate=self;
    

    你还应该实现shouldRecognizeSimultaneouslyWithGestureRecognizer:方法:

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    
        return YES;
    }
    

    注意:如果您的 view 中有两个以上的 gestureRecognisers,您将不得不在此方法中添加一些身份检查。

    编辑:

    刚找到Ole Begemann关于这个话题的文章:Gesture Recognition on iOS with Attention to Detail

    【讨论】:

    • 我已经有了这个代码。我用这个信息澄清了这个问题。
    • @AlexStone:我明白了。我目前不在我的 Mac 上,但需要对此进行一些测试。如果没有其他人站出来,我明天可以修改我的答案。
    • 我现在也有同样的问题.. 你有解决办法吗?
    猜你喜欢
    • 2015-08-21
    • 2012-12-03
    • 2021-12-23
    • 2012-04-06
    • 2012-12-01
    • 1970-01-01
    • 2023-03-19
    • 2010-10-09
    • 1970-01-01
    相关资源
    最近更新 更多