问题描述 投票:0回答:1

我的目标很简单,当一个按钮被点击时,按钮应该淡出,所以屏幕上应该有另一个图像。当一个按钮被点击时,该按钮应该淡出,屏幕上的另一张图片也应该淡出。我使用的是 迅捷游乐场. 我已经得到了按钮淡出时点击工作. 主要的问题是,一旦点击按钮,屏幕上的图像就会淡出。

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

如果有人知道如何做到这一点,请告诉我。

ios swift xcode swift-playground swift4.2
1个回答
0
投票

我的理解是你要隐藏按钮......一旦按钮被隐藏,你要显示图像......这里是代码......我希望它将有助于

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
            })
        }
    })
}
© www.soinside.com 2019 - 2024. All rights reserved.