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];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容