等到用户触摸屏幕。 Swift
Wait until the user touches the screen. Swift
发生某些事情后,我制作了一个带有标签的视图,
let myView = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
let label = UILabel(frame: CGRect(x:0,y:0,width: 100, height: 100))
myView.addSubview(lebel)
self.view.addSubview(myView)
myView.alpha = 0
UIView.animate(withDuration: 0.5, animations: {
myView.alpha = 1
})
现在我要你等到在屏幕上进行点击,然后
UIView.animate(withDuration: 0.5, animations: {
muView.alpha = 0
}, completion: { (bool) in
myView.removeFromSuperview()
})
如何才能等到触摸屏幕?
只需将点击手势添加到您的视图中
在viewDidload
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(_:)))
self.view.addGestureRecognizer(tapGesture)
处理 TapGesture
@IBAction func handleTapGesture(_ recognizer: UITapGestureRecognizer) {
UIView.animate(withDuration: 0.5, animations: {
muView.alpha = 0
}, completion: { (bool) in
myView.removeFromSuperview()
})
}
除了 Abdelahad 的回答之外,您还应该将 let myView
声明为 property
,以便在 @IBAction func handleTapGesture(_ recognizer: UITapGestureRecognizer) {}
中使用它。因此,新代码如下:
@IBAction func handleTapGesture(_ recognizer: UITapGestureRecognizer) {
UIView.animate(withDuration: 0.5, animations: {
self.myView.alpha = 0
}, completion: { (bool) in
self.myView.removeFromSuperview()
})
}
最后补充一下,我发现你的代码有点错误。 muView
错误,myView
正确。
发生某些事情后,我制作了一个带有标签的视图,
let myView = UIView(frame: CGRect(x: 100, y: 100, width: 100, height: 100))
let label = UILabel(frame: CGRect(x:0,y:0,width: 100, height: 100))
myView.addSubview(lebel)
self.view.addSubview(myView)
myView.alpha = 0
UIView.animate(withDuration: 0.5, animations: {
myView.alpha = 1
})
现在我要你等到在屏幕上进行点击,然后
UIView.animate(withDuration: 0.5, animations: {
muView.alpha = 0
}, completion: { (bool) in
myView.removeFromSuperview()
})
如何才能等到触摸屏幕?
只需将点击手势添加到您的视图中
在viewDidload
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture(_:)))
self.view.addGestureRecognizer(tapGesture)
处理 TapGesture
@IBAction func handleTapGesture(_ recognizer: UITapGestureRecognizer) {
UIView.animate(withDuration: 0.5, animations: {
muView.alpha = 0
}, completion: { (bool) in
myView.removeFromSuperview()
})
}
除了 Abdelahad 的回答之外,您还应该将 let myView
声明为 property
,以便在 @IBAction func handleTapGesture(_ recognizer: UITapGestureRecognizer) {}
中使用它。因此,新代码如下:
@IBAction func handleTapGesture(_ recognizer: UITapGestureRecognizer) {
UIView.animate(withDuration: 0.5, animations: {
self.myView.alpha = 0
}, completion: { (bool) in
self.myView.removeFromSuperview()
})
}
最后补充一下,我发现你的代码有点错误。 muView
错误,myView
正确。