本文介绍了iPhone iOS如何使UIRotationGestureRecognizer和UIPinchGestureRecognizer一起工作以扩展和旋转带有子视图的UIView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm implementing a drag/drop/resize/rotate labels within my app. So far everything is working except for the UIRotationGestureRecognizer gesture. More specifically, it does not work with the UIPinchGestureRecognizer gesture.

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

Normally the two gestures compete for two finger touches, so I'm running them in parallel. Below are my 2 methods that the gesture recognizers invoke.

在进行旋转手势时,视图在其中心周围旋转,其高度和宽度如下变化:高度变为宽度,宽度慢慢变成高度。最终,视图消失。

When doing the rotation gesture, the view spins wildly around it's center, with it's height and width changing as follows: height becomes width, width slowly turns into height. Eventually, the view disappears.

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

Within the view, I have another auto-resizing view. Normally, the pinch gesture automatically resizes the subviews as well, but in this case the subviews with autoresizing masks disappear. The subviews have height and width springs and left/top strut.

我做错了什么?如何通过手势调整和缩放UIView?

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

All the delegate methods and connections are setup properly. I need to understand how to handle the order in which the recognizers would be applying the scaling and rotation.

//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);    



}


推荐答案

如果你想要两个 gestureRecognisers 并行(同时)你的
视图应该实现< UIGestureRecognizerDelegate>

If you want two gestureRecognisers to run in parallel (simultaneously) your view should implement <UIGestureRecognizerDelegate>.

此外,你应该让它成为 gestureRecognizers 。

Also, you should make it a delegate of both gestureRecognizers.

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

你还应该实现 shouldRecognizeSimultaneouslyWithGestureRecognizer:方法:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

    return YES;
}

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

NOTE: if you have more then this two gestureRecognisers in your view you're gonna have to add some identity checking in this method.

编辑:

刚刚找到Ole Begemann关于此主题的文章:

Just found Ole Begemann's article on this topic: Gesture Recognition on iOS with Attention to Detail

这篇关于iPhone iOS如何使UIRotationGestureRecognizer和UIPinchGestureRecognizer一起工作以扩展和旋转带有子视图的UIView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 15:21