xcode [IOS SDK]-touchesBegan 以特定对象开始?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4473373/
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
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:01:31 来源:igfitidea点击:
[IOS SDK]- touchesBegan with specific object?
提问by mohacs
When I touch anywhere on screen touchesBegan event triggered. but I couldn't manage how if I touch specific object as like UIImageView?
当我触摸屏幕上的任何地方时触发 touchesBegan 事件。但是如果我像 UIImageView 一样触摸特定对象,我无法管理如何?
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView: touch.view];
imageView.center = location;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
}
回答by mohacs
Ok, found the solution, here is the code:
好的,找到了解决方案,代码如下:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([touch view] == imageView) {
CGPoint location = [touch locationInView: self.view];
imageView.center = location;
}
}