iOS 雷达效果实例详解
程序员文章站
2024-02-11 23:44:34
ios雷达效果
这段时间新app开始了,有个产品需求是做一个类似如下效果的雷达图:
中间的图片是用户头像,然后需要展示一个雷达扫描的效果。
分析下雷达图的...
ios雷达效果
这段时间新app开始了,有个产品需求是做一个类似如下效果的雷达图:
中间的图片是用户头像,然后需要展示一个雷达扫描的效果。
分析下雷达图的大致构成:
- 底部一个呈现用户头像的uiimageview
- 几个颜色渐变的同心圆,这些同心圆。 只需要在雷达视图的drawrect方法里画就可以了
- 盖在最上层的一个扇形,且扇形的圆心和雷达图视图的圆心是同一个点。扫描效果就是让这个扇形绕圆心转,因此把这个扇形抽成一个单独的类比较好。
同时这个雷达图应该提供两个接口:开始动画,暂停动画。因此雷达图的.h文件暴露出来的接口如下:
@interface cpradarview : uiview - (void)start;//开始扫描 - (void)stop;//停止扫描 @end
.m文件实现如下:
typedef ns_enum(nsuinteger, sectoranimationstatus) {//扇形视图动画状态 sectoranimationunstart, sectoranimationisrunning, sectoranimationispaused, }; #define circlegap 15 @interface cpradarview () @property (nonatomic, strong) cpsectorview sectorview; //扇形视图 @property (nonatomic, assign) sectoranimationstatus status; @end @implementation cpradarview - (instancetype)initwithframe:(cgrect)frame { if(self = [super initwithframe:frame]) { [self setupui]; _status = sectoranimationunstart; } return self; } - (void)setupui { self.backgroundcolor = [uicolor whitecolor]; [self addsubview:({ cgrect temp = self.frame; uiimageview imageview = [[uiimageview alloc] initwithframe:cgrectmake((temp.size.width - temp.size.width / 3.0) / 2.0, (temp.size.height - temp.size.width / 3.0) / 2.0, temp.size.width / 3.0, temp.size.width / 3.0)]; imageview.layer.cornerradius = temp.size.width / 6.0; imageview.layer.maskstobounds = yes; imageview.image = [uiimage imagenamed:@"hehe.jpg"]; imageview; })]; [self addsubview:({ cgrect temp = self.frame; _sectorview = [[cpsectorview alloc] initwithradius:temp.size.width / 6.0 + 4 circlegap degree:m_pi / 6]; cgrect frame = _sectorview.frame; frame.origin.x = (self.frame.size.width - frame.size.width) / 2.0; frame.origin.y = (self.frame.size.height - frame.size.height) / 2.0; _sectorview.frame = frame; _sectorview; })]; } - (void)start { if (_status == sectoranimationunstart) { _status = sectoranimationisrunning; cabasicanimation rotationanimation; rotationanimation = [cabasicanimation animationwithkeypath:@"transform.rotation.z"]; rotationanimation.tovalue = [nsnumber numberwithfloat: 2 m_pi ]; rotationanimation.duration = 5; rotationanimation.cumulative = yes; rotationanimation.removedoncompletion = no; rotationanimation.repeatcount = maxfloat; rotationanimation.fillmode = kcafillmodeforwards; [_sectorview.layer addanimation:rotationanimation forkey:@"rotationanimation"]; } if (_status == sectoranimationispaused) { _status = sectoranimationisrunning; [self resumelayer:_sectorview.layer]; } } - (void)stop { _status = sectoranimationispaused; [self pauselayer:_sectorview.layer]; } /* 暂停动画 @param layer layer / -(void)pauselayer:(calayer)layer { cftimeinterval pausedtime = [layer converttime:cacurrentmediatime() fromlayer:nil]; layer.speed = 0.0; layer.timeoffset = pausedtime; } /* 恢复动画 @param layer layer / - (void)resumelayer:(calayer)layer { cftimeinterval pausedtime = [layer timeoffset]; layer.speed = 1.0; layer.timeoffset = 0.0; layer.begintime = 0.0; cftimeinterval timesincepause = [layer converttime:cacurrentmediatime() fromlayer:nil] - pausedtime; layer.begintime = timesincepause; } /* 主要是用于画同心圆 @param rect rect / - (void)drawrect:(cgrect)rect { cgcontextref context = uigraphicsgetcurrentcontext(); nsarray colors = @[[uicolor colorwithhexstring:@"ff4e7d"], [uicolor colorwithhexstring:@"fd7293"], [uicolor colorwithhexstring:@"fcb8cd"], [uicolor colorwithhexstring:@"fde9f2"], [uicolor colorwithhexstring:@"fcebf3"]]; cgfloat radius = rect.size.width / 6.0; for (uicolor color in colors) { cgfloat red, green, blue, alpha; [color getred:&red green:&green blue:&blue alpha:&alpha]; cgcontextsetrgbstrokecolor(context, red, green, blue, alpha); cgcontextsetlinewidth(context, 1); cgcontextaddarc(context, rect.size.width / 2.0, rect.size.height / 2.0, radius, 0, 2* m_pi, 0); cgcontextdrawpath(context, kcgpathstroke); radius += circlegap; } }
其中cpsectorview 是定义的扇形视图,它什么都没干,只是将这个扇形画出来,其.h文件如下:
@interface cpsectorview : uiview - (instancetype)initwithradius:(cgfloat)radius degree:(cgfloat)degree; @end
radius 表示扇形的半径,degree表示扇形的弧度。其m文件如下:
@interface cpsectorview () @property (nonatomic, assign) cgfloat radius; @property (nonatomic, assign) cgfloat degree; @end @implementation cpsectorview - (instancetype)initwithradius:(cgfloat )radius degree:(cgfloat)degree { self = [super initwithframe:cgrectmake(0, 0, 2 radius, 2 radius)]; if (self) { _degree = degree; _radius = radius; } self.backgroundcolor = [uicolor clearcolor]; return self; } - (void)drawrect:(cgrect)rect { // cgcontextref context = uigraphicsgetcurrentcontext(); // uicolor *acolor = [uicolor colorwithhexstring:@"ff4e7d" alpha:0.5]; // cgcontextsetrgbstrokecolor(context, 1, 1, 1, 0); // cgpoint center = cgpointmake(cgrectgetmidx(rect), cgrectgetmidy(rect)); // cgcontextsetfillcolorwithcolor(context, acolor.cgcolor);//填充颜色 // // cgcontextmovetopoint(context, center.x, center.y); // cgcontextaddarc(context,center.x, center.y, _radius, _degree / 2.0, -_degree / 2.0, 1); // cgcontextclosepath(context); // cgcontextdrawpath(context, kcgpathfillstroke); //绘制路径 cgcontextref ctx = uigraphicsgetcurrentcontext(); uigraphicsbeginimagecontext(rect.size); cgcontextref imgctx = uigraphicsgetcurrentcontext(); cgpoint center = cgpointmake(cgrectgetmidx(rect), cgrectgetmidy(rect)); cgcontextmovetopoint(imgctx, center.x,center.y); cgcontextsetfillcolor(imgctx, cgcolorgetcomponents([uicolor blackcolor].cgcolor)); cgcontextaddarc(imgctx, center.x, center.y, _radius, _degree / 2.0, -_degree / 2.0, 1); cgcontextfillpath(imgctx);//画扇形遮罩 cgimageref mask = cgbitmapcontextcreateimage(uigraphicsgetcurrentcontext()); uigraphicsendimagecontext(); cgcontextcliptomask(ctx, self.bounds, mask); cgfloat components[8]={ 1.0, 0.306, 0.49, 0.5, //start color(r,g,b,alpha) 0.992, 0.937, 0.890, 0.5 //end color }; //为扇形增加径向渐变色 cgcolorspaceref space = cgcolorspacecreatedevicergb(); cggradientref gradient = cggradientcreatewithcolorcomponents(space, components, null,2); cgcolorspacerelease(space),space=null;//release cgpoint start = center; cgpoint end = center; cgfloat startradius = 0.0f; cgfloat endradius = _radius; cgcontextref gractx = uigraphicsgetcurrentcontext(); cgcontextdrawradialgradient(gractx, gradient, start, startradius, end, endradius, 0); cggradientrelease(gradient),gradient=null;//release }
如果对扇形不做径向颜色渐变直接用注释的代码即可。具体代码就不解释了,注释和函数名字都很清晰。
以上就是对ios 雷达效果的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!
上一篇: iOS 屏幕解锁文字动画效果