iOS Developer Library

Developer

UIKit Framework Reference UIMotionEffect Class Reference

Options
Deployment Target:

On This Page
Language:

UIMotionEffect

Conforms To


Import Statement


Swift

import UIKit

Objective-C

@import UIKit;

Availability


Available in iOS 7.0 and later.

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.

    Declaration

    Swift

    func keyPathsAndRelativeValuesForViewerOffset(_ viewerOffset: UIOffset) -> [NSObject : AnyObject]!

    Objective-C

    - (NSDictionary *)keyPathsAndRelativeValuesForViewerOffset:(UIOffset)viewerOffset

    Parameters

    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.

    Discussion

    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.

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

    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.

    Import Statement

    Objective-C

    @import UIKit;

    Swift

    import UIKit

    Availability

    Available in iOS 7.0 and later.