如何使UIImageView可以兼容并使它做某事?(迅速)

Leo*_*isi 8 xcode touch uiimage swift

(我刚刚开始使用Swift,并且对编程比较新,所以请耐心等待.)我试图在屏幕上显示随机块,用户必须点击它们才能使它们消失.我已经能够创建块,但我不知道如何实际使它们可以应用.有人可以帮帮我吗?到目前为止这是我的代码:

func createBlock(){

    let imageName = "block.png"
    let image = UIImage(named: imageName)
    let imageView = UIImageView(image: image!)

    imageView.frame = CGRect(x: xPosition, y: -50, width: size, height: size)
    self.view.addSubview(imageView)



    UIView.animateWithDuration(duration, delay: delay, options: options, animations: {

        imageView.backgroundColor = UIColor.redColor()

        imageView.frame = CGRect(x: self.xPosition, y: 590, width: self.size, height: self.size)

        }, completion: { animationFinished in


            imageView.removeFromSuperview()


    })



}
Run Code Online (Sandbox Code Playgroud)

这是新代码:

func createBlock(){


    let imageName = "block.png"
    let image = UIImage(named: imageName)
    let imageView = UIImageView(image: image!)

    imageView.frame = CGRect(x: xPosition, y: -50, width: size, height: size)
    self.view.addSubview(imageView)

    imageView.userInteractionEnabled = true
    let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("imageTapped"))
    imageView.addGestureRecognizer(tapRecognizer)

    func imageTapped(gestureRecognizer: UITapGestureRecognizer) {
        let tappedImageView = gestureRecognizer.view!
        tappedImageView.removeFromSuperview()
        score += 10
    }



    UIView.animateWithDuration(duration, delay: delay, options: options, animations: {

        imageView.backgroundColor = UIColor.redColor()
        imageView.frame = CGRect(x: self.xPosition, y: 590, width: self.size, height: self.size)

        }, completion: { animationFinished in


            imageView.removeFromSuperview()


    })



}
Run Code Online (Sandbox Code Playgroud)

Jer*_*ope 34

创建视图后,需要将其userInteractionEnabled属性设置为true.然后你需要附加一个手势.

imageView.userInteractionEnabled = true
//now you need a tap gesture recognizer
//note that target and action point to what happens when the action is recognized.
let tapRecognizer = UITapGestureRecognizer(target: self, action: Selector("imageTapped:"))
//Add the recognizer to your view.
imageView.addGestureRecognizer(tapRecognizer)
Run Code Online (Sandbox Code Playgroud)

现在你仍然需要这个函数,在这种情况下是imageTapped:,这是识别手势时动作发生的地方.识别出的手势将作为参数发送,您可以从手势视图属性中找出从哪个图像视图中点击.

func imageTapped(gestureRecognizer: UITapGestureRecognizer) {
    //tappedImageView will be the image view that was tapped.
    //dismiss it, animate it off screen, whatever.
    let tappedImageView = gestureRecognizer.view!
}
Run Code Online (Sandbox Code Playgroud)

  • 不需要使用Selector()"imageTapped:"也可以 (2认同)

Jav*_*ría 5

在Swift 3.0中:

        imvEditName.isUserInteractionEnabled = true
        let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped))
        imvEditName.addGestureRecognizer(tapRecognizer)
Run Code Online (Sandbox Code Playgroud)

和目标方法:

func imageTapped(sender: UIImageView) {
    print("image tapped")
}
Run Code Online (Sandbox Code Playgroud)