UIView添加背景视差效果(MotionEffect)

#pragma mark - UIView添加一个category

#pragma mark - UIView+MotionEffect.h

//

//  UIView+MotionEffect.h

//  背景视差效果

//

//  Created by HarrySun on 16/7/27.

//  Copyright © 2016 Mobby. All rights reserved.

//


#import <UIKit/UIKit.h>


@interface UIView (MotionEffect)


@property (nonatomic,strong) UIMotionEffectGroup *effectGroup;


- (void)addXAxisWithValue:(CGFloat)xValue YAxisWithValue:(CGFloat)yValue;

- (void)removeSelfMotionEffect;



@end


#pragma mark - UIView+MotionEffect.m


//

//  UIView+MotionEffect.m

//  背景视差效果

//

//  Created by HarrySun on 16/7/27.

//  Copyright © 2016 Mobby. All rights reserved.

//


#import "UIView+MotionEffect.h"

#import <objc/runtime.h>


static char motionEffectFlag;


@implementation UIView (MotionEffect)

- (void)setEffectGroup:(UIMotionEffectGroup *)effectGroup {

    // 清除关联

    objc_setAssociatedObject(self, &motionEffectFlag,nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

    

    // 建立关联

    objc_setAssociatedObject(self, &motionEffectFlag, effectGroup, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}


- (UIMotionEffectGroup *)effectGroup {

    // 返回关联

    returnobjc_getAssociatedObject(self, &motionEffectFlag);

}


- (void)addXAxisWithValue:(CGFloat)xValue YAxisWithValue:(CGFloat)yValue {

    if ((xValue >=0) && (yValue >= 0)) {

        UIInterpolatingMotionEffect *xAxis = [[UIInterpolatingMotionEffectalloc] initWithKeyPath:@"center.x"type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis];

        xAxis.minimumRelativeValue =@(-xValue);

        xAxis.maximumRelativeValue =@(xValue);

        

        UIInterpolatingMotionEffect *yAxis = [[UIInterpolatingMotionEffectalloc] initWithKeyPath:@"center.y"type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis];

        yAxis.minimumRelativeValue =@(-yValue);

        yAxis.maximumRelativeValue =@(yValue);

        

        // 先移除效果再添加效果

        self.effectGroup.motionEffects =nil;

        [selfremoveMotionEffect:self.effectGroup];

        self.effectGroup.motionEffects =@[xAxis, yAxis];

        

        // view添加效果

        [selfaddMotionEffect:self.effectGroup];

    }

}


- (void)removeSelfMotionEffect {

    [selfremoveMotionEffect:self.effectGroup];

}



@end



#pragma mark - 调用


在viewDidLoad中

    UIImageView *imageView = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"1.jpg"]];

    imageView.frame =self.view.bounds;

    imageView.center =self.view.center;

    [self.viewaddSubview:imageView];

    

    imageView.effectGroup = [UIMotionEffectGroupnew];

    

    [imageView addXAxisWithValue:15.fYAxisWithValue:15.f];



这样就能实现背景视差效果,这个效果在模拟器上无法呈现,有苹果设备的小伙伴可以将工程打包到设备上查看或者在锁屏状态下看home背景 也有背景视差效果。

demo网址:UIMotionEffect下载


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值