UIView添加背景视差效果(MotionEffect)
来源:互联网 发布:opencv车流量统计算法 编辑:程序博客网 时间:2024/04/29 04:14
#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 - 调用
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];
- UIView添加背景视差效果(MotionEffect)
- 背景移动视差效果
- ViewPager的视差背景效果
- jquery实现的视差滚动教程(视差大背景效果)
- UIMotionEffect和Home页背景视差效果
- UIMotionEffect和Home页背景视差效果
- 类似Launcher壁纸的视差背景效果
- ParallaxViewPager:ViewPager的视差背景效果
- UIMotionEffect和Home页背景视差效果
- ParallaxViewPager:ViewPager的视差背景效果
- UIMotionEffect和Home页背景视差效果
- ParallaxViewPager:ViewPager的视差背景效果
- 为UIView添加图片背景
- 利用CSS固定背景交替实现视差滚动效果
- 视差效果
- UIview添加阴影效果Shadow
- 给UIview添加阴影效果
- 给UIView添加阴影效果
- Android路由框架设计与实现
- 网线
- AngularJS Promise
- PyGobject(二十七)布局容器之PopoverMenu
- Train Problem II hdu 1023
- UIView添加背景视差效果(MotionEffect)
- 组合博弈 -- 三大基本博弈
- wiringPi安装
- C#+Arcgis Engine开发-点选查询
- Fence Repair(优先队列) POJ 3253
- Oracle数据库TNS常见错误解决方法
- 单点登录思路
- 数独问题的方法
- 在windows下使用VirtualEnv