Unified Contacts

30 August, 2014

Adding UITapGestureRecognizer to UIImageView

By default, UIImageView doesn't respond to any touch, such as UITabGestureRecognizer. However, you can enable it by either attributes inspector or code.

- Attributes Inspector
check the box "User Interaction Enabled" in the Interaction section


















- Code
self.imageView.userInteractionEnabled = true



Then, add a UITabGestureRecognizer to UIImageView.

var tapGesture = UITapGestureRecognizer(target: self, action: "tapped:")
self.imageView.addGestureRecognizer(tapGesture)

func tapped(recognizer: UITapGestureRecognizer) {
    println("tapped")
}

Okay, finished. Run and test it :)





UIImageView預設是不會對任何觸碰有反應, 例如UITabGestureRecognizer. 但可以透過attributes inspector或code去啟動.
- Attributes Inspector
在Interaction部份, 鈎選"User Interaction Enabled".


















- Code
self.imageView.userInteractionEnabled = true



之後, 把UITabGestureRecognizer加到UIImageView中.

var tapGesture = UITapGestureRecognizer(target: self, action: "tapped:")
self.imageView.addGestureRecognizer(tapGesture)

func tapped(recognizer: UITapGestureRecognizer) {
    println("tapped")
}

完成了 :)

No comments:

Post a Comment