欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > > 编程问答 >内容正文

编程问答

[控件] tranformfadeview -凯发k8官方网

发布时间:2025/1/21 编程问答 3 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 [控件] tranformfadeview 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

tranformfadeview

效果图:

 

源码地址:

https://github.com/youxianming/ui-component-collection

 

注意:

maskview是ios8中新出的,用以简化alpha遮罩的操作,与layer的mask是一回事,想要修改兼容的,请考虑使用layer的mask来满足你的需求.

 

特点:

* 方块的个数可以自己设定

* 你可以实现你自己的策略来设定渐变消失的方式

 

核心源码:

// // tranformfadeview.h // transformationfadeview // // created by xianmingyou on 15/4/16. // 凯发k8官方网 copyright (c) 2015年 xianmingyou. all rights reserved. // #import @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. // 凯发k8官方网 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 < 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 = statr_tag 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 statr_tag]; }/* 重写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

需要注意的细节:

 

总结

以上是凯发k8官方网为你收集整理的[控件] tranformfadeview的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

网站地图