美文网首页Swift
swift-CATransiton的简单使用

swift-CATransiton的简单使用

作者: Coder东 | 来源:发表于2017-03-30 15:59 被阅读147次
    //
    //  ViewController.swift
    //  Swift-使用CATranstion动画
    //
    //  Created by 品德信息 on 2017/2/24.
    //  Copyright © 2017年 品德信息. All rights reserved.
    //
    
    import UIKit
    
    class ViewController: UIViewController {
    
    //设置入场动画
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let rect = CGRect(x:0,y:64,width:320,height:200)
        let imageView = UIImageView(frame:rect)
        
        let image = UIImage(named: "girl")
        imageView.image = image
      // self.view.addSubview(imageView)
        let animation = CATransition()
        animation.duration = 2
        animation.timingFunction = CAMediaTimingFunction(name:kCAMediaTimingFunctionEaseOut)
        animation.type = kCATransitionPush
        
        imageView.layer.add(animation,forKey:"Push")
        
        
        //UIView动画块
        let rect1 = CGRect(x:50,y:50,width:220,height:200)
        let imageView1 = UIImageView(frame:rect1)
        let image1 = UIImage(named:"one")
        imageView1.image = image1
        imageView1.tag = 1
        // self.view.addSubview(imageView1)
        let button = UIButton(type:UIButtonType.custom)
        button.frame = CGRect(x:50,y:400,width:220,height:44)
        button.backgroundColor = UIColor.lightGray
        button.setTitle("Tap", for: UIControlState())
        button.addTarget(self, action: #selector(ViewController.playAnimation), for: UIControlEvents.touchUpInside)
          //        self.view.addSubview(button)
        //UIView的卷曲动画
        let rect2 = CGRect(x:50,y:50,width:220,height:200)
        let imageView2 = UIImageView(frame:rect2)
        let image2 = UIImage(named:"girl")
        imageView2.image = image2
        //    self.view.addSubview(imageView2)
        imageView2.tag = 2
        let button2 = UIButton(type:UIButtonType.system)
        button2.frame = CGRect(x:50,y:400,width:200,height:44)
        button2.backgroundColor = UIColor.lightGray
        button2.setTitle("Tap", for: UIControlState())
        button2.addTarget(self, action: #selector(ViewController.playAnimation), for: UIControlEvents.touchUpInside)
        self.view.addSubview(button2)
        
        //UIView 专场动画的制作 并 检测动画结束
    
        self.viewAnimation3()
    }
    
    func viewAnimation3()  {
        
        let rect = CGRect(x:50,y:64,width:200,height:200)
        let imageView = UIImageView(frame:rect)
        let image = UIImage(named:"girl")
        imageView.image = image
        self.view.addSubview(imageView)
        imageView.tag = 3
        
        let button = UIButton(type:UIButtonType.system)
        button.frame = CGRect(x:100,y:400,width:200,height:44)
        button.backgroundColor = UIColor.lightGray
        button.setTitle("Tap", for: UIControlState())
        button.addTarget(self, action: #selector(ViewController.playAnimation), for: UIControlEvents.touchUpInside)
        self.view.addSubview(button)
    }
    func playAnimation() {
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationCurve(UIViewAnimationCurve.easeOut)
        UIView.setAnimationDuration(5)
        UIView.setAnimationBeginsFromCurrentState(true)
        
        let view = self.view.viewWithTag(3)
        UIView.setAnimationTransition(UIViewAnimationTransition.flipFromLeft, for: view!, cache: true)
        view?.frame = CGRect(x:50,y:80,width:0,height:0)
        UIView.setAnimationDelegate(self)
        UIView.setAnimationDidStop(#selector(self.animationStop))
        
        UIView.commitAnimations()
    }
    
    func animationStop() {
        print("Animation stop")
        self.view.viewWithTag(3)?.removeFromSuperview()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    }
    

    相关文章

      网友评论

        本文标题:swift-CATransiton的简单使用

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