iPhone的iOS如何使UIRotationGestureRecognizer和UIPinchGestureRecognizer一起工作来缩放和旋转UIView子视图?
我在我的应用程序中实现了拖放/resize/旋转标签。 到目前为止,除了UIRotationGestureRecognizer
手势之外,一切正在工作。 更具体地说,它不适用于UIPinchGestureRecognizer
手势。
通常这两个手势竞争两个手指触摸,所以我正在并行运行它们。 以下是手势识别器调用的两种方法。
当做旋转手势时, 视图围绕它的中心旋转 ,高度和宽度变化如下:高度变成宽度,宽度变成高度。 最终,视图消失。
在视图中,我有另一个自动resize的视图。 通常,捏手势也会自动调整子视图的大小,但在这种情况下,自动调整遮罩的子视图消失。 子视图有高度和宽度的弹簧和左/顶撑杆。
我究竟做错了什么? 我怎样才能调整和扩大手势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); }
如果你想让两个gestureRecognisers
并行(同时)你的view
应该实现<UIGestureRecognizerDelegate>
。
另外,你应该使它成为两个gestureRecognizers
的代表。
rotationGestureRecognizer.delegate=self; pinchGestureRecognizer.delegate=self;
你也应该实现shouldRecognizeSimultaneouslyWithGestureRecognizer:
方法:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; }
注意:如果在view
有两个以上的gestureRecognisers
,那么你将不得不在这个方法中添加一些身份检查。
编辑:
刚刚find了Ole Begemann关于这个话题的文章: 关注于细节的iOS上的手势识别