@IBAction func toNagView(_ sender: UIButton) {
// let sb = UIStoryboard.init(name: "Main", bundle: nil)
// let nagView = sb.instantiateViewController(withIdentifier:"MyNagViewController")as! MyNagViewController
// nagView.view.backgroundColor = UIColor.init(white: 0, alpha: 0.5)
// self.definesPresentationContext = true
// nagView.modalPresentationCapturesStatusBarAppearance = true
// self.modalPresentationStyle = .currentContext
// self.navigationController?.pushViewController(nagView, animated: true)
// self.present(nagView, animated: true, completion: nil)
let objs = Bundle.main.loadNibNamed("NagView", owner: nil, options: nil)
nagView = objs?.first as! NagView
nagView?.frame = CGRect.init(x: 0, y: 20, width: self.view.frame.size.width, height: self.view.frame.size.height-20)
nagView?.backgroundColor = UIColor.init(white: 0, alpha: 0.5) //主View透明,子View不透明
let anima = CATransition.init()
anima.type = kCATransitionPush //设置动画的类型
anima.subtype = kCATransitionFromLeft //设置动画的方向
anima.duration = 0.5
nagView?.layer.add(anima, forKey: "showPushAnimation") //如果用self.view,中间会闪一下
let leftSwipe = UISwipeGestureRecognizer.init(target: self, action: #selector(nagViewShowAndMiss(gest:)))
leftSwipe.direction = .left
nagView?.addGestureRecognizer(leftSwipe)
self.view.addSubview(nagView!)
}
@objc func nagViewShowAndMiss(gest:UISwipeGestureRecognizer){
UIView.animate(withDuration: 0.5, animations: {
gest.view!.frame = CGRect.init(x: -gest.view!.frame.size.width, y: 20, width: gest.view!.frame.size.width, height: gest.view!.frame.size.height)
}) { (finished) in
gest.view?.removeFromSuperview()
}
}
nagView.backgroundColor = UIColor.init(white: 0, alpha: 0.5) //主View透明,子View不透明
Alpha 会传递,color不会传递
基本动画CABasicAnimation 完毕后会闪现本View anim.removedOnCompletion = false