How to check an ID of a object that has been touched (iOS)

后端 未结 2 528
盖世英雄少女心
盖世英雄少女心 2021-01-26 10:13

In my View I have an array with a bunch of different points, then I run that array through a loop to create a bunch of different squares in the view. You can also see that I tri

相关标签:
2条回答
  • 2021-01-26 10:32

    You could use a UIImageView for putting the image on screen instead of drawRect: and use the tag of that image view for identifying which view it is.

    The tag property is available for all UIView and its subclasses. (UIButton, UIImageView etc)

    From your approach I think there won't be multiple view's when you draw UIImage inside the drawRect: . All the images will be drawn into one single view. So in addition to you not able to identify an image, when you do a remove from superview,it won't work as expected. Using a UIImageView would solve a lot of problems, I guess.

    0 讨论(0)
  • 2021-01-26 10:46

    Rakesh is definitely right with his comments, use UIImageView if you can.

    Also touchesMoved will not detect your squares because you haven't added them as subviews, they are only images drawn in your view, which is another reason you should rather use uiimageviews.

    If you are insisting on doing it this (much harder) way: I suggest you to keep an NSDictionary with your ids as keys and cgrect frames of your squares as objects. And then with every touch iterate through all possible keys and call CGRectContainsPoint on your objects. Return the affiliated id (key) when point is contained within the rect. But I don't recommend doing so.

    0 讨论(0)
提交回复
热议问题