我想检测用户触摸释放,如果用户持有它,则代码在
下面有效,但不要告诉我,如果我握着(触摸并按住而不是释放)触摸......
请帮我解决这个问题
[imageview setUserInteractionEnabled:YES];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(holdAction:)];
[singleTap setNumberOfTapsRequired:1];
[imageview addGestureRecognizer:singleTap];
- (void)holdAction:(UIGestureRecognizer *)holdRecognizer
{
if (holdRecognizer.state == UIGestureRecognizerStateBegan) {
NSLog(@"Holding Correctly. Release when ready.");
} else if (holdRecognizer.state == UIGestureRecognizerStateEnded)
{
NSLog(@"You let go!");
}
}
答案 0 :(得分:1)
使用-touchesBegan:withEvent:
和-touchesEnded:withEvent:
方法执行此操作。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.isHolding = YES;
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.isHolding = NO;
}
其中self.isHolding是@property (nonatomic, assign) BOOL isHolding;
注意:在这些方法中,您可能需要执行额外的检查,以确定是否已在特定视图上开始触摸以及它们已结束的位置。
更新:相应地更改您的代码:
[imageview setUserInteractionEnabled:YES];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(holdAction:)];
[imageview addGestureRecognizer:longPress];
- (void)holdAction:(UILongPressGestureRecognizer *)holdRecognizer
{
if (holdRecognizer.state == UIGestureRecognizerStateBegan) {
NSLog(@"Holding Correctly. Release when ready.");
} else if (holdRecognizer.state == UIGestureRecognizerStateEnded)
{
NSLog(@"You let go!");
}
}