iOS开发之UIInterpolatingMotionEffect运动效应

iOS7增加了视差效果,即控件在屏幕上的位置随设备倾斜(上下左右)而偏移,可以通过UIInterpolatingMotionEffect类实现。
关键类UIInterpolatingMotionEffect有4个属性:

keyPath:倾斜屏幕将要影响的属性,比如center.x。
type:观察者视角,即屏幕的倾斜方向(水平/垂直),分别对应
UIInterpolatingMotionEffectType的两个枚举值。
minimumRelativeValue/maximumRelativeValue:控件偏移量阈值,注意是id类型。

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) UIImageView *backgroundImageView;
@property (nonatomic, strong) UIImageView *foregroundImageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}


/**
 重写控制器方法:隐藏当前控制器的statusbBar
 
 @return YES
 */
- (BOOL)prefersStatusBarHidden {
    return YES;
}


/**
 初始化UI
 */
- (void)setupUI {
    [self pf_AddSubviews];
    [self pf_addMotionEffect];
}


/**
 添加子控件
 */
- (void)pf_AddSubviews {
    _backgroundImageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.backgroundImageView.image = [UIImage imageNamed:@"img_background"];
    [self.view addSubview:self.backgroundImageView];
    
    _foregroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width * 0.25,
                                                                         [UIScreen mainScreen].bounds.size.height * 0.25,
                                                                         [UIScreen mainScreen].bounds.size.width * 0.5,
                                                                         [UIScreen mainScreen].bounds.size.height * 0.5)];
    self.foregroundImageView.image = [UIImage imageNamed:@"img_foreground"];
    [self.view addSubview:self.foregroundImageView];
}


/**
 添加运动效应
 */
- (void)pf_addMotionEffect {
    // 需要在手机的系统设置的辅助功能中关闭减弱动态效果,界面才会有效果
    
    // 给imageView添加运动效应
    
    // 前景图片:x方向
    // 创建motionEffect对象
    UIInterpolatingMotionEffect * foregroundMotionEffectX = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
    // 设置偏移阈值
    foregroundMotionEffectX.maximumRelativeValue = @(50);
    foregroundMotionEffectX.minimumRelativeValue = @(-50);
    // 将动效添加到目标控件
    [self.foregroundImageView addMotionEffect:foregroundMotionEffectX];
    
    // 前景图片:y方向
    UIInterpolatingMotionEffect * foregroundMotionEffectY = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
    foregroundMotionEffectY.maximumRelativeValue = @(50);
    foregroundMotionEffectY.minimumRelativeValue = @(-50);
    [self.foregroundImageView addMotionEffect:foregroundMotionEffectY];

    // 背景图片:x方向
    UIInterpolatingMotionEffect * backgroundMotionEffect = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
    backgroundMotionEffect.maximumRelativeValue = @(-100);
    backgroundMotionEffect.minimumRelativeValue = @(100);
    [self.backgroundImageView addMotionEffect:backgroundMotionEffect];
    
    // 背景图片:y方向
    UIInterpolatingMotionEffect * backEffY = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
    backEffY.maximumRelativeValue = @(-100);
    backEffY.minimumRelativeValue = @(100);
    [self.backgroundImageView addMotionEffect:backEffY];
}

你可能感兴趣的:(iOS开发之UIInterpolatingMotionEffect运动效应)