/ Swift

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.)

Image view不是控件,所以它不能像其他本来就是为了对输入作出响应的控件——比如按钮一样作出反应。例如,你无法创建一个针对用户轻触image view时候触发的方法(如果你Control拖拽image view到代码,你会发现没有Action这个选项可以使用)。

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.

幸运的是,通过简单地给一个view添加gesture recognizer就可以使它具备和控件相当的功能。Gesture recognizer是一种特殊的对象,它可以使被附加的view像控件一样具备响应的功能。Gesture recognizer对触控进行判断,例如轻扫、捏合或者旋转。你可以自定义一个方法,专门针对特定的手势作出响应,这也正是你需要对image view做的事情。

Yin Guobing

Yin Guobing

BOE技术研发工程师🔬,业余码农😳,蓝猫铲屎官🐈。曾独立开发了一款iOS APP并上线🎉。现居北京,正在为了理想中的生活而奋斗..

Read More