【发布时间】:2020-05-12 08:01:01
【问题描述】:
我的目标很简单。单击按钮时,该按钮应淡出,屏幕上的另一个图像也应淡出。我正在使用 Swift Playgrounds。 我已经让按钮在点击工作时淡出。主要问题是一旦单击按钮,屏幕上的图像就会淡出。
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