zoukankan      html  css  js  c++  java
  • Swift

    Swift - CALayer的contents属性动画

    效果

    源码

    https://github.com/YouXianMing/Swift-Animations

    //
    //  LiveImageView.swift
    //  Swift-Animations
    //
    //  Created by YouXianMing on 16/8/17.
    //  Copyright © 2016年 YouXianMing. All rights reserved.
    //
    
    import UIKit
    
    // MARK: Public class : LiveImageView
    
    class LiveImageView: UIImageView {
        
        // MARK: Properties.
        
        /// Animation's duration.
        var duration : CFTimeInterval = 0.3
        
        // MARK: Methods.
        
        /**
         Set image with animation or not.
         
         - parameter newVal:   The new image.
         - parameter animated: Animated or not.
         */
        func setImage(newVal : UIImage, animated : Bool) {
            
            if animated == true {
                
                let animation       = CABasicAnimation(keyPath: "contents")
                animation.fromValue = image?.CGImage
                animation.toValue   = newVal.CGImage
                animation.duration  = duration
                
                pLayer.contents = image?.CGImage
                pLayer.addAnimation(animation, forKey: nil)
                
                image = newVal
                
            } else {
                
                image = newVal
            }
        }
        
        // MARK: Private value & func.
        
        private var pLayer : CALayer!
        
        override init(frame: CGRect) {
            
            super.init(frame: frame)
            pLayer = layer
        }
        
        required init?(coder aDecoder: NSCoder) {
            
            fatalError("init(coder:) has not been implemented")
        }
    }
    //
    //  LiveImageViewController.swift
    //  Swift-Animations
    //
    //  Created by YouXianMing on 16/8/17.
    //  Copyright © 2016年 YouXianMing. All rights reserved.
    //
    
    import UIKit
    
    class LiveImageViewController: NormalTitleViewController {
        
        var timer  : GCDTimer!  = GCDTimer(inQueue: GCDQueue.mainQueue)
        var count  : NSInteger! = 0
        var images : [UIImage]! = [UIImage]()
        
        override func setup() {
            
            super.setup()
            
            images = [UIImage]()
            images.append(UIImage(named: "pic_1")!)
            images.append(UIImage(named: "pic_2")!)
            images.append(UIImage(named: "pic_3")!)
            images.append(UIImage(named: "pic_4")!)
            
            let image                       = images[0]
            let liveImageView               = LiveImageView(frame: CGRectMake(0, 0, image.size.width, image.size.height))
            liveImageView.center            = (contentView?.middlePoint)!
            liveImageView.layer.borderWidth = 3
            liveImageView.layer.borderColor = UIColor.blackColor().CGColor
            liveImageView.duration          = 0.5
            contentView?.addSubview(liveImageView)
            
            weak var wself = self
            timer.event({
                
                let currentIndex = (wself?.count)! % (wself?.images.count)!
                wself?.count     = (wself?.count)! + 1
                
                liveImageView.setImage(wself!.images[currentIndex], animated: true)
                
                UIView.animateWithDuration(0.5, animations: {
                    
                    var tmpRect          = liveImageView.bounds
                    tmpRect.size         = (liveImageView.image?.size)!
                    liveImageView.bounds = tmpRect
                    liveImageView.center = (wself?.contentView?.middlePoint)!
                })
                
                }, timeIntervalWithSeconds: 1.0)
            
            timer.start()
        }
    }
  • 相关阅读:
    NC外部统一流程管理平台方案
    Activiti 多个并发子流程的应用
    基于Activiti的流程应用开发平台JSAAS-WF V5.3
    整合Acitiviti在线流程设计器(Activiti-Modeler 5.18.0)
    基于Spring Security 的JSaaS应用的权限管理
    微信分享功能开发
    ORACLE schedule job设置
    存储过程清理N天前数据
    oracle函数trunc的使用
    往前往后推时间(排除工作日和节假日)
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/5782165.html
Copyright © 2011-2022 走看看