你怎么知道什么对象被触摸的的touchesBegan?
-
27-09-2019 - |
题
我知道,这是一个很常见的问题,但所有的每一个网站上的答案不工作!如果你还是不明白我的意思,那么也许这行代码将有助于您理解。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if (touch.view == nextbutton)
[self performSelector:@selector(next)];
if (touch.view == prevbutton)
[self performSelector:@selector(previous)];
if (touch.view == moreoptionsbutton)
[self performSelector:@selector(moresettings)];
}
这不,当你触摸nextbutton, prevbutton, and more optionsbutton
,这是顺便说一下UIImageViews
做任何事情。我一直在使用isEqual:
代替==
也尝试过,但还没有任何解决。任何建议?
解决方案
您必须设置userinteractionEnabled = YES您所有的UIImageViews否则他们将不会收到触摸事件。也改变了行:
UITouch *touch = [[event allTouches] anyObject];
到
UITouch *touch = [touches anyObject];
其他提示
我创建了一个检查,以确保它我希望之前将被点击的视图。
if( [touch.view isKindOfClass:[Custom class]] ){
CGPoint touchPoint = [touch locationInView:self.view];
if( CGRectContainsPoint( self.customClassOne.frame, touchPoint )
|| CGRectContainsPoint( self.customClassTwo.frame, touchPoint ) ){
[self touchOnCustomClass];
}
}
不隶属于 StackOverflow