UIImageView Recognizer

半世苍凉 提交于 2019-12-23 06:29:29

问题


In my app I have a big UIImageView and other 5 smaller.

My intention is to copy the content of the selected small Image to the biggest one. Also to get the name of the chosen image and show with a UILabel.

Will be maybe easier to replace the small images by UIButtons and display the image as a background?


回答1:


I would add a gestureRecognizer to each imageView. The function called by the gestureRecognizer would then change the image on the "big picker view". There is no built-in way to get the image name that the user chose. If you really needed this, then you could sublcass UIImageView and add a fileName property.

@IBOutlet var imageView: UIImageView!
@IBOutlet var bigPickerImageView: UIImageView!

override func viewDidLoad() {
    super.viewDidLoad()

    // create tap gesture recognizer
    let tapGesture = UITapGestureRecognizer(target: self, action: "tapGesture:")

    // add it to the image view;
    imageView.addGestureRecognizer(tapGesture)
    // make sure imageView can be interacted with by user
    imageView.userInteractionEnabled = true        
}

func tapGesture(gesture: UIGestureRecognizer) {
    // if the tapped view is a UIImageView then set it to imageview
    if let imageView = gesture.view as? UIImageView {  // if you subclass UIImageView, then change "UIImageView" to your subclass
        // change the image on the bigPickerImageView
        bigPickerImageView.image = imageView.image
        // if you subclass UIImageView, then you could get the filename here.
    }
}


来源:https://stackoverflow.com/questions/27230720/uiimageview-recognizer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!