UIGestureRecognizer 结束
我想在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在其他论坛上找到了答案。我希望这对其他人也有帮助。
回答:
I've found the answer on an other forum. I hope this will help somebody else too.
Answer:
如果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). SetgestureRecognizer.delaysTouchesEnded
toNO
, and it should work.以防万一这种情况也发生在您身上,这就是您阅读该问题的原因:
“结束”和“识别”是相同的值(3)。所以这段代码(快速)将不起作用:
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: