有Touch Up Inside
意见吗?当用户点击或单击视图时,我想调用一个动作。目前我在这个视图上使用了一个隐藏按钮,但这不是一个聪明的方法。
任何帮助表示赞赏:)
有Touch Up Inside
意见吗?当用户点击或单击视图时,我想调用一个动作。目前我在这个视图上使用了一个隐藏按钮,但这不是一个聪明的方法。
任何帮助表示赞赏:)
那么你也可以像这样在 UIView 上使用 addGestureRecognizer 方法:
// In some View controller
UITapGestureRecognizer *tapGR;
tapGR = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)] autorelease];
tapGR.numberOfTapsRequired = 1;
[myUIView addGestureRecognizer:tapGR];
// Add a delegate method to handle the tap and do something with it.
-(void)handleTap:(UITapGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded) {
// handling code
}
}
我不明白为什么使用隐藏按钮不是一种聪明的方式......这是Apple自己建议的方式,我认为它很聪明,目前是更好的方式......
如果- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
您希望检测屏幕上没有按钮的任何位置的触摸,请使用此选项。您还可以像这样找到触摸的位置:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint *tLocation = [touch locationInView:self.view];
}
注意:每次touchesEnded
点击视图时都会调用它,因此每次用户触摸屏幕时,上述内容都会改变。tLocation