iOS開發(fā)之UIInterpolatingMotionEffect運(yùn)動效應(yīng)

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];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容