[英]Swift Uiview background animation not working
我正在制作這個視圖,底部將彈出,背景視圖將淡入,當我按下按鈕關閉時,背景將淡出。
我實際上已經完成了當底部表彈出時它從底部到頂部 animation 並且背景顏色會淡入的一半部分,但是當我想關閉底部表時,它的情況不同,背景 animation 不這樣做會淡出 Z6F1C25ED1523992BZBBF 和而是從上到下 animation,有人知道我為什么會遇到這個錯誤嗎?
這是我的代碼
let fullView: CGFloat = 0
let screenSize: CGRect = UIScreen.main.bounds
@IBOutlet weak var Text: UILabel!
@IBOutlet weak var topView: UIView!
@IBOutlet weak var vieww: UIView!
@IBOutlet weak var botView: UIView!
@IBOutlet weak var Button: UIButton!
@objc func pressed(sender : Any) {
var closeView = screenSize.height
print(closeView)
UIView.animate(withDuration: 0.4) { [weak self] in
let frame = self?.view.frame
self?.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
self?.view.frame = CGRect(x: 0, y: closeView, width: frame!.width, height: frame!.height)
}
UIView.animate(withDuration: 0.4, delay: 0.35, options: .curveEaseOut, animations: {
self.topView.alpha = 0.0
}, completion: nil)
isShown = false
}
override func viewDidLoad() {
super.viewDidLoad()
button()
self.topView.alpha = 0.0
vieww.layer.cornerRadius = 20
vieww.layer.borderWidth = 0.5
vieww.layer.borderColor = UIColor(red:222/255, green:225/255, blue:227/255, alpha: 1).cgColor
vieww.clipsToBounds = true
botView.layer.masksToBounds = false
botView.layer.shadowColor = UIColor.black.cgColor
botView.layer.shadowOpacity = 0.14
botView.layer.shadowOffset = CGSize(width: 0, height: 0)
botView.layer.shadowRadius = 2.7
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//when the animation is popping up
UIView.animate(withDuration: 0.4) { [weak self] in
let frame = self?.view.frame
// let yComponent = UIScreen.main.bounds.height - 896
let yComponent = self?.fullView
self?.view.frame = CGRect(x: 0, y: yComponent!, width: frame!.width, height: frame!.height)
}
UIView.animate(withDuration: 0.4, delay: 0.35, options: .curveEaseOut, animations: {
self.topView.alpha = 0.5
}, completion: nil)
}
func button() {
Button.addTarget(self, action: #selector(pressed), for: .touchUpInside)
}
我仍然不知道為什么會發生這種情況,我猜測它是因為解雇 animation 和淡出 animation 不會做任何事情,因為解雇 animation 被稱為我首先嘗試彈出......但它仍然沒有任何意義,因為然后它仍然沒有意義底頁它的工作,現在我嘗試使用相同的方法,它不工作.....知道嗎? 謝謝:)
問題在於您的按下方法 -
您正在通過提供 y=closeView(這是屏幕高度的大小)來更改主視圖的框架。 隨着您的整個主視圖下降,您的彈出窗口正在發生變化。
topView 也隨着主視圖向下移動
@objc func pressed(sender : Any) {
let closeView = screenSize.height
print(closeView)
UIView.animate(withDuration: 0.4) { [weak self] in
let frame = self?.view.frame
self?.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
self?.view.frame = CGRect(x: 0, y: closeView, width: frame!.width, height: frame!.height) //Wrong
}
UIView.animate(withDuration: 0.4, delay: 0.35, options: .curveEaseOut, animations: {
self.topView.alpha = 0.0
}, completion: nil)
}
解決方案 -
self?.<bottomView>.frame = CGRect(x: 0, y: closeView, width: frame!.width, height: frame!.height) //Change bottom view with the bottom subview object which you want to push at bottom
您需要使用完成處理程序來隱藏這樣的視圖
UIView.animateKeyframes(withDuration: 0.4, delay: 0.35, options: .curveEaseOut, animations: {
self.topView.alpha = 0.0
}) { _ in
isShown = false
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.