欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS 雷達(dá)效果實例詳解

 更新時間:2016年09月20日 10:04:47   作者:SimpleWorld  
這篇文章主要介紹了iOS 雷達(dá)效果實例詳解的相關(guān)資料,需要的朋友可以參考下

iOS雷達(dá)效果

這段時間新app開始了,有個產(chǎn)品需求是做一個類似如下效果的雷達(dá)圖:


中間的圖片是用戶頭像,然后需要展示一個雷達(dá)掃描的效果。

分析下雷達(dá)圖的大致構(gòu)成:

  1. 底部一個呈現(xiàn)用戶頭像的UIImageView
  2. 幾個顏色漸變的同心圓,這些同心圓。 只需要在雷達(dá)視圖的drawRect方法里畫就可以了
  3. 蓋在最上層的一個扇形,且扇形的圓心和雷達(dá)圖視圖的圓心是同一個點。掃描效果就是讓這個扇形繞圓心轉(zhuǎn),因此把這個扇形抽成一個單獨的類比較好。

同時這個雷達(dá)圖應(yīng)該提供兩個接口:開始動畫,暫停動畫。因此雷達(dá)圖的.h文件暴露出來的接口如下:

@interface CPRadarView : UIView
- (void)start;//開始掃描
- (void)stop;//停止掃描
@end

.m文件實現(xiàn)如下:

typedef NS_ENUM(NSUInteger, SectorAnimationStatus) {//扇形視圖動畫狀態(tài)
 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;
}
/*
 恢復(fù)動畫
 
 @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
}

如果對扇形不做徑向顏色漸變直接用注釋的代碼即可。具體代碼就不解釋了,注釋和函數(shù)名字都很清晰。

以上就是對IOS 雷達(dá)效果的資料整理,后續(xù)繼續(xù)補充相關(guān)資料,謝謝大家對本站的支持!

相關(guān)文章

  • ios12中遇到的帶input彈窗的錯位問題的解決方法

    ios12中遇到的帶input彈窗的錯位問題的解決方法

    這篇文章主要介紹了ios12中遇到的帶input彈窗的錯位問題的解決方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • IOS使用UICollectionView實現(xiàn)無限輪播效果

    IOS使用UICollectionView實現(xiàn)無限輪播效果

    這篇文章主要為大家詳細(xì)介紹了IOS使用UICollectionView實現(xiàn)無限輪播效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-03-03
  • 百度地圖PC端判斷用戶是否在配送范圍內(nèi)

    百度地圖PC端判斷用戶是否在配送范圍內(nèi)

    在pc端設(shè)置商家的配送范圍,用戶在下單時,根據(jù)用戶設(shè)置的配送地點判斷是否在可配送范圍內(nèi),并給用戶相應(yīng)的提示,下面通過本文給大家分享具有實現(xiàn)代碼,感興趣的朋友一起學(xué)習(xí)吧
    2016-01-01
  • iOS使用UICollectionView實現(xiàn)列表頭部拉伸效果

    iOS使用UICollectionView實現(xiàn)列表頭部拉伸效果

    這篇文章主要介紹了iOS使用UICollectionView實現(xiàn)列表頭部拉伸效果,OC和Swift兩個版本,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • iOS實現(xiàn)百度外賣頭像波浪的效果

    iOS實現(xiàn)百度外賣頭像波浪的效果

    對于現(xiàn)在很多人來說,叫外賣就成了不可或缺的習(xí)慣。某日瞬間發(fā)現(xiàn)百度外賣的APP波浪效果很是吸引人,相比較其他的外賣APP,顏值略高些.(淘寶也有波浪的效果),遂就思考如何實現(xiàn)這種"浪"的效果,下面來一起看看。
    2016-08-08
  • Flutter Widgets MediaQuery控件屏幕信息適配

    Flutter Widgets MediaQuery控件屏幕信息適配

    這篇文章主要為大家介紹了Flutter Widgets 之 MediaQuery控件獲取屏幕信息和屏幕適配示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • iOS開發(fā)中使用Quartz2D繪圖及自定義UIImageView控件

    iOS開發(fā)中使用Quartz2D繪圖及自定義UIImageView控件

    這篇文章主要介紹了iOS開發(fā)中使用Quartz2D繪圖及自定義UIImageView控件的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2015-11-11
  • 解決JSON數(shù)據(jù)因為null導(dǎo)致數(shù)據(jù)加載失敗的方法

    解決JSON數(shù)據(jù)因為null導(dǎo)致數(shù)據(jù)加載失敗的方法

    前段時間發(fā)現(xiàn)一個問題,當(dāng)JSON數(shù)據(jù)中有null會導(dǎo)致數(shù)據(jù)加載失敗,后來解決了,現(xiàn)在將解決方法分享給大家,有同樣問題的朋友們可以參考。下面來一起看看吧。
    2016-09-09
  • iOS Swift 值類型與引用類型使用區(qū)別基礎(chǔ)詳解

    iOS Swift 值類型與引用類型使用區(qū)別基礎(chǔ)詳解

    這篇文章主要為大家介紹了iOS Swift 值類型與引用類型使用區(qū)別基礎(chǔ)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • iOS圖片放大的方式(transform和frame)

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

    這篇文章主要介紹了iOS圖片放大的兩種方式,transform方式放大圖片,從中心開始放大,另一種用frame改變寬高,詳細(xì)內(nèi)容請參考下文
    2016-04-04

最新評論