简体   繁体   中英

How do I programmatically make clicking on a UIImageView do something on the iPhone?

I have programmatically added a UIImageView. How do I make it send a message to my controller when the users clicks it, sending itself as the only parameter?

I wouldn't use a UIImageView unless there was some other compelling reason. I'd just use a custom UIButton and use the UIImage as its background.

  1. Create a new class that extends UIImageView and use that.
  2. In your class, override the touch events - touchesBegan, touchesEnded and/or touchesMoved as appropriate
  3. From those methods, call back to a method on the controller.

I'd just put a custom button over the UIImage view, and use normally - buttons with Custom style are invisible and so will not obscure the image. It's also a great way to turn regions of text into clickable areas as well.

Or, you could assign the custom image as a graphic background for the button.

just put a button over the imageview, with the Alpha attribute set to 0.10 or less. you can capture the button's tap event but almost cannot see it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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