在项目中,常常用到轮播图,我们常常会想到用UIScrollView来实现。但是还有很多较高大上的方法,效果也更加炫酷。比如下面将要说的碎片化轮播。

效果类似现实中广告牌的翻转效果,如图

        

实际上是两层view在交替显示,所以先要一个TranformFadeView类,实现翻转的翻转碎片化的效果,翻转时间,碎片宽度的等..

TranformFadeView.h

#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

#import "TranformFadeView.h"typedef enum : NSUInteger {kSubViewTag = 0x1000,} ETranformFadeViewValue;@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 < 1 || self.verticalCount < 1) {return;}// 承载所有的maskViewself.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   <= 1 ? height : (height / self.verticalCount);CGFloat maskViewWidth  = self.horizontalCount <= 1 ? width  : (width  / self.horizontalCount);// 用以计数int count = 0;// 先水平循环,再垂直循环for (int horizontal = 0; horizontal < self.horizontalCount; horizontal++) {for (int vertical = 0; vertical < self.verticalCount; vertical++) {CGRect frame = CGRectMake(maskViewWidth  * horizontal,maskViewHeight * vertical,maskViewWidth,maskViewHeight);UIView *maskView         = [[UIView alloc] initWithFrame:frame];maskView.frame           = frame;maskView.tag             = kSubViewTag + count;maskView.backgroundColor = [UIColor blackColor];[self.allMaskView addSubview:maskView];count++;}}self.maskViewCount = count;// 存储self.countArray  = [NSMutableArray array];for (int i = 0; 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 = 0; i < self.maskViewCount; i++) {UIView *tmpView = [self maskViewWithTag:[self strategyNormal:i]];[UIView animateWithDuration:(self.fadeDuradtion <= 0.f ? 1.f : self.fadeDuradtion)delay:i * (self.animationGapDuration <= 0.f ? 0.2f : self.animationGapDuration)options:UIViewAnimationOptionCurveLinearanimations:^{tmpView.alpha = 0.f;} completion:^(BOOL finished) {}];}} else {for (int i = 0; i < self.maskViewCount; i++) {UIView *tmpView = [self maskViewWithTag:i];tmpView.alpha   = 0.f;}}
}- (void)showAnimated:(BOOL)animated {if (animated == YES) {for (int i = 0; i < self.maskViewCount; i++) {UIView *tmpView = [self maskViewWithTag:[self strategyNormal:i]];[UIView animateWithDuration:(self.fadeDuradtion <= 0.f ? 1.f : self.fadeDuradtion)delay:i * (self.animationGapDuration <= 0.f ? 0.2f : self.animationGapDuration)options:UIViewAnimationOptionCurveLinearanimations:^{tmpView.alpha = 1.f;} completion:^(BOOL finished) {}];}} else {for (int i = 0; i < self.maskViewCount; i++) {UIView *tmpView = [self maskViewWithTag:i];tmpView.alpha   = 1.f;}}
}/***  根据tag值获取maskView**  @param tag maskView的tag值**  @return tag值对应的maskView*/
- (UIView *)maskViewWithTag:(NSInteger)tag {return [self.maskView viewWithTag:tag + kSubViewTag];
}#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

控制其中,用定时器设定5张图片的交替轮播,采用两层TranformFadeView

#import "ViewController.h"
#import "TranformFadeView.h"typedef enum : NSUInteger {kTypeOne,kTypeTwo,} EType;
@interface ViewController ()@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (nonatomic, strong) TranformFadeView *tranformFadeViewOne;
@property (nonatomic, strong) TranformFadeView *tranformFadeViewTwo;
@property (nonatomic,strong)NSTimer *timer;
@property (nonatomic)         EType     type;@property (nonatomic, strong) NSArray   *images;
@property (nonatomic)         NSInteger  count;
@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];self.images = @[[UIImage imageNamed:@"banner5"],[UIImage imageNamed:@"banner1"],[UIImage imageNamed:@"banner2"],[UIImage imageNamed:@"banner3"],[UIImage imageNamed:@"banner4"]];// 图片1self.tranformFadeViewOne                      = [[TranformFadeView alloc] initWithFrame:self.contentView.bounds];self.tranformFadeViewOne.contentMode          = UIViewContentModeScaleAspectFill;self.tranformFadeViewOne.image                = [self currentImage];self.tranformFadeViewOne.verticalCount        = 3;self.tranformFadeViewOne.horizontalCount      = 20;self.tranformFadeViewOne.center               = self.contentView.center;self.tranformFadeViewOne.fadeDuradtion        = 1.f;self.tranformFadeViewOne.animationGapDuration = 0.075f;[self.tranformFadeViewOne buildMaskView];[self.contentView addSubview:self.tranformFadeViewOne];// 图片2self.tranformFadeViewTwo                      = [[TranformFadeView alloc] initWithFrame:self.contentView.bounds];self.tranformFadeViewTwo.contentMode          = UIViewContentModeScaleAspectFill;self.tranformFadeViewTwo.image                = [self currentImage];self.tranformFadeViewTwo.verticalCount        = 3;self.tranformFadeViewTwo.horizontalCount      = 30;self.tranformFadeViewTwo.center               = self.contentView.center;self.tranformFadeViewTwo.fadeDuradtion        = 1.f;self.tranformFadeViewTwo.animationGapDuration = 0.075f;[self.tranformFadeViewTwo buildMaskView];[self.contentView addSubview:self.tranformFadeViewTwo];[self.tranformFadeViewTwo fadeAnimated:NO];self.timer = [NSTimer timerWithTimeInterval:4 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSDefaultRunLoopMode];
}- (void)timerAction
{NSLog(@"adsf");if (self.type == kTypeOne) {self.type = kTypeTwo;[self.contentView sendSubviewToBack:self.tranformFadeViewTwo];self.tranformFadeViewTwo.image = [self currentImage];[self.tranformFadeViewTwo showAnimated:NO];[self.tranformFadeViewOne fadeAnimated:YES];} else {self.type = kTypeOne;[self.contentView sendSubviewToBack:self.tranformFadeViewOne];self.tranformFadeViewOne.image = [self currentImage];[self.tranformFadeViewOne showAnimated:NO];[self.tranformFadeViewTwo fadeAnimated:YES];}}
- (UIImage *)currentImage {self.count = ++self.count % self.images.count;return self.images[self.count];
}

