RazzleDazzle动画

看到 RazzleDazzleg官方Demo,流畅的交互式动画瞬间让我大爱,所以花时间学习了下。
特别适合做版本介绍类动画

动画分类

1. 背景颜色类动画 BackgroundColorAnimation 用来实现偏移量和背景颜色改变的动画

代码和解释

let backgroundColorAnimation = BackgroundColorAnimation(view: scrollView)
//先创建一个背景动画对象
backgroundColorAnimation[0] = UIColor(red: 0.4, green: 0.4, blue: 0.4, alpha: 1)
backgroundColorAnimation[0.5] = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)
backgroundColorAnimation[0.99] = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)
backgroundColorAnimation[1] = UIColor(red: 0.14, green: 0.8, blue: 1, alpha: 1)
//表达式左边下标0代表的是引导页的偏移量,右边代表当前偏移量背景颜色
//注意下标的取值范围为0...numberOfPages() 这里越界不会奔溃只是无效果而已
animator.addAnimation(backgroundColorAnimation)
//最后再把这个动画添加到RD的动画管理对象中去,这个操作对每一种动画都一样,设置完毕后添加。
2. 缩放类动画 ScaleAnimation 用来对动画对象进行放大缩小

代码和解释

let starScaleAnimation = ScaleAnimation(view: star)
//star 缩放对象
starScaleAnimation.addKeyframe(0, value: 1, easing: EasingFunctionEaseInQuad)
starScaleAnimation[1] = 10
//0,1代表偏移量的占比,10为倍数
animator.addAnimation(starScaleAnimation)
let starHideAnimation = HideAnimation(view: star, hideAt: 1)
animator.addAnimation(starHideAnimation)

完整代码

import UIKit
import RazzleDazzle

class ViewController: AnimatedPagingScrollViewController {

