forked from Juanpe/SkeletonView
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added SkeletonAnimationBuilder to create layer animations
- Loading branch information
Showing
4 changed files
with
91 additions
and
38 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// | ||
// SkeletonAnimationBuilder.swift | ||
// SkeletonView-iOS | ||
// | ||
// Created by Juanpe Catalán on 17/11/2017. | ||
// Copyright © 2017 SkeletonView. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
typealias GradientAnimationPoint = (from: CGPoint, to: CGPoint) | ||
|
||
public enum GradientDirection { | ||
case leftRight | ||
case rightLeft | ||
case topBottom | ||
case bottomTop | ||
case topLeftBottomRight | ||
case bottomRightTopLeft | ||
|
||
public var slidingAnimation: SkeletonLayerAnimation { | ||
return SkeletonAnimationBuilder().makeSlidingAnimation(with: self) | ||
} | ||
|
||
var startPoint: GradientAnimationPoint { | ||
switch self { | ||
case .leftRight: | ||
return (from: CGPoint(x:-1, y:0.5), to: CGPoint(x:1, y:0.5)) | ||
case .rightLeft: | ||
return (from: CGPoint(x:1, y:0.5), to: CGPoint(x:-1, y:0.5)) | ||
case .topBottom: | ||
return (from: CGPoint(x:0.5, y:-1), to: CGPoint(x:0.5, y:1)) | ||
case .bottomTop: | ||
return (from: CGPoint(x:0.5, y:1), to: CGPoint(x:0.5, y:-1)) | ||
case .topLeftBottomRight: | ||
return (from: CGPoint(x:-1, y:-1), to: CGPoint(x:1, y:1)) | ||
case .bottomRightTopLeft: | ||
return (from: CGPoint(x:1, y:1), to: CGPoint(x:-1, y:-1)) | ||
} | ||
} | ||
|
||
var endPoint: GradientAnimationPoint { | ||
switch self { | ||
case .leftRight: | ||
return (from: CGPoint(x:0, y:0.5), to: CGPoint(x:2, y:0.5)) | ||
case .rightLeft: | ||
return ( from: CGPoint(x:2, y:0.5), to: CGPoint(x:0, y:0.5)) | ||
case .topBottom: | ||
return ( from: CGPoint(x:0.5, y:0), to: CGPoint(x:0.5, y:2)) | ||
case .bottomTop: | ||
return ( from: CGPoint(x:0.5, y:2), to: CGPoint(x:0.5, y:0)) | ||
case .topLeftBottomRight: | ||
return ( from: CGPoint(x:0, y:0), to: CGPoint(x:2, y:2)) | ||
case .bottomRightTopLeft: | ||
return ( from: CGPoint(x:2, y:2), to: CGPoint(x:0, y:0)) | ||
} | ||
} | ||
} | ||
|
||
public class SkeletonAnimationBuilder { | ||
|
||
public init() { | ||
} | ||
|
||
public func makeSlidingAnimation(with direction: GradientDirection, duration: CFTimeInterval = 1.5) -> SkeletonLayerAnimation { | ||
return { layer in | ||
|
||
let startPointAnim = CABasicAnimation(keyPath: #keyPath(CAGradientLayer.startPoint)) | ||
startPointAnim.fromValue = NSValue(cgPoint: direction.startPoint.from) | ||
startPointAnim.toValue = NSValue(cgPoint: direction.startPoint.to) | ||
|
||
let endPointAnim = CABasicAnimation(keyPath: #keyPath(CAGradientLayer.endPoint)) | ||
endPointAnim.fromValue = NSValue(cgPoint: direction.endPoint.from) | ||
endPointAnim.toValue = NSValue(cgPoint: direction.endPoint.to) | ||
|
||
let animGroup = CAAnimationGroup() | ||
animGroup.animations = [startPointAnim, endPointAnim] | ||
animGroup.duration = duration | ||
animGroup.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseIn) | ||
animGroup.repeatCount = .infinity | ||
|
||
return animGroup | ||
} | ||
} | ||
} |
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