IOS多媒体-简单动画

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21153627/article/details/84142903
import UIKit
class ViewController: UIViewController  {
    override func viewDidLoad() {
        super.viewDidLoad();
       test1()
    }
    
    func test1() {
        let imageVIew = UIImageView(frame: CGRect(x: 50, y: 50, width: 220, height: 320))
        imageVIew.image = UIImage(named: "Pic4")
        imageVIew.tag = 1
        self.view.addSubview(imageVIew)
        
        let button = UIButton(type: UIButtonType.system)
        button.frame = CGRect(x: 50, y: 400, width: 220, height: 44 )
        button.backgroundColor = UIColor.orange
        button.setTitle("Tap it.", for: UIControlState())
        button.addTarget(self, action: #selector(ViewController.playAnimation), for: UIControlEvents.touchUpInside)
        self.view.addSubview(button)
    }
    @objc func playAnimation(){
        //反转动画的制作
        //发出开始动画的请求 提交请求期间可以定义动画的展开方式
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationCurve(.easeOut)//淡入淡出
        UIView.setAnimationDuration(5)//时间
        UIView.setAnimationBeginsFromCurrentState(true)//动画从当前状态开始播放
        let view = self.view.viewWithTag(1)
        //视图在进行翻转的同时移动到目标位置并缩小至不可见
        view?.frame = CGRect(x: 50, y: 50, width: 0, height: 0 )
        //是指动画的代理起为当前根视图
        UIView.setAnimationDelegate(self)
        //设置动画结束执行的方法
        UIView.setAnimationDidStop(#selector(ViewController.animationStop))
        //设置反转动画
        UIView.setAnimationTransition(.flipFromLeft, for: view!, cache: true)
        UIView.commitAnimations()
        
        
        //卷曲动画
//        UIView.beginAnimations(nil, context: nil)
//        UIView.setAnimationCurve(.easeOut)//淡入淡出
//        UIView.setAnimationDuration(5)//时间
//        UIView.setAnimationBeginsFromCurrentState(true)
//        let view = self.view.viewWithTag(1)
//        UIView.setAnimationTransition(.curlUp, for: view!, cache: true)
//        UIView.commitAnimations()
    }
   
    @objc func animationStop(){
        //动画结束后的操作
        //将视图从父视图移除
        self.view.viewWithTag(1)?.removeFromSuperview()
    }
   
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

猜你喜欢

转载自blog.csdn.net/qq_21153627/article/details/84142903