    private let star = UIImageView(image: #imageLiteral(resourceName: "Star"))
    private let presents = UIImageView(image: #imageLiteral(resourceName: "IFTTTPresents"))
    private let razzle = UIImageView(image: #imageLiteral(resourceName: "Razzle"))
    private let dazzle = UIImageView(image: #imageLiteral(resourceName: "Dazzle"))
    
    private let musicStand = UIImageView(image: #imageLiteral(resourceName: "MusicStand"))
    private let musicNotes = UIImageView(image: #imageLiteral(resourceName: "MusicNotes"))
    private let plane = UIImageView(image: #imageLiteral(resourceName: "Plane"))
    private var planePathLayer = CAShapeLayer()
    private let planePathView = UIView()
    
    private let bigCloud = UIImageView(image: #imageLiteral(resourceName: "BigCloud"))
    private let littleCloud = UIImageView(image: #imageLiteral(resourceName: "LittleCloud"))
    private let sun = UIImageView(image: #imageLiteral(resourceName: "Sun"))
    private let iftttCloud = UIImageView(image: #imageLiteral(resourceName: "IFTTTCloud"))
    
    private let page2Text = UIImageView(image: #imageLiteral(resourceName: "Page2Text"))
    private let page3Text = UIImageView(image: #imageLiteral(resourceName: "Page3Text"))
    
    private var airplaneFlyingAnimation: PathPositionAnimation?
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        configViews()
        configAnimations()
    }
    
    override var prefersStatusBarHidden: Bool {
        return true
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        scaleAirplanePath(toSize: scrollView.frame.size)
    }
    
    override func numberOfPages() -> Int {
        return 4
    }
    
    private func configViews() {
        contentView.addSubview(presents)
        contentView.addSubview(star)
        contentView.addSubview(razzle)
        contentView.addSubview(dazzle)
        contentView.addSubview(planePathView)
        contentView.addSubview(musicStand)
        contentView.addSubview(musicNotes)
        contentView.addSubview(bigCloud)
        contentView.addSubview(littleCloud)
        contentView.addSubview(sun)
        contentView.addSubview(iftttCloud)
        contentView.addSubview(page2Text)
        contentView.addSubview(page3Text)
        animateCurrentFrame()
    }
    
    private func configAnimations () {
        configScrollView()
        
        configPresents()
        configStar()
        configRazzleDazzle()
        
        configMusicStand()
        configMusicNotes()
        
        configPage2Text()
        configBigCloud()
        configLittleCloud()
        configAirplane()
        
        configSun()
        configPage3Text()
        configIftttCloud()
    }
    
    func configScrollView() {
        let backgroundColorAnimation = BackgroundColorAnimation(view: scrollView)
        backgroundColorAnimation[0] = UIColor(red: 0.4, green: 0.4, blue: 0.4, alpha: 1)
        backgroundColorAnimation[0.5] = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)
        backgroundColorAnimation[0.99] = UIColor(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)
        backgroundColorAnimation[1] = UIColor(red: 0.14, green: 0.8, blue: 1, alpha: 1)
        animator.addAnimation(backgroundColorAnimation)
    }
    
    func configPresents() {
        NSLayoutConstraint(item: presents, attribute: .top, relatedBy: .equal, toItem: contentView, attribute: .top, multiplier: 1, constant: 20).isActive = true
        keepView(presents, onPages: [0, 1])
        
        let presentsHideAnimation = HideAnimation(view: presents, hideAt: 1)
        animator.addAnimation(presentsHideAnimation)
    }
    
    func configStar() {
        let width = NSLayoutConstraint(item: star, attribute: .width, relatedBy: .lessThanOrEqual, toItem: scrollView, attribute: .width, multiplier: 1.3, constant: 0)
        
        let height = NSLayoutConstraint(item: star, attribute: .height, relatedBy: .lessThanOrEqual, toItem: scrollView, attribute: .height, multiplier: 1, constant: 0)
        
        let top = NSLayoutConstraint(item: star, attribute: .top, relatedBy: .greaterThanOrEqual, toItem: scrollView, attribute: .top, multiplier: 1, constant: 30)
        
        let aspect = NSLayoutConstraint(item: star, attribute: .height, relatedBy: .equal, toItem: star, attribute: .width, multiplier: 1, constant: 0)
        
        let centerY = NSLayoutConstraint(item: star, attribute: .centerY, relatedBy: .equal, toItem: scrollView, attribute: .centerY, multiplier: 1, constant: 0)
        
        NSLayoutConstraint.activate([width, height, top, aspect, centerY])
        keepView(star, onPages: [0, 1])
        
        let starScaleAnimation = ScaleAnimation(view: star)
        starScaleAnimation.addKeyframe(0, value: 1, easing: EasingFunctionEaseInQuad)
        starScaleAnimation[1] = 10
        animator.addAnimation(starScaleAnimation)
        
        let starHideAnimation = HideAnimation(view: star, hideAt: 1)
        animator.addAnimation(starHideAnimation)
    }
    
    func configRazzleDazzle() {
        let razzleWidth = NSLayoutConstraint(item: razzle, attribute: .width, relatedBy: .equal, toItem: star, attribute: .width, multiplier: 0.6, constant: 0)
        let razzleHeight = NSLayoutConstraint(item: razzle, attribute: .height, relatedBy: .equal, toItem: razzle, attribute: .width, multiplier: 218.0 / 576, constant: 0)
        let dazzleWidth = NSLayoutConstraint(item: dazzle, attribute: .width, relatedBy: .equal, toItem: star, attribute: .width, multiplier: 0.6, constant: 0)
        let dazzleHeight = NSLayoutConstraint(item: dazzle, attribute: .height, relatedBy: .equal, toItem: dazzle, attribute: .width, multiplier: 386 / 588.0, constant: 0)
        NSLayoutConstraint.activate([razzleWidth, razzleHeight, dazzleWidth, dazzleHeight])
        
        let razzleVertical = NSLayoutConstraint(item: razzle, attribute: .centerY, relatedBy: .equal, toItem: star, attribute: .centerY, multiplier: 1, constant: 0)
        let dazzleVertical = NSLayoutConstraint(item: dazzle, attribute: .centerY, relatedBy: .equal, toItem: star, attribute: .centerY, multiplier: 1, constant: 0)
        NSLayoutConstraint.activate([razzleVertical, dazzleVertical])
        
        let razzleVerticalAnimation = ConstraintConstantAnimation(superview: scrollView, constraint: razzleVertical)
        razzleVerticalAnimation[0] = -30
        razzleVerticalAnimation[1] = -200
        animator.addAnimation(razzleVerticalAnimation)
        
        let dazzleVerticalAnimation = ConstraintConstantAnimation(superview: scrollView, constraint: dazzleVertical)
        dazzleVerticalAnimation[0] = 80
        dazzleVerticalAnimation[1] = 260
        animator.addAnimation(dazzleVerticalAnimation)
        
        keepView(razzle, onPage: 0)
        keepView(dazzle, onPage: 0)
        
        let razzleRotationAnimation = RotationAnimation(view: razzle)
        razzleRotationAnimation[0] = 0
        razzleRotationAnimation[1] = 100
        animator.addAnimation(razzleRotationAnimation)
        
        let dazzleRotationAnimation = RotationAnimation(view: dazzle)
        dazzleRotationAnimation[0] = 0
        dazzleRotationAnimation[1] = 100
        animator.addAnimation(dazzleRotationAnimation)
    }
    
    private func configMusicStand() {
        let verticalConstraint = NSLayoutConstraint(item: musicStand, attribute: .bottom, relatedBy: .greaterThanOrEqual, toItem: scrollView, attribute: .bottom, multiplier: 1, constant: 0)
        
        let topConstraint = NSLayoutConstraint(item: musicStand, attribute: .top, relatedBy: .greaterThanOrEqual, toItem: scrollView, attribute: .top, multiplier: 1, constant: 40)
        
        let widthConstraint = NSLayoutConstraint(item: musicStand, attribute: .width, relatedBy: .lessThanOrEqual, toItem: scrollView, attribute: .width, multiplier: 1, constant: 0)
        
        let aspectConstraint = NSLayoutConstraint(item: musicStand, attribute: .height, relatedBy: .equal, toItem: musicStand, attribute: .width, multiplier: 1184 / 750.0, constant: 0)
        
        NSLayoutConstraint.activate([verticalConstraint, topConstraint, widthConstraint, aspectConstraint])
        keepView(musicStand, onPages: [1, 2], withAttribute: .right)
        
        let verticalAnimation = ConstraintMultiplierAnimation(superview: scrollView, constraint: verticalConstraint, attribute: .height, referenceView: contentView)
        verticalAnimation.addKeyframe(1, value: 0, easing: EasingFunctionEaseOutCubic)
        verticalAnimation[2] = 1
        animator.addAnimation(verticalAnimation)
    }
    
    private func configMusicNotes() {
        let width = NSLayoutConstraint(item: musicNotes, attribute: .width, relatedBy: .equal, toItem: musicStand, attribute: .width, multiplier: 1, constant: 0)
        
        let height = NSLayoutConstraint(item: musicNotes, attribute: .height, relatedBy: .equal, toItem: musicStand, attribute: .height, multiplier: 1, constant: 0)
        
        let centerY = NSLayoutConstraint(item: musicNotes, attribute: .centerY, relatedBy: .equal, toItem: musicStand, attribute: .centerY, multiplier: 1, constant: 0)
        
        NSLayoutConstraint.activate([width, height, centerY])
        
        keepView(musicNotes, onPages: [2, 1, 2], atTimes: [0.5, 1, 2], withAttribute: .right)
        
    }
    
    private func configPage2Text() {
        let centerY = NSLayoutConstraint(item: page2Text, attribute: .centerY, relatedBy: .equal, toItem: scrollView, attribute: .centerY, multiplier: 1, constant: -30)
        
        NSLayoutConstraint.activate([centerY])
        
        keepView(page2Text, onPage: 2)
    }
    
    private func configBigCloud() {
        let vertical = NSLayoutConstraint(item: bigCloud, attribute: .centerY, relatedBy: .equal, toItem: scrollView, attribute: .top, multiplier: 1, constant: 0)
        
        let width = NSLayoutConstraint(item: bigCloud, attribute: .width, relatedBy: .lessThanOrEqual, toItem: scrollView, attribute: .width, multiplier: 0.78, constant: 0)
        
        let height = NSLayoutConstraint(item: bigCloud, attribute: .height, relatedBy: .lessThanOrEqual, toItem: scrollView, attribute: .height, multiplier: 0.2, constant: 0)
        
        let aspect = NSLayoutConstraint(item: bigCloud, attribute: .height, relatedBy: .equal, toItem: bigCloud, attribute: .width, multiplier: 0.45, constant: 0)
        
        NSLayoutConstraint.activate([vertical, width, height, aspect])
        
        keepView(bigCloud, onPages: [1.35, 2.35, 1.8], atTimes: [1, 2, 3])
        
        let verticalAnimation = ConstraintMultiplierAnimation(superview: scrollView, constraint: vertical, attribute: .height, referenceView: scrollView)
        verticalAnimation[1] = -0.2
        verticalAnimation[2] = 0.2
        animator.addAnimation(verticalAnimation)
    }
    
    private func configLittleCloud() {
        let top = NSLayoutConstraint(item: littleCloud, attribute: .bottom, relatedBy: .equal, toItem: bigCloud, attribute: .top, multiplier: 1, constant: 0)
        
        NSLayoutConstraint.activate([top])
        
        keepView(littleCloud, onPages: [0.75, 1.75], atTimes: [1, 2])
    }
    
    private func configSun() {
        let top = NSLayoutConstraint(item: sun, attribute: .centerY, relatedBy: .equal, toItem: scrollView, attribute: .top, multiplier: 1, constant: 0)
        
        NSLayoutConstraint.activate([top])
        
        keepView(sun, onPages: [2.8, 2.6], atTimes: [2.5, 3])
        
        let verticalAnimation = ConstraintConstantAnimation(superview: scrollView, constraint: top)
        verticalAnimation[2] = -200
        verticalAnimation[3] = 20
        animator.addAnimation(verticalAnimation)
    }
    
    private func configIftttCloud() {
        let centerY = NSLayoutConstraint(item: iftttCloud, attribute: .centerY, relatedBy: .equal, toItem: scrollView, attribute: .centerY, multiplier: 1.5, constant: 0)
        
        NSLayoutConstraint.activate([centerY])
        
        keepView(iftttCloud, onPages: [3.5, 3], atTimes: [2, 3], withAttribute: .centerX)
    }
    
    private func configPage3Text() {
        let centerY = NSLayoutConstraint(item: page3Text, attribute: .centerY, relatedBy: .equal, toItem: scrollView, attribute: .centerY, multiplier: 1, constant: 0)
        
        NSLayoutConstraint.activate([centerY])
        
        keepView(page3Text, onPage: 3)
    }
    
    private func configAirplane() {
        planePathLayer = airplanePathLayer()
        planePathView.layer.addSublayer(planePathLayer)
        planePathView.addSubview(plane)
        planePathView.translatesAutoresizingMaskIntoConstraints = false
        plane.translatesAutoresizingMaskIntoConstraints = false
        let planeBottom = NSLayoutConstraint(item: plane, attribute: .bottom, relatedBy: .equal, toItem: planePathView, attribute: .centerY, multiplier: 1, constant: 0)
        
        let planeRight = NSLayoutConstraint(item: plane, attribute: .right, relatedBy: .equal, toItem: planePathView, attribute: .centerX, multiplier: 1, constant: 0)
        
        NSLayoutConstraint.activate([planeBottom, planeRight])
        
        
        let planPathBottom = NSLayoutConstraint(item: planePathView, attribute: .bottom, relatedBy: .equal, toItem: scrollView, attribute: .bottom, multiplier: 1, constant: 40)
        
        let planePathWidth = NSLayoutConstraint(item: planePathView, attribute: .width, relatedBy: .equal, toItem: plane, attribute: .width, multiplier: 1, constant: 0)
        
        let planePathHeight = NSLayoutConstraint(item: planePathView, attribute: .height, relatedBy: .equal, toItem: plane, attribute: .height, multiplier: 1, constant: 0)
        
        NSLayoutConstraint.activate([planePathWidth, planePathHeight, planPathBottom])
        
        keepView(planePathView, onPages: [1.5, 2.5], atTimes: [1, 2], withAttribute: .left)
        
        let planeFlyingAnimation = PathPositionAnimation(view: plane, path: planePathLayer.path)
        planeFlyingAnimation[1] = 0
        planeFlyingAnimation[2] = 1
        animator.addAnimation(planeFlyingAnimation)
        airplaneFlyingAnimation = planeFlyingAnimation
        
        let planePathAnimation = LayerStrokeEndAnimation(layer: planePathLayer)
        planePathAnimation[1] = 0
        planePathAnimation[2] = 1
        animator.addAnimation(planePathAnimation)
        
        let planeAlphaAnimation = AlphaAnimation(view: planePathView)
        planeAlphaAnimation[1.06] = 0
        planeAlphaAnimation[1.08] = 1
        planeAlphaAnimation[2.5] = 1
        planeAlphaAnimation[3] = 0
        animator.addAnimation(planeAlphaAnimation)
    }
    
    private func airplanePath() -> CGPath {
        let airplanePath = UIBezierPath()
        airplanePath.move(to: CGPoint(x: 120, y: 20))
        airplanePath.addCurve(to: CGPoint(x: 40, y: -130), controlPoint1: CGPoint(x: 120, y: 20), controlPoint2: CGPoint(x: 140, y: -50))
        airplanePath.addCurve(to: CGPoint(x: 30, y: -430), controlPoint1: CGPoint(x: -60, y: -210), controlPoint2: CGPoint(x: -320, y: -430))
        airplanePath.addCurve(to: CGPoint(x: -210, y: -190), controlPoint1: CGPoint(x: 320, y: -430), controlPoint2: CGPoint(x: 130, y: -190))
        return airplanePath.cgPath
    }
    
    private func airplanePathLayer() -> CAShapeLayer {
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = airplanePath()
        shapeLayer.strokeColor = UIColor.white.cgColor
        shapeLayer.lineDashPattern = [20, 20]
        shapeLayer.lineWidth = 4
        shapeLayer.miterLimit = 4
        shapeLayer.fillColor = nil
        shapeLayer.fillRule = kCAFillRuleEvenOdd
        return shapeLayer
    }
    
    private func scaleAirplanePath(toSize pageSize: CGSize) {
        let scaleSize = CGSize(width: pageSize.width / 375.0, height: pageSize.height / 667.0)
        
        var scaleTransform = CGAffineTransform(scaleX: scaleSize.width, y: scaleSize.height)
        let scaledPath = airplanePath().copy(using: &scaleTransform)
        planePathLayer.path = scaledPath
        
        if let planeAnimation = airplaneFlyingAnimation {
            planeAnimation.path = scaledPath
        }
    }
    
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,214评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,307评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,543评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,221评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,224评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,007评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,313评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,956评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,441评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,925评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,018评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,685评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,234评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,240评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,464评论 1 261
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,467评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,762评论 2 345

推荐阅读更多精彩内容

  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥ios动画全貌。在这里你可以看...
    每天刷两次牙阅读 8,465评论 6 30
  • 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥iOS动画全貌。在这里你可以看...
    F麦子阅读 5,094评论 5 13
  • 前言 本文只要描述了iOS中的Core Animation(核心动画:隐式动画、显示动画)、贝塞尔曲线、UIVie...
    GitHubPorter阅读 3,600评论 7 11
  • 显式动画 显式动画,它能够对一些属性做指定的自定义动画,或者创建非线性动画,比如沿着任意一条曲线移动。 属性动画 ...
    清风沐沐阅读 1,921评论 1 5
  • Animation Animation类是所有动画(scale、alpha、translate、rotate)的基...
    四月一号阅读 1,900评论 0 10