答案 0 :(得分:0)
创建一个对象并出售自定义present
动画:
class TheVendor: NSObject, UIViewControllerTransitioningDelegate {
func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return TheAnimator()
}
}
然后创建动画师:
class TheAnimator: NSObject, UIViewControllerAnimatedTransitioning {
func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
// return a duration
}
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
// return the custom animation
}
}
然后当你present
时,进行正确的绑定:
presentedViewController.transitioningDelegate = TheVendor()
presentedViewController.modalPresentationStyle = .custom
答案 1 :(得分:0)
以下是您问题的解决方案:
class ViewController: UIViewController {
// Your image view what to split
@IBOutlet weak var imgView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
// Start split animation
self.splitAnimation(imgView: self.imgView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func splitAnimation(imgView: UIImageView){
let topHalf = imgView.image?.topHalf
let bottomHalf = imgView.image?.bottomHalf
let imgViewTop = UIImageView(frame: CGRect(x: 0, y: 0, width: imgView.frame.size.width, height: imgView.frame.size.height/2.0))
imgViewTop.image = topHalf
let imgViewBottom = UIImageView(frame: CGRect(x: 0, y: imgView.frame.size.height/2.0, width: imgView.frame.size.width, height: imgView.frame.size.height/2.0))
imgViewBottom.image = bottomHalf
imgView.addSubview(imgViewTop)
imgView.addSubview(imgViewBottom)
imgView.image = nil
UIView.animate(withDuration: 1.5, delay: 1.0, options: .curveLinear, animations: {
imgViewTop.frame = CGRect(x: 0, y: -imgView.frame.size.height/2.0, width: imgView.frame.size.width, height: imgView.frame.size.height/2.0)
imgViewBottom.frame = CGRect(x: 0, y: imgView.frame.size.height, width: imgView.frame.size.width, height: imgView.frame.size.height/2.0)
}, completion: { (complete) in
imgViewTop.removeFromSuperview()
imgViewBottom.removeFromSuperview()
imgView.removeFromSuperview()
})
}
}
现在将此扩展添加到您的班级:
extension UIImage {
var topHalf: UIImage? {
guard let cgImage = cgImage, let image = cgImage.cropping(to: CGRect(origin: .zero, size: CGSize(width: size.width, height: size.height/2))) else { return nil }
return UIImage(cgImage: image, scale: 1, orientation: imageOrientation)
}
var bottomHalf: UIImage? {
guard let cgImage = cgImage, let image = cgImage.cropping(to: CGRect(origin: CGPoint(x: 0, y: CGFloat(Int(size.height)-Int(size.height/2))), size: CGSize(width: size.width, height: CGFloat(Int(size.height) - Int(size.height/2))))) else { return nil }
return UIImage(cgImage: image)
}
}
根据您自己管理动画的持续时间
在Xcode 9.3和Swift 4.1中测试的代码