Swift Uiview 背景动画不工作

Swift Uiview background animation not working

我正在制作这个视图,其中底部sheet 会弹出,背景视图会淡入,当我按下按钮关闭背景时,背景会淡出。

我实际上已经完成了一半,当底部sheet弹出时,它是从底部到顶部的动画,背景颜色会淡入,但当我想关闭底部时情况不同sheet,背景动画不是淡出动画而是从上到下动画,有人知道我为什么会遇到这个错误吗?

这是我的代码

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)
}

我仍然不知道为什么会这样,我猜是因为关闭动画和淡出动画不会做任何事情,因为关闭动画首先被调用...但它仍然没有任何意义,因为那时我尝试在底部 sheet 弹出它的工作原理,现在我尝试使用相同的方法但它不起作用.....有什么想法吗?谢谢:)

您需要使用完成处理程序来像这样隐藏视图

 UIView.animateKeyframes(withDuration: 0.4, delay: 0.35, options: .curveEaseOut, animations: {
       self.topView.alpha = 0.0
    }) { _ in
      isShown = false
    }

问题出在你按下的方法上 -

您正在通过提供 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