iOS Developer Library — Prerelease


UIKit Framework Reference UIMotionEffect Class Reference

Deployment Target:

On This Page


The UIMotionEffect class is an abstract superclass for defining motion-based modifiers for views. Subclasses are responsible for defining the behavior to apply to a view when motion is detected. They do this by overriding the keyPathsAndRelativeValuesForViewerOffset: method and returning one or more key paths representing the view properties to modify.

Subclassing Notes

This class is abstract and cannot be instantiated directly. You can use the UIInterpolatingMotionEffect class to implement effects or you can subclass and implement your own effects. If you subclass, your subclass must conform to the NSCopying and NSCoding protocols and must implement the keyPathsAndRelativeValuesForViewerOffset: method.

  • For a given set of offset values, returns the view properties (and corresponding values) to update.



    func keyPathsAndRelativeValuesForViewerOffset(_ viewerOffset: UIOffset) -> [String : AnyObject]?


    - (NSDictionary<NSString *,id> * _Nullable)keyPathsAndRelativeValuesForViewerOffset:(UIOffset)viewerOffset



    The direction that the device is facing relative to the viewer. The range of each offset value is -1 to 1. When the device’s screen is pointed directly at the user, the offset values are (0, 0). Rotating the phone so that the screen points down or to the right of the viewer yields a positive value. Rotating the phone so that the screen points above the user or to the user’s left yields a negative value.

    Return Value

    A dictionary containing one or more key path strings representing the properties of the view to modify. The value associated with each key path string is the value to add to the given property—that is, it is the amount by which to change the current value of the property. Specify each key path relative to the view containing the motion effect object.


    Subclasses must override this method and use it to return the values to apply to a view when the user changes the relative orientation of the device’s screen. iOS computes the viewer offset values and calls this method so that subclassers can decide how that offset affects the underlying views. For example, you might use this method to modify the position of the view and its layer to give the view some visual depth.

    1. NSDictionary* dict = @{
    2. @"center" : [NSValue valueWithCGPoint:CGPointMake(3.4, 1.2)], @"layer.shadowOffset.width" : @(-1.1)
    3. };

    The key paths you return in the dictionary must refer to animatable properties. The motion effect object uses the returned information to animate the view’s properties to the new values. You can animate properties of the view, its layer, or any subviews that are accessible from the view’s properties.


    Available in iOS 7.0 and later.