繁体   English   中英

iPhone iOS如何让UIRotationGestureRecognizer和UIPinchGestureRecognizer一起工作来扩展和旋转带有子视图的UIView?

[英]iPhone iOS how to make UIRotationGestureRecognizer and UIPinchGestureRecognizer to work together to scale and rotate a UIView with subviews?

我正在我的应用程序中实现拖放/调整大小/旋转标签。 到目前为止,除了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);    



}

如果您希望两个gestureRecognisers并行(同时)运行,您的view应该实现<UIGestureRecognizerDelegate>

此外,您应该使它成为两个gestureRecognizers的委托。

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

你还应该实现shouldRecognizeSimultaneouslyWithGestureRecognizer:方法:

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

    return YES;
}

注意:如果您在view有更多这两个gestureRecognisers ,则必须在此方法中添加一些身份检查。

编辑:

刚刚找到了Ole Begemann关于这个主题的文章: iOS上的手势识别,注重细节

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM