这可能已发布在某处,但我找不到。我正在编写一个带有两个 UIView 的简单 iOS 应用程序。用户将首先按住屏幕的某个区域,然后释放其触摸,然后快速触摸下面的第二个视图。
第一个 UIView 附加了一个 UILongPressGestureRecognizer 并且工作正常。第二个 UIView 附加了一个 UITapGestureRecognizer 并且也可以正常工作。但是,我无法让这些手势识别器返回任何表明用户释放了触摸的内容。
我尝试过这段代码但没有成功:
- (void)holdAction:(UILongPressGestureRecognizer *)holdRecognizer
{
if (UIGestureRecognizerStateRecognized) {
holdLabel.text = @"Holding Correctly. Release Touch when ready.";
holdView.backgroundColor = [UIColor greenColor];
} else if (UIGestureRecognizerStateCancelled){
holdLabel.text = @"Ended";
holdView.backgroundColor = [UIColor redColor];
}
任何建议都会很棒,特别是如果有人知道如何实现返回用户触摸设备状态的调用。我查看了开发人员文档,结果一无所获。
This may have been posted here somewhere but I can't find it. I am writing a simple iOS app with two UIViews. The user will first press and hold a certain area of the screen then release their touch on it then quickly touching a second view below.
The first UIView has a UILongPressGestureRecognizer attached to it and works fine. The second UIView has a UITapGestureRecognizer attached to it and also works fine. I cannot however, get either of these gesture recognizers to return anything that states that the user released their touch.
I have tried this code to no avail:
- (void)holdAction:(UILongPressGestureRecognizer *)holdRecognizer
{
if (UIGestureRecognizerStateRecognized) {
holdLabel.text = @"Holding Correctly. Release Touch when ready.";
holdView.backgroundColor = [UIColor greenColor];
} else if (UIGestureRecognizerStateCancelled){
holdLabel.text = @"Ended";
holdView.backgroundColor = [UIColor redColor];
}
Any suggestions would be great and especially if someone knows how to implement a call that returns the state of a user touching the device. I've looked over the developer docs and have come up empty.
发布评论
评论(3)
经过几个小时的修补后,我找到了一种可行的方法,不确定这是否是最好的方法。事实证明我需要像下面的代码一样编写它。我没有调用我在 viewDidLoad() 方法中声明的特定 UIGestureRecognizer。
After tinkering for a couple hours, I found a way that is working, not sure if its the best way to do it. Turns out I need to be writing it like the code below. I wasn't calling the specific UIGestureRecognizer I was declaring in the viewDidLoad() method.
您需要在此处使用手动触摸处理(而不是使用手势识别器)。任何 UIResponder 子类都可以实现以下四种方法:
通过使用这些方法,您可以访问触摸事件的每个阶段。您可能必须实现自己的逻辑来检测长按,但您可以完全访问所有触摸。
有关触摸处理的更多信息,WWDC 2011 的此会议是黄金会议(需要开发帐户):
https://developer.apple.com/itunes/?destination=adc.apple.com.8270634034.08270634040.8367260921?i=1527940296
You need to use manual touch handling here (as opposed to using a gesture recognizer). Any UIResponder subclass can implement the following four methods:
By using these methods, you get access to every phase of the touch events. You might have to implement your own logic to detect the long press, but you have full access to all touches.
For more information on touch handling, this session from WWDC 2011 is golden (requires dev account):
https://developer.apple.com/itunes/?destination=adc.apple.com.8270634034.08270634040.8367260921?i=1527940296
斯威夫特4+
Swift 4+