運動效果Motion Effects:當設(shè)備上下左右傾斜會產(chǎn)生視差效果茄螃。在iOS7之后雪猪,UIInterpolatingMotionEffect提供了這樣的功能
先看效果:
請忽略右下角的水印、渣圖像渤刃、黑邊拥峦、以及小灰球按鈕
開始敲代碼
-
新建工程,設(shè)置方向
-
在SB中添加兩個ImageView卖子,分別為背景和仙女兒
- 代碼
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *back;
@property (weak, nonatomic) IBOutlet UIImageView *fairy;
@end
@implementation ViewController
-(void)viewDidLoad {
[super viewDidLoad];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
// 設(shè)置仙女的運動效果 ===== BEGIN =====
// 設(shè)置在x軸的偏移范圍
UIInterpolatingMotionEffect * fairyEffX = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];// type表示沿水平方向運行效果
fairyEffX.maximumRelativeValue = @(50);
fairyEffX.minimumRelativeValue = @(-50);
// 為view添加運動效果
[self.fairy addMotionEffect:fairyEffX];
UIInterpolatingMotionEffect * fairyEffY = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
fairyEffY.maximumRelativeValue = @(50);
fairyEffY.minimumRelativeValue = @(-50);
[self.fairy addMotionEffect:fairyEffY];
// 設(shè)置背景的運動效果 ===== BEGIN =====
UIInterpolatingMotionEffect * backEffX = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];// type表示沿水平方向運行效果
backEffX.maximumRelativeValue = @(-100);
backEffX.minimumRelativeValue = @(100);
[self.back addMotionEffect:backEffX];
UIInterpolatingMotionEffect * backEffY = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];
backEffY.maximumRelativeValue = @(-100);
backEffY.minimumRelativeValue = @(100);
[self.back addMotionEffect:backEffY];
}
@end
- 真機測試
打開設(shè)置->通用->輔助功能->減弱動態(tài)效果->關(guān)閉
醬嬸兒才可以看到效果
Demo在這里