【问题标题】:How to make an image disappear after clicking it?如何让图片点击后消失?
【发布时间】:2019-10-19 09:52:24
【问题描述】:

我想让一个小image.png在点击后消失。 我是 swift 的新生儿,所以我在 Playground 上使用单一视图进行实验。谁能帮帮我?

【问题讨论】:

    标签: swift xcode11 swift5.1


    【解决方案1】:

    在创建 your_imageView 时添加此代码

    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(imageTapped(tapGestureRecognizer:)))
    your_imageView.isUserInteractionEnabled = true
    your_imageView.addGestureRecognizer(tapGestureRecognizer)
    
    func imageTapped(tapGestureRecognizer: UITapGestureRecognizer)
    {
        let tappedImage = tapGestureRecognizer.view as! UIImageView
    
        tappedImage.isHidden = true
    }
    

    【讨论】:

    • 谢谢!但为什么它给我“在声明之前使用局部变量'imageTapped(tapGestureRecognizer:)'”?
    • 将 imageTapped() 函数的定义移到创建手势识别器的代码上方。这应该可以解决您的“在声明之前使用局部变量'imageTapped(tapGestureRecognizer:)'”的问题。
    猜你喜欢
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 2018-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多