图片碎片化mask动画
图片碎片化mask动画
效果
源码
https://github.com/YouXianMing/Animations
//
// TransformFadeViewController.m
// Animations
//
// Created by YouXianMing on 15/11/17.
// Copyright © 2015年 YouXianMing. All rights reserved.
// #import "TransformFadeViewController.h"
#import "TranformFadeView.h"
#import "GCD.h" typedef enum : NSUInteger { TYPE_ONE,
TYPE_TWO, } EType; @interface TransformFadeViewController () @property (nonatomic, strong) TranformFadeView *tranformFadeViewOne;
@property (nonatomic, strong) TranformFadeView *tranformFadeViewTwo; @property (nonatomic, strong) GCDTimer *timer;
@property (nonatomic) EType type; @property (nonatomic, strong) NSArray *images;
@property (nonatomic) NSInteger count; @end @implementation TransformFadeViewController - (void)viewDidLoad { [super viewDidLoad];
} - (void)setup { [super setup]; self.images = @[[UIImage imageNamed:@""],
[UIImage imageNamed:@""],
[UIImage imageNamed:@""],
[UIImage imageNamed:@""],
[UIImage imageNamed:@""]]; // 图片1
self.tranformFadeViewOne = [[TranformFadeView alloc] initWithFrame:self.view.bounds];
self.tranformFadeViewOne.contentMode = UIViewContentModeScaleAspectFill;
self.tranformFadeViewOne.image = [self currentImage];
self.tranformFadeViewOne.verticalCount = ;
self.tranformFadeViewOne.horizontalCount = ;
self.tranformFadeViewOne.center = self.view.center;
[self.tranformFadeViewOne buildMaskView]; self.tranformFadeViewOne.fadeDuradtion = .f;
self.tranformFadeViewOne.animationGapDuration = 0.1f; [self.view addSubview:self.tranformFadeViewOne]; // 图片2
self.tranformFadeViewTwo = [[TranformFadeView alloc] initWithFrame:self.view.bounds];
self.tranformFadeViewTwo.contentMode = UIViewContentModeScaleAspectFill;
self.tranformFadeViewTwo.verticalCount = ;
self.tranformFadeViewTwo.horizontalCount = ;
self.tranformFadeViewTwo.center = self.view.center;
[self.tranformFadeViewTwo buildMaskView]; self.tranformFadeViewTwo.fadeDuradtion = .f;
self.tranformFadeViewTwo.animationGapDuration = 0.1f; [self.view addSubview:self.tranformFadeViewTwo];
[self.tranformFadeViewTwo fadeAnimated:YES]; // timer
self.timer = [[GCDTimer alloc] initInQueue:[GCDQueue mainQueue]];
[self.timer event:^{ if (self.type == TYPE_ONE) { self.type = TYPE_TWO; [self.view sendSubviewToBack:self.tranformFadeViewTwo];
self.tranformFadeViewTwo.image = [self currentImage];
[self.tranformFadeViewTwo showAnimated:NO];
[self.tranformFadeViewOne fadeAnimated:YES]; } else { self.type = TYPE_ONE; [self.view sendSubviewToBack:self.tranformFadeViewOne];
self.tranformFadeViewOne.image = [self currentImage];
[self.tranformFadeViewOne showAnimated:NO];
[self.tranformFadeViewTwo fadeAnimated:YES];
} } timeIntervalWithSecs:];
[self.timer start];
} - (UIImage *)currentImage { self.count = ++self.count % self.images.count; return self.images[self.count];
} @end
//
// TranformFadeView.h
// TransformationFadeView
//
// Created by XianMingYou on 15/4/16.
// Copyright (c) 2015年 XianMingYou. All rights reserved.
// #import <UIKit/UIKit.h> @interface TranformFadeView : UIView /**
* Image显示方式
*/
@property (nonatomic) UIViewContentMode contentMode; /**
* 要显示的相片
*/
@property (nonatomic, strong) UIImage *image; /**
* 垂直方向方块的个数
*/
@property (nonatomic) NSInteger verticalCount; /**
* 水平的个数
*/
@property (nonatomic) NSInteger horizontalCount; /**
* 开始构造出作为mask用的view
*/
- (void)buildMaskView; /**
* 渐变动画的时间
*/
@property (nonatomic) NSTimeInterval fadeDuradtion; /**
* 两个动画之间的时间间隔
*/
@property (nonatomic) NSTimeInterval animationGapDuration; /**
* 开始隐藏动画
*
* @param animated 是否执行动画
*/
- (void)fadeAnimated:(BOOL)animated; /**
* 开始显示动画
*
* @param animated 时候执行动画
*/
- (void)showAnimated:(BOOL)animated; @end
//
// TranformFadeView.m
// TransformationFadeView
//
// Created by XianMingYou on 15/4/16.
// Copyright (c) 2015年 XianMingYou. All rights reserved.
// #import "TranformFadeView.h" #define STATR_TAG 0x19871220 @interface TranformFadeView () /**
* 图片
*/
@property (nonatomic, strong) UIImageView *imageView; /**
* 所有的maskView
*/
@property (nonatomic, strong) UIView *allMaskView; /**
* maskView的个数
*/
@property (nonatomic) NSInteger maskViewCount; /**
* 存储maskView的编号
*/
@property (nonatomic, strong) NSMutableArray *countArray; @end @implementation TranformFadeView /**
* 初始化并添加图片
*
* @param frame frame值
*/
- (void)initImageViewWithFrame:(CGRect)frame { self.imageView = [[UIImageView alloc] initWithFrame:frame];
self.imageView.layer.masksToBounds = YES;
[self addSubview:self.imageView];
} - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [self initImageViewWithFrame:self.bounds];
} return self;
} - (void)buildMaskView { // 如果没有,就返回空
if (self.horizontalCount < || self.verticalCount < ) { return;
} // 承载所有的maskView
self.allMaskView = [[UIView alloc] initWithFrame:self.bounds];
self.maskView = self.allMaskView; // 计算出每个view的尺寸
CGFloat height = self.frame.size.height;
CGFloat width = self.frame.size.width;
CGFloat maskViewHeight = self.verticalCount <= ? height : (height / self.verticalCount);
CGFloat maskViewWidth = self.horizontalCount <= ? width : (width / self.horizontalCount); // 用以计数
int count = ; // 先水平循环,再垂直循环
for (int horizontal = ; horizontal < self.horizontalCount; horizontal++) { for (int vertical = ; vertical < self.verticalCount; vertical++) { CGRect frame = CGRectMake(maskViewWidth * horizontal,
maskViewHeight * vertical,
maskViewWidth,
maskViewHeight); UIView *maskView = [[UIView alloc] initWithFrame:frame];
maskView.frame = frame;
maskView.tag = STATR_TAG + count;
maskView.backgroundColor = [UIColor blackColor]; [self.allMaskView addSubview:maskView]; count++;
}
} self.maskViewCount = count; // 存储
self.countArray = [NSMutableArray array];
for (int i = ; i < self.maskViewCount; i++) { [self.countArray addObject:@(i)];
}
} /**
* 策略模式一
*
* @param inputNumber 输入
*
* @return 输出
*/
- (NSInteger)strategyNormal:(NSInteger)inputNumber { NSNumber *number = self.countArray[inputNumber];
return number.integerValue;
} - (void)fadeAnimated:(BOOL)animated { if (animated == YES) { for (int i = ; i < self.maskViewCount; i++) { UIView *tmpView = [self maskViewWithTag:[self strategyNormal:i]];
[UIView animateWithDuration:(self.fadeDuradtion <= .f ? .f : self.fadeDuradtion)
delay:i * (self.animationGapDuration <= .f ? 0.2f : self.animationGapDuration)
options:UIViewAnimationOptionCurveLinear
animations:^{ tmpView.alpha = .f; } completion:^(BOOL finished) { }];
} } else { for (int i = ; i < self.maskViewCount; i++) { UIView *tmpView = [self maskViewWithTag:i];
tmpView.alpha = .f;
}
}
} - (void)showAnimated:(BOOL)animated { if (animated == YES) { for (int i = ; i < self.maskViewCount; i++) { UIView *tmpView = [self maskViewWithTag:[self strategyNormal:i]]; [UIView animateWithDuration:(self.fadeDuradtion <= .f ? .f : self.fadeDuradtion)
delay:i * (self.animationGapDuration <= .f ? 0.2f : self.animationGapDuration)
options:UIViewAnimationOptionCurveLinear
animations:^{ tmpView.alpha = .f;
} completion:^(BOOL finished) { }];
} } else { for (int i = ; i < self.maskViewCount; i++) { UIView *tmpView = [self maskViewWithTag:i];
tmpView.alpha = .f;
}
}
} /**
* 根据tag值获取maskView
*
* @param tag maskView的tag值
*
* @return tag值对应的maskView
*/
- (UIView *)maskViewWithTag:(NSInteger)tag { return [self.maskView viewWithTag:tag + STATR_TAG];
} #pragma mark - setter & getter.
@synthesize contentMode = _contentMode;
- (void)setContentMode:(UIViewContentMode)contentMode { _contentMode = contentMode;
self.imageView.contentMode = contentMode;
} - (UIViewContentMode)contentMode { return _contentMode;
} @synthesize image = _image;
- (void)setImage:(UIImage *)image { _image = image;
self.imageView.image = image;
} - (UIImage *)image { return _image;
} @end
细节
使用的时候动态切换图片就可以了,实际上只需要创建出2个view.
图片碎片化mask动画的更多相关文章
- 一个使用openGL渲染的炫丽Android动画库二(碎片化曲面动画)
续一个使用openGL渲染的炫丽Android动画库 MagicSurfaceView v1.1.0发布, 新增碎片化曲面动画 地址:https://github.com/gplibs/android ...
- 用 Core Animation 实现图片的碎片化
用 Core Animation 实现图片的碎片化 参考书籍: 效果如下: 原理其实非常简单哦:). 1. 创建一个CALayer,使用其 contents 属性来装载一张图片(获取图片的CGImag ...
- 腾讯优测优分享 | Android碎片化问题小结——关于闪光灯的那些事儿
腾讯优测是专业的android自动化测试平台,拥有上千款真机,彻底解决android碎片化问题! 这里我要说的不是闪光灯的硬件特征,也不是说底层驱动的原理,我只是跟大家聊一聊在项目中遇到的一些关于闪光 ...
- Android学习笔记(四)之碎片化Fragment实现仿人人客户端的侧边栏
其实一种好的UI布局,可以使用户感到更加的亲切与方便.最近非常流行的莫过于侧边栏了,其实我也做过很多侧边栏的应用,但是那些侧边栏的使用我 都不是很满意,现在重新整理,重新写了一个相对来说我比较满意的侧 ...
- jQuery css3鼠标悬停图片显示遮罩层动画特效
jQuery css3鼠标悬停图片显示遮罩层动画特效 效果体验:http://hovertree.com/texiao/jquery/39/ 效果图: 源码下载:http://hovertree.co ...
- Atitit 图像处理 灰度图片 灰度化的原理与实现
Atitit 图像处理 灰度图片 灰度化的原理与实现 24位彩色图与8位灰度图 首先要先介绍一下24位彩色图像,在一个24位彩色图像中,每个像素由三个字节表示,通常表示为RGB.通常,许多24位彩色图 ...
- [No000034]知乎-长期接收碎片化知识有什么弊端?
你所接受的一切信息,构成了你的思维方式. 所以,长期接受碎片信息的后果,就是让你的思维变得狭隘,难以进行复杂的思考. 碎片信息通常具备这样的特征: •它们往往是一些事实的集合而非逻辑 •它们往往大量简 ...
- HTML5+javascript实现图片加载进度动画效果
在网上找资料的时候,看到网上有图片加载进度的效果,手痒就自己也写了一个. 图片加载完后,隐藏loading效果. 想看加载效果,请ctrel+F5强制刷新或者清理缓存. 效果预览: 0% // ...
- 程序员MM的自白:磨人小妖精之安卓碎片化
文/腾讯优测 章婉霞 除了crash问题,Android平台的碎片化越来越受到移动开发的关注,且不谈支持Android系统的移动设备早已过万款,屏幕.品牌以及传感器等方面的碎片化问题也困扰着开发者. ...
随机推荐
- Error_code: 1594(mysql 5617)主从同步报错
报错信息 2017-09-05 09:37:22 7425 [ERROR] Slave SQL: Relay log read failure: Could not parse relay log e ...
- HBase(一)HBase入门简介
一 HBase 的起源 HBase 的原型是 Google 的 BigTable 论文,受到了该论文思想的启发,目前作为 Hadoop 的子项目来开发维护,用于支持结构化的数据存储. Apache H ...
- Ionic Js十九:加载动画
ion-spinner ionSpinner 提供了许多种旋转加载的动画图标.当你的界面加载时,你就可以呈现给用户相应的加载图标. 该图标采用的是SVG.  实例 HTML 代码 <ion-c ...
- 41:和为S的两个数
import java.util.ArrayList; import java.util.Collections; /** * 面试题41:和为S的两个数 * 输入一个递增排序的数组和一个数字S,在数 ...
- Python并发编程系列之多进程(multiprocessing)
1 引言 本篇博文主要对Python中并发编程中的多进程相关内容展开详细介绍,Python进程主要在multiprocessing模块中,本博文以multiprocessing种Process类为中心 ...
- java UTF8 HEX
private final static char[] hexArray = "0123456789ABCDEF".toCharArray(); public static Str ...
- python-arcade时钟
最近开始学习arcade的图形库,感觉功能很丰富,尝试画了个时钟,显示如下: 贴上调整好的代码: import arcade import math,time SCREEN_WIDTH = 800 S ...
- 切换 NPM 镜像源
转载:快速切换NPM源 我们介绍过cnpmjs.org和淘宝 npm 两个 NPM 镜像.除此之外,还有一些国外的 NPM 镜像.不同地区访问不同的镜像速度可能有差异,因此有时候需要切换 NPM 镜像 ...
- Swift2.0语言教程之下标脚本
Swift2.0语言教程之下标脚本 下标脚本 下标脚本是访问对象.集合或者序列的快速方式.开发者不需要调用实例特定的赋值和访问方法,就可以直接访问所需要的数值.例如在数组中,可以直接使用下标去访问或者 ...
- Django Q对象
使用Q 对象进行复杂的查询¶ filter() 等方法中的关键字参数查询都是一起进行“AND” 的. 如果你需要执行更复杂的查询(例如OR 语句),你可以使用Q 对象. Q 对象 (django.db ...