gpt4 book ai didi

iphone - UIGestureRecognizer 结尾

转载 作者:太空狗 更新时间:2023-10-30 03:21:32 30 4
gpt4 key购买 nike

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

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

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

委托(delegate)方法:

- (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 弧度。

欢迎任何建议。谢谢!

最佳答案

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

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.

关于iphone - UIGestureRecognizer 结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4951617/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com