iOS7增加了視差效果,即控件在屏幕上的位置隨設(shè)備傾斜(上下左右)而偏移,可以通過UIInterpolatingMotionEffect
類實(shí)現(xiàn)。
關(guān)鍵類UIInterpolatingMotionEffect
有4個屬性:
keyPath
:傾斜屏幕將要影響的屬性,比如center.x。
type
:觀察者視角,即屏幕的傾斜方向(水平/垂直),分別對應(yīng)
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];
}
/**
重寫控制器方法:隱藏當(dāng)前控制器的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];
}
/**
添加運(yùn)動效應(yīng)
*/
- (void)pf_addMotionEffect {
// 需要在手機(jī)的系統(tǒng)設(shè)置的輔助功能中關(guān)閉減弱動態(tài)效果,界面才會有效果
// 給imageView添加運(yùn)動效應(yīng)
// 前景圖片:x方向
// 創(chuàng)建motionEffect對象
UIInterpolatingMotionEffect * foregroundMotionEffectX = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];
// 設(shè)置偏移閾值
foregroundMotionEffectX.maximumRelativeValue = @(50);
foregroundMotionEffectX.minimumRelativeValue = @(-50);
// 將動效添加到目標(biāo)控件
[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];
}