(1)UIView+MotionEffect.h
#import <UIKit/UIKit.h> @interface UIView (MotionEffect) @property (nonatomic, strong) UIMotionEffectGroup *effectGroup; - (void)addXAxisWithValue:(CGFloat)xValue YAxisWithValue:(CGFloat)yValue; - (void)removeSelfMotionEffect; @end
(2)UIView+MotionEffect.m
#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 { // 返回关联 return objc_getAssociatedObject(self, &motionEffectFlag); } - (void)addXAxisWithValue:(CGFloat)xValue YAxisWithValue:(CGFloat)yValue { if ((xValue >= 0) && (yValue >= 0)) { UIInterpolatingMotionEffect *xAxis = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.x" type:UIInterpolatingMotionEffectTypeTiltAlongHorizontalAxis]; xAxis.minimumRelativeValue = @(-xValue); xAxis.maximumRelativeValue = @(xValue); UIInterpolatingMotionEffect *yAxis = [[UIInterpolatingMotionEffect alloc] initWithKeyPath:@"center.y" type:UIInterpolatingMotionEffectTypeTiltAlongVerticalAxis]; yAxis.minimumRelativeValue = @(-yValue); yAxis.maximumRelativeValue = @(yValue); // 先移除效果再添加效果 self.effectGroup.motionEffects = nil; [self removeMotionEffect:self.effectGroup]; self.effectGroup.motionEffects = @[xAxis, yAxis]; // 给view添加效果 [self addMotionEffect:self.effectGroup]; } } - (void)removeSelfMotionEffect { [self removeMotionEffect:self.effectGroup]; } @end
(3)调用示例
- (void)viewDidLoad { [super viewDidLoad]; UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"flower"]]; imageView.center = self.view.center; [self.view addSubview:imageView]; imageView.effectGroup = [UIMotionEffectGroup new]; [imageView addXAxisWithValue:15.f YAxisWithValue:15.f]; }
相关推荐
为UIView添加弹簧效果,并设置显示文本内容和弹出次数
Fireworks - 为 UIView 添加烟花动画效果
6个UIView的弹簧效果布局,添加点击实现
自定义 UIView的阴影效果自定义 UIView的阴影效果
使用Swfit扩展为UIView添加随UIScrollView滚动产生视差效果
UIView-Parallaxify UIView类别可向UIView添加视差效果
RKNotificationHub:为任何UIView添加一个通知角标
UIView 不像 UIButton 加了点击事件就会有点击效果,体验要差不少,这里分别通过自定义和扩展来实现类似 UIButton 的效果。 正文 一、为 UIView 添加点击事件 extension UIView { func addOnClickListener...
MCUIViewLayout 通过UIView添加的类别可轻松构建布局
UIView 震动动画效果 AFViewShaker ,AFViewShaker 是一个简单的工具用来执行 。 使用方法: AFViewShaker *...
Twinkle - ✨一个Swift和简单的方法能让任何UIView产生闪烁效果
uiView 动画翻转的效果,显示前后两个视图替换显示
github:https://github.com/Mr-yuwei/Yeebadge
自己写了一个Dome 实现了UIView或者时UIViewController 缩放动画效果
已封装好的实现渐变的UIView,只需传入渐变开始值和结束值就可直接使用。有四种渐变方向可选
UIView切换效果集 HMGLTransitions ,HMGLTransitions 实现了一组不同的 UIView 之间切换效果的功能。
为UIView添加圆角扩展
支持 CocoaPods! pod 'LCBlur' GitHub:https://github.com/iTofu/LCBlur 请在 GitHub 上查阅,以保证版本最新! 有问题请移步 GitHub!...(之前发过一次,效果没讲清楚,貌似不能编辑,重新发下哈!)
使用拓展与runtime,为UIView添加手势事件