-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from dabing1022/master
add ball rotate chase animation
- Loading branch information
Showing
4 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
NVActivityIndicatorView/Animations/NVActivityIndicatorAnimationBallRotateChase.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// NVActivityIndicatorAnimationBallRotateChase.swift | ||
// NVActivityIndicatorViewDemo | ||
// | ||
// Created by ChildhoodAndy on 15/12/7. | ||
// Copyright © 2015年 Nguyen Vinh. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
class NVActivityIndicatorAnimationBallRotateChase: NVActivityIndicatorAnimationDelegate { | ||
|
||
func setUpAnimationInLayer(layer: CALayer, size: CGSize, color: UIColor) { | ||
let circleSize = size.width / 5; | ||
|
||
// Draw circles | ||
for var i = 0; i < 5; i++ { | ||
let factor = Float(i) * 1.0 / 5 | ||
let circle = NVActivityIndicatorShape.Circle.createLayerWith(size: CGSize(width: circleSize, height: circleSize), color: color) | ||
|
||
let animation = rotateAnimation(factor, x: layer.bounds.size.width / 2, y: layer.bounds.size.height / 2, size: size) | ||
circle.addAnimation(animation, forKey: "animation") | ||
layer.addSublayer(circle) | ||
} | ||
} | ||
|
||
func rotateAnimation(rate: Float, x: CGFloat, y: CGFloat, size: CGSize) -> CAAnimationGroup { | ||
let duration: CFTimeInterval = 1.5 | ||
let fromScale = 1 - rate | ||
let toScale = 0.2 + rate | ||
let timeFunc = CAMediaTimingFunction(controlPoints: 0.5, 0.15 + rate, 0.25, 1.0) | ||
|
||
// Scale animation | ||
let scaleAnimation = CABasicAnimation(keyPath: "transform.scale") | ||
scaleAnimation.duration = duration | ||
scaleAnimation.repeatCount = HUGE | ||
scaleAnimation.fromValue = fromScale | ||
scaleAnimation.toValue = toScale | ||
|
||
// Position animation | ||
let positionAnimation = CAKeyframeAnimation(keyPath: "position") | ||
positionAnimation.duration = duration | ||
positionAnimation.repeatCount = HUGE | ||
positionAnimation.path = UIBezierPath(arcCenter: CGPoint(x: x, y: y), radius: size.width / 2, startAngle: 3 * CGFloat(M_PI) * 0.5, endAngle: 3 * CGFloat(M_PI) * 0.5 + 2 * CGFloat(M_PI), clockwise: true).CGPath | ||
|
||
// Aniamtion | ||
let animation = CAAnimationGroup() | ||
animation.animations = [scaleAnimation, positionAnimation] | ||
animation.timingFunction = timeFunc | ||
animation.duration = duration | ||
animation.repeatCount = HUGE | ||
animation.removedOnCompletion = false | ||
|
||
return animation | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters