zoukankan      html  css  js  c++  java
  • UIView使用UIMotionEffect效果

    UIView使用UIMotionEffect效果

    这个效果在模拟器上看不了,所以无法截图.

    UIView+MotionEffect.h  +  UIView+MotionEffect.m

    //
    //  UIView+MotionEffect.h
    //
    //  Copyright (c) 2014年 Nick Jensen. 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
    //
    //  UIView+MotionEffect.m
    //
    //  Copyright (c) 2014年 Nick Jensen. 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
    {
        // 返回关联
        return objc_getAssociatedObject(self, &motionEffectFlag);
    }
    
    - (void)addXAxisWithValue:(CGFloat)xValue YAxisWithValue:(CGFloat)yValue
    {
        NSLog(@"%p", &motionEffectFlag);
        
        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

    使用:

    - (void)viewDidLoad {
        
        [super viewDidLoad];
    
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"show"]];
        [self.view addSubview:imageView];
        imageView.center = self.view.center;
    
        imageView.effectGroup = [UIMotionEffectGroup new];
        [imageView addXAxisWithValue:5.f YAxisWithValue:5.f];
    }

    注意:

    给类目添加属性需要重写setter.getter方法哦:)

  • 相关阅读:
    软件测试技术实战 设计、工具及管理(51Testing软件测试网作品系列)
    MATLAB智能算法超级学习手册
    HTML与CSS入门经典(第9版)
    深入理解Android 5 源代码
    中文版Dreamweaver CS6基础培训教程(第2版)
    可用性测试手册(第2版)
    网络综合布线系统与施工技术第4版
    PHP核心技术与最佳实践(第2版)
    [OC Foundation框架
    [OC Foundation框架
  • 原文地址:https://www.cnblogs.com/YouXianMing/p/3779970.html
Copyright © 2011-2022 走看看