TouchMoved:withEvent: 和 UIRotationgestureRecognizer 不能一起工作

发布于 2025-01-03 04:27:25 字数 840 浏览 1 评论 0原文

我正在尝试这两种方法来移动和旋转 UIView。两种方法都是分开工作的,但如果我旋转然后移动 UIView,它就会消失。

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {


CGRect rect = self.aView.frame;

UITouch *touch = [touches anyObject];

CGPoint pPoint = [touch previousLocationInView:self.view];
CGPoint cPoint = [touch locationInView:self.view];

float deltaX = cPoint.x - pPoint.x;
float deltaY = cPoint.y - pPoint.y;

rect.origin.x = rect.origin.x + deltaX;
rect.origin.y = rect.origin.y + deltaY;

self.aView.frame = rect;

}
- (void)rotate:(UIRotationGestureRecognizer *) recognizer {

    CGFloat rotation = angle + recognizer.rotation;

    NSLog(@"%f", angle * 180 / M_PI);

    self.aView.transform = CGAffineTransformMakeRotation (rotation);

    if (recognizer.state == UIGestureRecognizerStateEnded) 
        angle = rotation;

}

I'm trying these two methods to move and rotate an UIView. Both methods work separately but if I rotate and then move the UIView it disappears.

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {


CGRect rect = self.aView.frame;

UITouch *touch = [touches anyObject];

CGPoint pPoint = [touch previousLocationInView:self.view];
CGPoint cPoint = [touch locationInView:self.view];

float deltaX = cPoint.x - pPoint.x;
float deltaY = cPoint.y - pPoint.y;

rect.origin.x = rect.origin.x + deltaX;
rect.origin.y = rect.origin.y + deltaY;

self.aView.frame = rect;

}
- (void)rotate:(UIRotationGestureRecognizer *) recognizer {

    CGFloat rotation = angle + recognizer.rotation;

    NSLog(@"%f", angle * 180 / M_PI);

    self.aView.transform = CGAffineTransformMakeRotation (rotation);

    if (recognizer.state == UIGestureRecognizerStateEnded) 
        angle = rotation;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那请放手 2025-01-10 04:27:25

手势识别器优先于 touchMoved,因此很难在同一视图中使用它们。

使用 UIPanGestureRecognizer 而不是 touchMoved 来处理 UIView 的拖动。 来使 UIPanGestureRecognizer 和 UIRotationGestureRecognizer 相互协作。

– gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

然后,您可以通过实现UIGestureRecognizerDelegate 协议中定义的方法

Gesture recognisers take priority over touchMoved, so it's hard to use them both with the same view.

Use a UIPanGestureRecognizer instead of touchMoved to handle dragging the UIView. You can then get the UIPanGestureRecognizer and UIRotationGestureRecognizer to cooperate with one another by implementing the

– gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

method, which is defined in the UIGestureRecognizerDelegate protocol.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文