Skip to content

Commit

Permalink
Merge pull request #20 from dabing1022/master
Browse files Browse the repository at this point in the history
add ball rotate chase animation
  • Loading branch information
ninjaprox committed Dec 10, 2015
2 parents d008493 + 089e3f1 commit 3259c34
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
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
}
}

3 changes: 3 additions & 0 deletions NVActivityIndicatorView/NVActivityIndicatorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public enum NVActivityIndicatorType {
case Pacman
case BallGridBeat
case SemiCircleSpin
case BallRotateChase

private func animation() -> NVActivityIndicatorAnimationDelegate {
switch self {
Expand Down Expand Up @@ -99,6 +100,8 @@ public enum NVActivityIndicatorType {
return NVActivityIndicatorAnimationBallGridBeat()
case .SemiCircleSpin:
return NVActivityIndicatorAnimationSemiCircleSpin()
case .BallRotateChase:
return NVActivityIndicatorAnimationBallRotateChase()
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions NVActivityIndicatorViewDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
90A879C61BA557B1001C8989 /* NVActivityIndicatorAnimationSemiCircleSpin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 90A879831BA55798001C8989 /* NVActivityIndicatorAnimationSemiCircleSpin.swift */; };
90A879C71BA557B1001C8989 /* NVActivityIndicatorAnimationSquareSpin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 90A879841BA55798001C8989 /* NVActivityIndicatorAnimationSquareSpin.swift */; };
90A879C81BA557B1001C8989 /* NVActivityIndicatorAnimationTriangleSkewSpin.swift in Sources */ = {isa = PBXBuildFile; fileRef = 90A879851BA55798001C8989 /* NVActivityIndicatorAnimationTriangleSkewSpin.swift */; };
BD1452341C15810200B952C8 /* NVActivityIndicatorAnimationBallRotateChase.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD1452331C15810200B952C8 /* NVActivityIndicatorAnimationBallRotateChase.swift */; };
BD1452351C1586F200B952C8 /* NVActivityIndicatorAnimationBallRotateChase.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD1452331C15810200B952C8 /* NVActivityIndicatorAnimationBallRotateChase.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -136,6 +138,7 @@
90A879861BA55798001C8989 /* NVActivityIndicatorAnimationDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NVActivityIndicatorAnimationDelegate.swift; sourceTree = "<group>"; };
90A879871BA55798001C8989 /* NVActivityIndicatorShape.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NVActivityIndicatorShape.swift; sourceTree = "<group>"; };
90A879881BA55798001C8989 /* NVActivityIndicatorView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NVActivityIndicatorView.swift; sourceTree = "<group>"; };
BD1452331C15810200B952C8 /* NVActivityIndicatorAnimationBallRotateChase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NVActivityIndicatorAnimationBallRotateChase.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -274,6 +277,7 @@
90A879831BA55798001C8989 /* NVActivityIndicatorAnimationSemiCircleSpin.swift */,
90A879841BA55798001C8989 /* NVActivityIndicatorAnimationSquareSpin.swift */,
90A879851BA55798001C8989 /* NVActivityIndicatorAnimationTriangleSkewSpin.swift */,
BD1452331C15810200B952C8 /* NVActivityIndicatorAnimationBallRotateChase.swift */,
);
path = Animations;
sourceTree = "<group>";
Expand Down Expand Up @@ -447,6 +451,7 @@
90A879BD1BA557B1001C8989 /* NVActivityIndicatorAnimationBallZigZagDeflect.swift in Sources */,
90A879B51BA557B1001C8989 /* NVActivityIndicatorAnimationBallRotate.swift in Sources */,
90A879B31BA557B1001C8989 /* NVActivityIndicatorAnimationBallPulseRise.swift in Sources */,
BD1452351C1586F200B952C8 /* NVActivityIndicatorAnimationBallRotateChase.swift in Sources */,
90A879C71BA557B1001C8989 /* NVActivityIndicatorAnimationSquareSpin.swift in Sources */,
90A879AE1BA557B1001C8989 /* NVActivityIndicatorAnimationBallClipRotateMultiple.swift in Sources */,
90A879AF1BA557B1001C8989 /* NVActivityIndicatorAnimationBallClipRotatePulse.swift in Sources */,
Expand Down Expand Up @@ -488,6 +493,7 @@
90A8798C1BA55798001C8989 /* NVActivityIndicatorAnimationBallClipRotatePulse.swift in Sources */,
90A879A51BA55798001C8989 /* NVActivityIndicatorAnimationTriangleSkewSpin.swift in Sources */,
90A879921BA55798001C8989 /* NVActivityIndicatorAnimationBallRotate.swift in Sources */,
BD1452341C15810200B952C8 /* NVActivityIndicatorAnimationBallRotateChase.swift in Sources */,
90A879941BA55798001C8989 /* NVActivityIndicatorAnimationBallScaleMultiple.swift in Sources */,
90A879A11BA55798001C8989 /* NVActivityIndicatorAnimationLineSpinFadeLoader.swift in Sources */,
90A879981BA55798001C8989 /* NVActivityIndicatorAnimationBallTrianglePath.swift in Sources */,
Expand Down
5 changes: 3 additions & 2 deletions NVActivityIndicatorViewDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ class ViewController: UIViewController {
.TriangleSkewSpin,
.Pacman,
.BallGridBeat,
.SemiCircleSpin]
.SemiCircleSpin,
.BallRotateChase]
let cols = 4
let rows = 7
let rows = 8
let cellWidth = Int(self.view.frame.width / CGFloat(cols))
let cellHeight = Int(self.view.frame.height / CGFloat(rows))

Expand Down

0 comments on commit 3259c34

Please sign in to comment.