• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Swift-CALayer的contents属性动画

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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()
    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Swift:使用CAShapeLayer打造一个ProgresssBar发布时间:2022-07-14
下一篇:
[Swift]LeetCode684.冗余连接|RedundantConnection发布时间:2022-07-14
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap