简体   繁体   English

在 iPhone 中检测 UIImageView 上的触摸

[英]Detecting touches on a UIImageView in IPhone

I'm programming a King Chess game on the iPhone.我正在 iPhone 上编写 King Chess 游戏。 When the user touches a square on the screen, I want to know which square was selected.当用户触摸屏幕上的一个正方形时,我想知道选择了哪个正方形。

I am using UIImageViews to show squares on the screen.我正在使用 UIImageViews 在屏幕上显示正方形。

UIImageview would not be feasible in your scenario. UIImageview 在您的场景中不可行。 You can use UIButton with customtype and assign image to that button.您可以将 UIButton 与 customtype 一起使用,并将图像分配给该按钮。 So it looks like button but you can add target and you know which square touch/pressed.所以它看起来像按钮,但您可以添加目标并且您知道哪个方形触摸/按下。

Hope this help.希望这有帮助。

in touchesEnded: method, u can get the position of ur touch.touchesEnded:方法中,您可以获得您触摸的 position。 Then, u go thru all the image views (squares), comparing their frame positions using CGRectIntersectsRect and find which square is touched.然后,u go 通过所有图像视图(正方形),使用CGRectIntersectsRect比较它们的帧位置并找出触摸了哪个正方形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM