iOS 雷达效果实例详解

 更新时间:2016年09月20日 10:04:47   作者:SimpleWorld  
这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下

iOS雷达效果

这段时间新app开始了,有个产品需求是做一个类似如下效果的雷达图:


中间的图片是用户头像,然后需要展示一个雷达扫描的效果。

分析下雷达图的大致构成:

  1. 底部一个呈现用户头像的UIImageView
  2. 几个颜色渐变的同心圆,这些同心圆。 只需要在雷达视图的drawRect方法里画就可以了
  3. 盖在最上层的一个扇形,且扇形的圆心和雷达图视图的圆心是同一个点。扫描效果就是让这个扇形绕圆心转,因此把这个扇形抽成一个单独的类比较好。

同时这个雷达图应该提供两个接口:开始动画,暂停动画。因此雷达图的.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 雷达效果的资料整理,后续继续补充相关资料,谢谢大家对本站的支持!

相关文章

  • ios12中遇到的带input弹窗的错位问题的解决方法

    ios12中遇到的带input弹窗的错位问题的解决方法

    这篇文章主要介绍了ios12中遇到的带input弹窗的错位问题的解决方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-05-05
  • IOS使用UICollectionView实现无限轮播效果

    IOS使用UICollectionView实现无限轮播效果

    这篇文章主要为大家详细介绍了IOS使用UICollectionView实现无限轮播效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • 百度地图PC端判断用户是否在配送范围内

    百度地图PC端判断用户是否在配送范围内

    在pc端设置商家的配送范围,用户在下单时,根据用户设置的配送地点判断是否在可配送范围内,并给用户相应的提示,下面通过本文给大家分享具有实现代码,感兴趣的朋友一起学习吧
    2016-01-01
  • iOS使用UICollectionView实现列表头部拉伸效果

    iOS使用UICollectionView实现列表头部拉伸效果

    这篇文章主要介绍了iOS使用UICollectionView实现列表头部拉伸效果,OC和Swift两个版本,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • iOS实现百度外卖头像波浪的效果

    iOS实现百度外卖头像波浪的效果

    对于现在很多人来说,叫外卖就成了不可或缺的习惯。某日瞬间发现百度外卖的APP波浪效果很是吸引人,相比较其他的外卖APP,颜值略高些.(淘宝也有波浪的效果),遂就思考如何实现这种"浪"的效果,下面来一起看看。
    2016-08-08
  • Flutter Widgets MediaQuery控件屏幕信息适配

    Flutter Widgets MediaQuery控件屏幕信息适配

    这篇文章主要为大家介绍了Flutter Widgets 之 MediaQuery控件获取屏幕信息和屏幕适配示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • iOS开发中使用Quartz2D绘图及自定义UIImageView控件

    iOS开发中使用Quartz2D绘图及自定义UIImageView控件

    这篇文章主要介绍了iOS开发中使用Quartz2D绘图及自定义UIImageView控件的方法,代码基于传统的Objective-C,需要的朋友可以参考下
    2015-11-11
  • 解决JSON数据因为null导致数据加载失败的方法

    解决JSON数据因为null导致数据加载失败的方法

    前段时间发现一个问题,当JSON数据中有null会导致数据加载失败,后来解决了,现在将解决方法分享给大家,有同样问题的朋友们可以参考。下面来一起看看吧。
    2016-09-09
  • iOS Swift 值类型与引用类型使用区别基础详解

    iOS Swift 值类型与引用类型使用区别基础详解

    这篇文章主要为大家介绍了iOS Swift 值类型与引用类型使用区别基础详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-07-07
  • iOS图片放大的方式(transform和frame)

    iOS图片放大的方式(transform和frame)

    这篇文章主要介绍了iOS图片放大的两种方式,transform方式放大图片,从中心开始放大,另一种用frame改变宽高,详细内容请参考下文
    2016-04-04

最新评论