Skip to content

Instantly share code, notes, and snippets.

@Jesse-calkin
Created March 31, 2016 04:01
Show Gist options
  • Save Jesse-calkin/5ded71b68f578f441c95a4e92d05fa76 to your computer and use it in GitHub Desktop.
Save Jesse-calkin/5ded71b68f578f441c95a4e92d05fa76 to your computer and use it in GitHub Desktop.
UIMotionEffect Extensions
//
// UIMotionEffectExtensions.swift
// Collection Views
//
// Created by jesse calkin on 3/30/16.
// Copyright © 2016 Shoshin Boogie. All rights reserved.
//
import UIKit
extension UIMotionEffect {
/**
Creates a `UIInterpolatingMotionEffect` for both `x` and `y` axis.
- parameter offset: `CGSize` for the amount of offset to be applied on the `x` and `y` axis when tilting the device.
*/
class func tiltEffect(offset: CGSize) -> UIMotionEffect {
let horizontalEffect = UIInterpolatingMotionEffect(keyPath: "center.x", type: UIInterpolatingMotionEffectType.TiltAlongHorizontalAxis)
horizontalEffect.minimumRelativeValue = offset.width * -1
horizontalEffect.maximumRelativeValue = offset.width
let verticalEffect = UIInterpolatingMotionEffect(keyPath: "center.y", type: UIInterpolatingMotionEffectType.TiltAlongVerticalAxis)
verticalEffect.minimumRelativeValue = offset.height * -1
verticalEffect.maximumRelativeValue = offset.height
let effectGroup = UIMotionEffectGroup()
effectGroup.motionEffects = [horizontalEffect, verticalEffect]
return effectGroup
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment