如果单击按钮 - 为图像制作动画 Swift 游乐场

If Button clicked - do animation to image Swift Playgrounds

我的目标很简单。单击按钮时,按钮应该淡出,屏幕上的另一个图像也应该淡出。我正在使用 Swift 游乐场我已经有了点击时淡出的按钮工作。主要问题是单击按钮后屏幕上的图像会淡出。

import PlaygroundSupport
import AVFoundation
class MyViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let title = UIImage(named: "Picture1")
        let imageView = UIImageView(image: title) //this is the image I want to fade out when the button is clicked
        imageView.image = title
        imageView.frame.size.width = 570
        imageView.frame.size.height = 140
        imageView.frame.origin.x = 100
        imageView.frame.origin.y = 0
        imageView.alpha = 0

        let button = UIButton(type: UIButton.ButtonType.custom) as UIButton
        let image = UIImage(named: "startbutton4.png") as UIImage?
        button.frame = CGRect(x: 265, y: 300, width: 249, height: 85)
        button.setImage(image, for: [])
        button.contentMode = .center
        button.imageView?.contentMode = .scaleAspectFit


        button.imageView?.contentMode = .scaleAspectFit
        button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
        self.view.addSubview(button)
        view.addSubview(imageView)
    }

    @objc func buttonAction(sender: UIButton) {
        sender.isHighlighted = false
        UIView.animate(withDuration: 1, delay: 1, options: .curveEaseOut, animations: {
           sender.alpha = 0
        }, completion: nil) // <--- this right here is the button fading out 
    }

}
let master = MyViewController()
PlaygroundPage.current.liveView = master

如果有人知道如何执行此操作,请告诉我。

我的理解是您想隐藏按钮...隐藏按钮后您想显示图像..这是相关代码..我希望它会有所帮助

func buttonAction(sender: UIButton) {
    sender.isHighlighted = false
    UIView.animate(withDuration: 1, delay: 1, options: .curveEaseOut, animations: {
       sender.alpha = 0
    }, completion: { success in
        if success {
        UIView.animate(withDuration: 1, delay: 1, options: .curveEaseOut, animations: {
           imageView.alpha = 1
            })
        }
    })
}