点击/单击查看动作 Xcode
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11382304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
tap/click view action Xcode
提问by ph3nx
is there a Touch Up Inside
for views?
I want to call an action when the user tap's or click's a view.
Currently I use a hidden button over this view, but thats not a smart way.
有Touch Up Inside
意见吗?我想在用户点击或点击视图时调用一个动作。目前我在这个视图上使用了一个隐藏的按钮,但这不是一个聪明的方法。
Any help is appreciated :)
任何帮助表示赞赏:)
回答by Cliff Ribaudo
Well you can also use the addGestureRecognizer method on UIView like so:
那么你也可以像这样在 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
}
}
回答by aleroot
I don't understand why using the hidden button would be not a smart way ... It is the suggested way by Apple itself, i think it is smart and the better way at the moment ...
我不明白为什么使用隐藏按钮不是一种聪明的方式......这是Apple本身建议的方式,我认为它是明智的并且目前更好的方式......
回答by Conor Taylor
Use - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
if you are looking to detect touches anywhere on the screen with no buttons. You can also find the location of the touch like so:
使用- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
,如果你正在寻找任何地方发现没有按钮的触摸屏上。您还可以像这样找到触摸的位置:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint *tLocation = [touch locationInView:self.view];
}
Note: touchesEnded
is called EVERY SINGLE TIMEthe view is tapped, so the above tLocation
will change every time the user touches the screen.
注意:touchesEnded
被称为EVERY SINGLE TIME视图被点击,因此tLocation
每次用户触摸屏幕时上述内容都会改变。