时间:2021-05-20
iOS雷达效果
这段时间新app开始了,有个产品需求是做一个类似如下效果的雷达图:
中间的图片是用户头像,然后需要展示一个雷达扫描的效果。
分析下雷达图的大致构成:
同时这个雷达图应该提供两个接口:开始动画,暂停动画。因此雷达图的.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文件如下:
radius 表示扇形的半径,degree表示扇形的弧度。其m文件如下:
如果对扇形不做径向颜色渐变直接用注释的代码即可。具体代码就不解释了,注释和函数名字都很清晰。
以上就是对IOS 雷达效果的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
AndroidImageView的selector效果实例详解在平时开发中如Button我们给它加上selector分别呈现pressed以及normal效果能
微信小程序利用css实现遮罩效果实例详解实现效果图:如图所示,使用css实现小程序的遮罩效果,代码如下js文件代码://index.js//获取应用实例vara
本文实例为大家分享了iOS实现圆环比例图的具体代码,供大家参考,具体内容如下实现效果实现方法1.SSTCircleProgressView@interfaceS
IOS图片的原生(Graphics)详解及实例一,效果图。二,工程图。三,代码。RootViewController.h#import@interfaceRoo
先上效果图-功能展示-初高级棋盘切换效果实现思路及主要代码详解1.绘制棋盘利用Quartz2D绘制棋盘.代码如下-(void)drawBackground:(C