美文网首页iOS开发
view 翻页动画

view 翻页动画

作者: _浅墨_ | 来源:发表于2022-04-22 23:04 被阅读0次
    //
    //  ViewTransitionViewControllerViewController.swift
    //  MFEfficiencyControl
    //
    //  Created by changyou on 2022/4/22.
    //
    //  view 翻页动画:微信读书的仿真动画就是这样实现的?
    
    import UIKit
    
    class ViewTransitionViewControllerViewController: UIViewController {
    
        
        lazy var containerView: UIView = {
            let containerView = UIView(frame: CGRect(x: 30,y: 100,width: 200,height: 300))
            view.addSubview(containerView)
            containerView.backgroundColor = .gray
            return containerView
        }()
        
        lazy var redView: UIView = {
            let redView = UIView(frame: CGRect(x: 0,y: 0,width: 200,height: 300))
            view.addSubview(redView)
            redView.backgroundColor = .gray
            return redView
        }()
        
        lazy var orangeView: UIView = {
            let orangeView = UIView(frame: CGRect(x: 0,y: 0,width: 200,height: 300))
            view.addSubview(orangeView)
            orangeView.backgroundColor = .gray
            return orangeView
        }()
        
        
        enum ViewType {
            case orange
            case red
        }
        
        var currentType: ViewType = .orange
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            containerView.addSubview(redView)
            redView.frame.size = containerView.frame.size
            redView.frame.origin = CGPoint(x: 0, y: 0)
            
            containerView.addSubview(orangeView)
            orangeView.frame.size = containerView.frame.size
            orangeView.frame.origin = CGPoint(x: 0, y: 0)
            
        }
        
    
        @IBAction func transitionView(_ sender:Any) {
            
            switch currentType {
                
            case .orange:
                
                UIView.transition(with: containerView, duration: 1.0,options: [.transitionCurlUp],animations: {
                    
                    self.orangeView.removeFromSuperview()
                    
                    self.containerView.addSubview(self.redView)
                    
                }) { (success) in
                    
                    self.currentType = .red
                    
                }
            case .red:
                
                UIView.transition(with: containerView, duration: 1.0,options: [.transitionCurlUp],animations: {
                    
                    self.redView.removeFromSuperview()
                    
                    self.containerView.addSubview(self.orangeView)
                    
                }) { (success) in
                    
                    self.currentType = .orange
                    
                }
                
            }
        }
    
    }
    
    

    相关文章

      网友评论

        本文标题:view 翻页动画

        本文链接:https://www.haomeiwen.com/subject/sxeoertx.html