
Classification: RazzleDazzle
Platform: IOS
Language: Swift
Language: Swift
Device:
iPhone / iPad
MIT
Licence:
Download
Installation
Carthage
RazzleDazzle is available through Carthage. To install it, simply add the following line to your Cartfile:github "IFTTT/RazzleDazzle"
CocoaPods
RazzleDazzle is also available through CocoaPods. To install it, simply add the following line to your Podfile:pod "RazzleDazzle"
Because
RazzleDazzle is written in Swift, be sure to add use_frameworks! at the top of your Podfile.source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!
pod 'RazzleDazzle'
Usage
Animated Paging Scroll Views
First, import
RazzleDazzle into your view controller, and subclass AnimatedPagingScrollViewController.import RazzleDazzle
class ViewController: AnimatedPagingScrollViewController {
Tell the paging scroll view controller how many pages it should have.
override func numberOfPages() -> Int {
return 4
}
Add any views you want to animate to the scrollview's
contentView on viewDidLoad.override func viewDidLoad() {
super.viewDidLoad()
contentView.addSubview(firstLabel)
}
Add your desired vertical position and size constraints to your views.
contentView.addConstraint(NSLayoutConstraint(item: firstLabel, attribute: .CenterY, relatedBy: .Equal, toItem: contentView, attribute: .CenterY, multiplier: 1, constant: 0))
Tell the animated paging scroll view controller to keep the view on the page you want it to stay on.
keepView(firstLabel, onPage: 1)
You can even tell the animated paging scroll view controller to keep the view still on more than one page, while other views scroll past it.
keepView(firstLabel, onPages: [1,2])
Or offset the view's center from the page's center:
keepView(firstLabel, onPage: 1.25)
Just make sure that if you're using any of the
keepView functions that you don't set an x-position NSLayoutConstraint on the view, as it will conflict with the animated x-position constraints generated by RazzleDazzle.RazzleDazzle Animations
Generally, creating animations in
RazzleDazzle works similarly to creating animations in JazzHands. First, import RazzleDazzle into your view controller.import RazzleDazzle
Then, create an Animator to manage all of the animations in this
UIViewController.var animator = Animator()
Create an animation for a view that you want to animate. There are multiple types of animation that can be applied to a view. For this example, we'll use
AlphaAnimation, which fades a view in and out.let alphaAnimation = AlphaAnimation(view: viewThatYouWantToAnimate)
Register the animation with the animator.
animator.addAnimation(alphaAnimation)
Add some keyframes to the animation. Let's fade this view out between times 30 and 60.
alphaAnimation[30] = 1
alphaAnimation[60] = 0
Now, to animate the view, tell the animator what time it is. For example, to tie this animation to a UIScrollView, notify the animator of time in the scroller's delegate method.
func scrollViewDidScroll(_ scrollView: UIScrollView) {
animator.animate(scrollView.contentOffset.x)
}
This will produce an effect where the view will be fully faded in and visible for scroll positions 0 to 30. Between scroll positions 30 and 60, the view will fade out to be invisible, and it will stay faded out for scroll positions greater than 60.
Animation Types
RazzleDazzle supports several types of animations:- AlphaAnimation animates the
alphaproperty (creates fade effects). - BackgroundColorAnimation animates the
backgroundColorproperty. - RotationAnimation animates a rotation transform (for rotation effects).
- ScaleAnimation applies a scaling transform (to scale view sizes).
- TranslationAnimation applies a translation transform (to translate view position).
- CornerRadiusAnimation animates the
layer.cornerRadiusproperty. - HideAnimation animates the
hiddenproperty (hides and shows views). - LayerStrokeStartAnimation animates the
strokeStartproperty of aCAShapeLayer(does not work with LayerStrokeEndAnimation). - LayerStrokeEndAnimation animates the
strokeEndproperty of aCAShapeLayer(does not work with LayerStrokeStartAnimation). - LayerFillColorAnimation animates the
fillColorproperty of aCAShapeLayer. - LayerStrokeColorAnimation animates the
strokeColorproperty of aCAShapeLayer. - PathPositionAnimation animates the
layer.positionproperty of aUIViewalong a path. - LabelTextColorAnimation animates the
textColorproperty of aUILabel. - ConstraintConstantAnimation animates an
AutoLayoutconstraint constant. - ConstraintMultiplierAnimation animates an
AutoLayoutconstraint constant as a multiple of an attribute of another view (to offset or resize views based on another view's size) - ScrollViewPageConstraintAnimation animates an
AutoLayoutconstraint constant to place a view on a scroll view page (to position views on a scrollView using AutoLayout). This is the animation doing the heavy lifting forAnimatedPagingScrollViewController'skeepView(view: onPage:)function.
Creating Custom Animation Types
RazzleDazzle is easy to extend by creating your own custom animation types!Custom Animation Types
To create your own custom animation type, your type needs to conform to the
Animatable protocol. All this requires is that you implement an animate(time:) function that takes a CGFloat time value and does something with it.
For most custom animations, you'll want to subclass
Animation with the specific type of the property you want to interpolate for each keyframe.public class BorderWidthAnimation : Animation , Animatable {
Create a property to store whatever view (or other object) you are applying the animations to, and create an initializer that takes a view as input.
private let view : UIView
public init(view: UIView) {
self.view = view
}
Optionally, you can add a function to validate any input values that will be checked each time a keyframe is added, such as for Alpha values that must range from 0 to 1.
public override func validateValue(_ value: CGFloat) -> Bool {
return (value >= 0) && (value <= 1)
}
Then, all you need to do is to make the appropriate changes to your view when the
animate(time:) function is called.public func animate(_ time: CGFloat) {
if !hasKeyframes() {return}
view.layer.borderWidth = self[time]
}
You can then create an instance of your new Animation in your
UIViewController, give it the view you'd like to animate, add it to your Animator and set some keyframes as above, and it will animate your custom property when the Animator is told to animate.Interpolatable Types
RazzleDazzle can animate any type that conforms to the Interpolatable protocol. It comes pre-cooked to support animating CGFloats, CGPoints, CGSizes, CGRects, and UIColors.
If the property you'd like to animate is of a different type, just extend that type to conform to
Interpolatable by adding a static function interpolateFrom(fromValue: toValue: withProgress:) that returns an instance of that type between two other instances of the same type.extension CGPoint : Interpolatable {
public static func interpolateFrom(fromValue: CGPoint, to toValue: CGPoint, withProgress progress: CGFloat) -> CGPoint {
assert((0 <= progress) && (progress <= 1), "Progress must be between 0 and 1")
let interpolatedX = CGFloat.interpolateFrom(fromValue.x, to: toValue.x, withProgress: progress)
let interpolatedY = CGFloat.interpolateFrom(fromValue.y, to: toValue.y, withProgress: progress)
return CGPointMake(interpolatedX, interpolatedY)
}
}
If your property is a
CGFloat or one of the other built-in interpolatable types, you only need to create an animation type that tells RazzleDazzle how to apply the keyframe values to your view, as above.