Skip to content

Instantly share code, notes, and snippets.

@Stickerbox
Created November 30, 2017 11:19
Show Gist options
  • Save Stickerbox/083e5b8af506920c7dd1de6ae8998aed to your computer and use it in GitHub Desktop.
Save Stickerbox/083e5b8af506920c7dd1de6ae8998aed to your computer and use it in GitHub Desktop.
A collection of simple UIView animations in Swift
//
// Created by Jordan.Dixon on 30/11/2017.
// Copyright © 2017 Jordan.Dixon. All rights reserved.
//
import UIKit
// MARK: Paralax effect
internal extension UIView {
func addParalaxEffect(amount: Int = 20) {
let horizontal = UIInterpolatingMotionEffect(keyPath: "center.x", type: .tiltAlongHorizontalAxis)
horizontal.minimumRelativeValue = -amount
horizontal.maximumRelativeValue = amount
let vertical = UIInterpolatingMotionEffect(keyPath: "center.y", type: .tiltAlongVerticalAxis)
vertical.minimumRelativeValue = -amount
vertical.maximumRelativeValue = amount
let group = UIMotionEffectGroup()
group.motionEffects = [horizontal, vertical]
addMotionEffect(group)
}
}
// MARK: Tweak
internal extension UIView {
internal enum TweakDirection {
case up, down, left, right
}
func tweak(offset: CGFloat = 30, inDirection direction: TweakDirection = .down) {
let cellAnimation = createTweakAnimation(for: self, to: offset, direction: direction)
self.layer.add(cellAnimation, forKey: "position")
}
private func createTweakAnimation(for view: UIView, to offset: CGFloat, direction: TweakDirection) -> CABasicAnimation {
let animation = CABasicAnimation(keyPath: "position")
animation.duration = 0.1
animation.repeatCount = 0
animation.autoreverses = true
animation.fromValue = CGPoint(x: view.center.x, y: view.center.y)
switch direction {
case .up:
animation.toValue = CGPoint(x: view.center.x, y: view.center.y - offset)
case .down:
animation.toValue = CGPoint(x: view.center.x, y: view.center.y + offset)
case .left:
animation.toValue = CGPoint(x: view.center.x - offset, y: view.center.y)
case .right:
animation.toValue = CGPoint(x: view.center.x + offset, y: view.center.y)
}
return animation
}
}
// MARK: Wobble
internal extension UIView {
func wobble(duration: CFTimeInterval = 0.07, repeatCount: Float = 3) {
let animation = CAKeyframeAnimation(keyPath: "transform")
let wobbleAngle: CGFloat = 0.09
let valLeft = CATransform3DMakeRotation(wobbleAngle, 0, 0, 1)
let valRight = CATransform3DMakeRotation(-wobbleAngle, 0, 0, 1)
animation.values = [valLeft, valRight]
animation.autoreverses = true
animation.duration = duration
animation.repeatCount = repeatCount
layer.add(animation, forKey: "transform")
}
}
// MARK: Tap In/Out
internal extension UIView {
func tapInAnimated(shouldRevert: Bool = true, duration: TimeInterval = 0.2) {
UIView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 1, options: [.curveEaseOut, .allowUserInteraction], animations: {
self.layer.transform = CATransform3DMakeScale(0.8, 0.8, 0.8)
}, completion: { didAnimate in
if shouldRevert { self.tapOutAnimated() }
})
}
func tapOutAnimated(duration: TimeInterval = 0.5, withDelay delay: Double = 0) {
let damping: CGFloat = delay == 0 ? 1.0 : 0.5
UIView.animate(withDuration: duration, delay: delay, usingSpringWithDamping: damping, initialSpringVelocity: damping, options: [.curveEaseOut, .allowUserInteraction], animations: {
self.layer.transform = CATransform3DIdentity
}, completion: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment