UIView添加背景视差效果(MotionEffect)
2016-07-27 17:09
337 查看
#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下载
#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下载
相关文章推荐
- C# .net Ueditor实现图片上传到阿里云OSS 对象存储
- Error:C:\Users\lqm\.gradle\caches\2.10\scripts\ijinit34_7wu3ex74z3a8e98fc8d35fuid\cp_init\cache.prop
- 修改开机提示
- HDU 4027 Can you answer these queries?
- issue配置文件说明
- Builder设计模式
- Android view中的requestLayout和invalidate方法
- Android Training--Managing the System UI
- 我的Android进阶之旅------>解决DownloadManager报错java.lang.SecurityException: Invalid value for visibility: 2
- DES对称加密算法 CryptoUitls
- Debugging performance issues in Go programs
- Android 同时集成阿里云推送和阿里百川IM会报BuildConfig.class重复的错
- ./configure: error: SSL modules require the OpenSSL library
- [LeetCode] 62. Unique Paths
- Qt Quick 底部导航+路径动画
- Error:java: javacTask: source release 8 requires target release 1.8
- UGUI通用脚本
- ios queue及GCD理解的文章
- ScrollView嵌套TableView,左划删除手势失效解决方案
- JD-GUI在Ubuntu 14.04 64位系统依赖包失败的问题