Swift: Work with View Controllers

Image View如何触发事件？

An image view isn’t a control, so it’s not designed to respond to input in the same way that a control—such as a button—responds. For example, you can’t simply create an action method that’s triggered when a user taps on an image view. (If you try to Control-drag from the image view to your code, you’ll notice that you can’t select Action in the Connection field.)

Fortunately, it’s quite easy to give a view the same capabilities as a control by adding a gesture recognizer to it. Gesture recognizers are objects that you attach to a view that allow the view to respond to actions the way a control does. Gesture recognizers interpret touches to determine whether they correspond to a specific gesture, such as a swipe, pinch, or rotation. You can write an action method that occurs when a gesture recognizer recognizes its assigned gesture, which is exactly what you need to do for the image view.