@end

iOS 图片碎片化轮播相关推荐

  1. 图片碎片化mask动画

    图片碎片化mask动画 效果 源码 https://github.com/YouXianMing/Animations // // TransformFadeViewController.m // A ...

  2. html循环加载多个图片,两行代码实现图片碎片化加载

    今天来实现一个图片碎片化加载效果,效果如下: 我们分为 3 个步骤来实现: 定义 html 结构 拆分图片 编写动画函数 定义 html 结构 这里只需要一个 canvas 元素就可以了. id=&q ...

  3. html图片自动循环轮播图,js实现图片无缝循环轮播

    本文实例为大家分享了js实现图片无缝循环轮播的具体代码,供大家参考,具体内容如下 代码如下 Document #container{ overflow:hidden; width:400px; hei ...

  4. android 3d布局轮播,android 图片/视频混合轮播控件banner

    android 图片/视频混合轮播控件banner 在youth5201314的图片轮播控件做的修改 原作者github地址:https://github.com/youth5201314/banne ...

  5. css实现的图片列表切换轮播特效html页面前端源码

    大家好,今天给大家介绍一款, css实现的图片列表切换轮播特效html页面前端源码(图1).送给大家哦,获取方式在本文末尾. 图1 特效非常炫酷,可以用于制造音乐播放切换或视频播放切换前端(图2) 图 ...

  6. 利用jQuery实现图片无限循环轮播(不借助于轮播插件)

    原来我主要是用Bootstrap框架或者swiper插件实现轮播图的功能,而这次是用jQuery来实现图片无限循环轮播! 用到的技术有:html.css.JavaScript(少).jQuery(主要 ...

  7. 使用CSS实现简单的图片切换(轮播图)

    使用CSS实现简单的图片切换(轮播图) 预览图如下: 目录 使用CSS实现简单的图片切换(轮播图) 一:首先创建基本布局 1:创建一个div容器 ,里面的ul与ol标签分别对应轮播图片和下方圆点: 二 ...

  8. html js 3d图片轮播,js实现3D图片逐张轮播幻灯片特效代码分享

    本文实例讲述了javascript实现3D图片逐张轮播幻灯片特效.分享给大家供大家参考.具体如下: 这是一款基于javascript实现3D图片逐张轮播幻灯片特效代码,实现过程很简单. 运行效果图:- ...

  9. uniapp 实现图片自动纵向轮播

    ✴️星光不负赶路人,所有的幸运都来自于坚持不懈的努力,大家一起加油吧 问题:uniapp 实现图片自动纵向轮播 直接上代码 <template><view class="l ...

最新文章

  1. webgl三维观察综合实验_【CV现状-2】三维感知
  2. html body onunload,html页面的onunload、onbeforeunload
  3. vue和layui哪个更好用_幕布和Mind+思维导图哪个更好用?
  4. Python入门之中文乱码
  5. linux如何查看mysql是否启动
  6. MySQL压测工具--sysbench
  7. Tomcat 学习过程4
  8. 《一》Android 数据库 SQlite SQLiteOpenHelper
  9. c语言回调函数_【云里雾里】回调函数与钩子函数
  10. 如何加载和保存AutoCAD DXF文件(七参数法)
  11. VS2005 SP1安装
  12. 一个全栈工程师要掌握哪些技能
  13. 计算机图形学 期中大作业 俄罗斯方块
  14. 使用HEXO搭建个人博客时遇到的问题日志 PART.1
  15. 格式化U盘提示Windows 无法完成格式化
  16. nfsd linux是什么进程,如何从NFS启动Linux及原理
  17. 图书管理系统之普通用户、超级管理员页面布局(四)
  18. 支付宝SDK接入详细指南(附官方支付demo)
  19. 3000字/16张炫酷动态图,推荐一款好用到爆的Python可视化利器
  20. Windows下用docker打包镜像

热门文章

  1. 车辆协同下的交通信号优化与协调
  2. Jackson和Gson的使用
  3. 武警中队智慧磐石,科技强勤,三维电子沙盘开发教程第十三课
  4. 美通社发布《2020年亚太区企业传播报告》白皮书
  5. 联想网御防火墙内网地址映射不能直接访问临时解决方法
  6. 嵌入式软件开发和其他软件开发的区别
  7. 智慧水站送水票桶装水v1.0.26
  8. 中国(北京)国际智能工业与信息安全技术发展论坛暨2014年中国嵌入式系统年会邀请函...
  9. JavaFX实战:几种事件监听实现,键盘按键事件监听,鼠标滚轮事件监听,鼠标按键事件监听,鼠标移动事件监听
  10. 苹果消息服务器ip,苹果手机改服务器IP有影响吗