UIGestureRecognizer 结束

发布于 2024-10-17 02:53:34 字数 1270 浏览 5 评论 0原文

我想在我的 iPhone 应用程序中处理旋转手势并在此期间旋转 imageView。在手势结束时,我想将 imageView 旋转到固定位置。 所以即。如果我将 imageView 从 0 弧度旋转到 M_PI/2 弧度,但在中间的某个位置我以手势结束。结束后我想测试角度,如果它接近 M_PI/2,则将其设置为 M_PI/2,否则设置为 0。
这是我尝试执行此操作的代码:

我创建识别器并将其添加到我的视图中。

UIGestureRecognizer *recognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(gestureRecognized:)];
recognizer.delegate = self;
[self addGestureRecognizer:recognizer];
[recognizer release];

委托方法:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if (_imageView) {
        return YES;
    }
    return NO;
}

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

手势识别方法:

- (void)gestureRecognized:(UIRotationGestureRecognizer *)recognizer {
    _imageView.transform = CGAffineTransformMakeRotation(recognizer.rotation);
}

这些方法都有效,但这是我尝试结束手势的方法。这不起作用:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"gesture end");
}

在转换方面我也遇到了一些问题。有时会回到0弧度。

欢迎任何建议。谢谢!

I would like to handle the rotation gesture in my iPhone app and rotate an imageView during this. At the end of the gesture I would like to rotate to a fix position the imageView.
So ie. if I rotate the imageView from 0 radian to M_PI/2 radians, but somewhere on halfway I end with the gesture. After the end I would like to test the angle and if it close to M_PI/2 then set it to M_PI/2 otherwise to 0.
Here's my code how I tried to do this:

I create and add the recognizer to my view.

UIGestureRecognizer *recognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(gestureRecognized:)];
recognizer.delegate = self;
[self addGestureRecognizer:recognizer];
[recognizer release];

Delegate methods:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if (_imageView) {
        return YES;
    }
    return NO;
}

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

Gesture recognized method:

- (void)gestureRecognized:(UIRotationGestureRecognizer *)recognizer {
    _imageView.transform = CGAffineTransformMakeRotation(recognizer.rotation);
}

These methods are working, but here's the method how I tried to get the end of the gesture. This is not working:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"gesture end");
}

Also with the transform I have a little problem. Sometimes it is going back to the 0 radian.

Any suggestion is welcome. Thanks!

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

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

发布评论

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

评论(3

妄司 2024-10-24 02:53:34

我在其他论坛上找到了答案。我希望这对其他人也有帮助。
回答:

您可以通过检查操作方法中 UIRotationGestureRecognizer 的状态来检测手势的结束。如果 [gestureRecognizer state] == UIGestureRecognizerStateEnded 则完成。如果某些原因导致它被取消(例如,打来电话),那么您会看到它以 UIGestureRecognizerStateCancelled 结束。

如果您想将识别器的旋转直接应用于您的视图,而不是将其视为与先前位置的增量,您可以设置识别器的旋转,其状态为 UIGestureRecognizerStateBegan,然后它将计算该值的偏移量,您可以直接应用它们。这只需要您记住上次结束的旋转,然后当识别器的状态为 UIGestureRecognizerStateBegan 时,您可以执行 [gestureRecognizer setRotation:lastAppliedRotation] 之类的操作。

I've found the answer on an other forum. I hope this will help somebody else too.
Answer:

You can detect the end of the gesture by checking the UIRotationGestureRecognizer's state in your action method. If [gestureRecognizer state] == UIGestureRecognizerStateEnded then it's done. If something had instead caused it to be cancelled (for instance, a phone call coming in) then you'd see it end in UIGestureRecognizerStateCancelled instead.

If you want to apply the recognizer's rotation directly to your view rather than treat it as a delta from the previous location you can set the recognizer's rotation the its state is UIGestureRecognizerStateBegan, and then it will calculate offsets from that value and you can apply them directly. This just requires that you remember the rotation you last ended at, and then you can do something like [gestureRecognizer setRotation:lastAppliedRotation] when the recognizer's state is UIGestureRecognizerStateBegan.

死开点丶别碍眼 2024-10-24 02:53:34

如果gestureRecognizer识别出手势,则默认情况下不会调用touchesEnded(将调用touchesCancelled)。仅当识别器无法识别手势时才会调用touchesEnded )。将gestureRecognizer.delaysTouchesEnded设置为NO,它应该可以工作。

If the gestureRecognizer recognizes the gesture, by default touchesEnded won't be called (touchesCancelled will be called instead. touchesEnded would only be called if the recognizer doesn't recognize the gesture). Set gestureRecognizer.delaysTouchesEnded to NO, and it should work.

神经暖 2024-10-24 02:53:34

以防万一这种情况也发生在您身上,这就是您阅读该问题的原因:
“结束”和“识别”是相同的值(3)。所以这段代码(快速)将不起作用:

if (gesture.state == .recognized || gesture.state == .changed) {
}
else if (gesture.state == .ended) {
//do something else
//=> never called
}

Just in case this happens to you as well and it's the reason you're reading that question :
"ended" and "recognized" are the same value ( 3 ). So this code (in swift) won't work:

if (gesture.state == .recognized || gesture.state == .changed) {
}
else if (gesture.state == .ended) {
//do something else
//=> never called
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文