iOS development UIMotionEffect motion visual effects

1. Introduction to UIMotionEffect

  In iOS7.0, the UIMotionEffect motion visual effect was launched, that is, the effect is different from the screen offset from different angles!

NS_CLASS_AVAILABLE_IOS(7_0)
@interface UIMotionEffect : NSObject <NSCopying, NSCoding>
- (instancetype)init NS_DESIGNATED_INITIALIZER;
 - (nullable instancetype)initWithCoder:(NSCoder * )aDecoder NS_DESIGNATED_INITIALIZER;
 // The observer's angle offset viewerOffset, get the attributes and values ​​of the motion visual effect- 
(nullable NSDictionary<NSString *, id > * )keyPathsAndRelativeValuesForViewerOffset:(UIOffset)viewerOffset;
 @end

typedef NS_ENUM(NSInteger, UIInterpolatingMotionEffectType) {
    UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis, // X axis 
    UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis // Y axis 
};

@interface UIInterpolatingMotionEffect : UIMotionEffect
- (instancetype)initWithKeyPath:(NSString *)keyPath type:(UIInterpolatingMotionEffectType)type NS_DESIGNATED_INITIALIZER;//初始化
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;

@property ( readonly , nonatomic) NSString *keyPath; // Get the angle offset 
@property ( readonly , nonatomic) UIInterpolatingMotionEffectType type; // Get the type 
@property (nullable, strong, nonatomic) id minimumRelativeValue; // Minimum angle offset 
@ property (nullable, strong, nonatomic) id maximumRelativeValue; // Maximum angle offset 
@end

@interface UIMotionEffectGroup : UIMotionEffect
@property (nullable, copy, nonatomic) NSArray <__kindof UIMotionEffect *> *motionEffects; // Add horizontal and vertical effects to the corresponding UI 
@end

 

2. Simple to use

- (void)addEffectWithOffset:(NSInteger)offset withView:(UIView *)view{
    UIInterpolatingMotionEffect *effectX = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
    effectX.minimumRelativeValue = @(-offset);
    effectX.maximumRelativeValue = @(offset);
    
    UIInterpolatingMotionEffect *effectY = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
    effectY.minimumRelativeValue = @(-offset/2);
    effectY.maximumRelativeValue = @(offset/2);
    
//    UIMotionEffectGroup *group = [[UIMotionEffectGroup alloc] init];
//    group.motionEffects = @[effectX,effectY];
    view.motionEffects = @[effectX,effectY];
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324747050&siteId=291194637