www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當前位置:首頁 > > 充電吧
[導讀]前段時間 沒事兒,自己自定義了 一個雷達掃描/擴散效果的View。掃描View 效果如下:擴散View 效果如下:自定義的代碼如下:1.?RadarView.h#importtypedef?NS_EN



前段時間 沒事兒,自己自定義了 一個雷達掃描/擴散效果的View。

掃描View 效果如下:



擴散View 效果如下:



自定義的代碼如下:

1.?RadarView.h


#importtypedef?NS_ENUM(NSInteger,?RadarViewType)?{
????RadarViewTypeScan,
????RadarViewTypeDiffuse
};

@interface?RadarView?:?UIView

/**?雷達?空心圓圈的顏色?*/
@property?(nonatomic,?strong)?UIColor?*?radarLineColor;
/**?扇形開始顏色?必須由RGBA值初始化
?*??[UIColor?colorWithRed:?green:?blue:?alpha:]
?*/
@property?(nonatomic,?strong)?UIColor?*?startColor;
/**?扇形結(jié)束顏色?必須由RGBA值初始化
?*??[UIColor?colorWithRed:?green:?blue:?alpha:]
?*/
@property?(nonatomic,?strong)?UIColor?*?endColor;

/**
?*
?*??@param?radius???????半徑
?*??@param?angle????????角度
?*??@param?radarLineNum?雷達線數(shù)量
?*??@param?hollowRadius?空心圓半徑
?*
?*??@return?掃描?雷達?View
?*/
+?(RadarView?*)scanRadarViewWithRadius:(CGFloat)radius
?????????????????????????????????angle:(int)angle
??????????????????????????radarLineNum:(int)radarLineNum
??????????????????????????hollowRadius:(CGFloat)hollowRadius;


/**
?*
?*??@param?startRadius?擴散圓?起始的半徑
?*??@param?endRadius???擴散圓?消失的半徑
?*??@param?circleColor?擴散圓?的顏色
?*
?*??@return?擴散?雷達?View
?*/
+?(RadarView?*)diffuseRadarViewWithStartRadius:(CGFloat)startRadius
?????????????????????????????????????endRadius:(CGFloat)endRadius
???????????????????????????????????circleColor:(UIColor?*)circleColor;

/**
?*??展示在targerView上
?*
?*??@param?targerView?
?*/
-?(void)showTargetView:(UIView?*)targerView;

-?(void)dismiss;

/**?開始掃描動畫?*/
-?(void)startAnimatian;

/**?停止掃描動畫?*/
-?(void)stopAnimation;

@end


2.?RadarView.m


#import?"RadarView.h"

#define?CenterX?self.bounds.size.width*0.5
#define?CenterY?self.bounds.size.height*0.5

#define?DefaultRadarLineColor?[UIColor?colorWithWhite:1?alpha:0.7]
#define?DefaultStartColor?[UIColor?colorWithRed:1?green:1?blue:1?alpha:0.5]
#define?DefaultEndColor?[UIColor?colorWithRed:1?green:1?blue:1?alpha:0]

#define?DefaultCircleColor?[UIColor?colorWithWhite:1?alpha:0.5]

@interface?RadarView?()

#pragma?mark?-?掃描類型的RadarView?屬性
/**?扇形半徑?*/
@property?(nonatomic,?assign)?CGFloat?sectorRadius;
/**?扇形?角度?*/
@property?(nonatomic,?assign)?int?angle;
/**?雷達?空心圓圈的數(shù)量?*/
@property?(nonatomic,?assign)?int?radarLineNum;
/**?中心?空心圓的半徑?(一般?這里放置一個圓形的頭像)?*/
@property?(nonatomic,?assign)?int?hollowRadius;

#pragma?mark?-?擴散類型的RadarView?屬性
/**?擴散動畫?起始?的半徑?*/
@property?(nonatomic,?assign)?CGFloat?startRadius;
/**?擴散動畫?結(jié)束?的半徑?*/
@property?(nonatomic,?assign)?CGFloat?endRadius;
/**?圓圈的顏色?*/
@property?(nonatomic,?strong)?UIColor?*?circleColor;

@property?(nonatomic,?strong)?NSTimer?*?timer;

@property?(nonatomic,?assign)?RadarViewType?radarViewType;

@end

@implementation?RadarView

+?(RadarView?*)scanRadarViewWithRadius:(CGFloat)radius?angle:(int)angle?radarLineNum:(int)radarLineNum?hollowRadius:(CGFloat)hollowRadius?{
????return?[[self?alloc]?initWithRadius:radius?angle:angle?radarLineNum:radarLineNum?hollowRadius:hollowRadius];
}

-?(instancetype)initWithRadius:(CGFloat)radius
?????????????????????????angle:(int)angle
??????????????????radarLineNum:(int)radarLineNum
??????????????????hollowRadius:(CGFloat)hollowRadius?{
????if?(self?=?[super?init])?{
????????self.radarViewType?=?RadarViewTypeScan;
????????self.sectorRadius?=?radius;
????????self.frame?=?CGRectMake(0,?0,?radius*2,?radius*2);
????????self.angle?=?angle;
????????self.radarLineNum?=?radarLineNum-1;
????????self.hollowRadius?=?hollowRadius;
????????self.backgroundColor?=?[UIColor?clearColor];
????}
????return?self;
}

+?(RadarView?*)diffuseRadarViewWithStartRadius:(CGFloat)startRadius?endRadius:(CGFloat)endRadius?circleColor:(UIColor?*)circleColor?{
????return?[[self?alloc]?initWithStartRadius:startRadius?endRadius:endRadius?circleColor:circleColor];
}

-?(instancetype)initWithStartRadius:(CGFloat)startRadius?endRadius:(CGFloat)endRadius?circleColor:(UIColor?*)circleColor?{
????if?(self?=?[super?init])?{
????????self.radarViewType?=?RadarViewTypeDiffuse;
????????self.frame?=?CGRectMake(0,?0,?endRadius*2,?endRadius*2);
????????self.startRadius?=?startRadius;
????????self.endRadius?=?endRadius;
????????self.circleColor?=?circleColor;
????????self.backgroundColor?=?[UIColor?clearColor];
????}
????return?self;
}

//?Only?override?drawRect:?if?you?perform?custom?drawing.
//?An?empty?implementation?adversely?affects?performance?during?animation.
-?(void)drawRect:(CGRect)rect?{
????//?Drawing?code
????if?(_radarViewType?==?RadarViewTypeScan)?{
????????if?(!_startColor)?{
????????????_startColor?=?DefaultStartColor;
????????}
????????if?(!_endColor)?{
????????????_endColor?=?DefaultEndColor;
????????}
????????if?(!_radarLineColor)?{
????????????_radarLineColor?=?DefaultRadarLineColor;
????????}
????????
????????//?畫雷達線
????????[self?drawRadarLine];
????????
????????CGContextRef?context?=?UIGraphicsGetCurrentContext();
????????//?把要畫的扇形?分開畫,一次畫1°,每次的顏色漸變
????????for?(int?i?=?0;?i?<?_angle;?i++)?{
????????????UIColor?*?color?=?[self?colorWithCurrentAngleProportion:i*1.0/_angle];
????????????[self?drawSectorWithContext:context?color:color?startAngle:-90-i];
????????}
????}
}

/**?畫扇形?*/
-?(void)drawSectorWithContext:(CGContextRef)context
????????????????????????color:(UIColor?*)color
???????????????????startAngle:(CGFloat)startAngle?{
????//畫扇形,也就畫圓,只不過是設(shè)置角度的大小,形成一個扇形
????CGContextSetFillColorWithColor(context,?color.CGColor);//填充顏色
????CGContextSetLineWidth(context,?0);//線的寬度
????//以self.radius為半徑圍繞圓心畫指定角度扇形
????CGContextMoveToPoint(context,?CenterX,?CenterY);
????CGContextAddArc(context,?CenterX,?CenterY,?_sectorRadius,?startAngle?*?M_PI?/?180,?(startAngle-1)?*?M_PI?/?180,?1);
????CGContextClosePath(context);
????CGContextDrawPath(context,?kCGPathFillStroke);?//繪制路徑
}


/**?畫雷達線?*/
-?(void)drawRadarLine?{
????CGFloat?minRadius?=?(_sectorRadius-_hollowRadius)*(pow(0.618,?_radarLineNum-1));
????/**?畫?圍著空心半徑的第一個空心圓,此圓不在計數(shù)內(nèi)?*/
????[self?drawLineWithRadius:_hollowRadius+minRadius*0.382];
????
????for?(int?i?=?0;?i?<?_radarLineNum;?i++)?{
????????[self?drawLineWithRadius:_hollowRadius?+?minRadius/pow(0.618,?i)];
????}
}

/**?畫空心圓?*/
-?(void)drawLineWithRadius:(CGFloat)radius?{
????CAShapeLayer?*solidLine?=??[CAShapeLayer?layer];
????CGMutablePathRef?solidPath?=??CGPathCreateMutable();
????solidLine.lineWidth?=?1.0f?;
????solidLine.strokeColor?=?_radarLineColor.CGColor;
????solidLine.fillColor?=?[UIColor?clearColor].CGColor;
????CGPathAddEllipseInRect(solidPath,?nil,?CGRectMake(self.bounds.size.width*0.5-radius,?self.bounds.size.height*0.5-radius,?radius*2,?radius*2));
????solidLine.path?=?solidPath;
????CGPathRelease(solidPath);
????[self.layer?addSublayer:solidLine];
}

#pragma?mark?-?展示
-?(void)showTargetView:(UIView?*)targerView?{
????self.center?=?targerView.center;
????[targerView?addSubview:self];
}

#pragma?mark?-?
-?(void)dismiss?{
????[self?removeFromSuperview];
}

#pragma?mark?-?開始動畫
-?(void)startAnimatian?{
????if?(_radarViewType?==?RadarViewTypeScan)?{
????????CABasicAnimation*?rotationAnimation;
????????rotationAnimation?=?[CABasicAnimation?animationWithKeyPath:@"transform.rotation.z"];
????????rotationAnimation.toValue?=?[NSNumber?numberWithFloat:?1?*?M_PI?*?2.0?];
????????rotationAnimation.duration?=?2;
????????rotationAnimation.cumulative?=?YES;
????????rotationAnimation.repeatCount?=?INT_MAX;
????????[self.layer?addAnimation:rotationAnimation?forKey:@"rotationAnimation"];
????}?else?{
????????[self?diffuseAnimation];
????????_timer?=?[NSTimer?scheduledTimerWithTimeInterval:0.5?target:self?selector:@selector(diffuseAnimation)?userInfo:nil?repeats:YES];
????}
}

#pragma?mark?-?結(jié)束動畫
-?(void)stopAnimation?{
????if?(_radarViewType?==?RadarViewTypeScan)?{
????????[self.layer?removeAnimationForKey:@"rotationAnimation"];
????}?else?{
????????[_timer?invalidate];
????????_timer?=?nil;
????}
}

-?(UIColor?*)colorWithCurrentAngleProportion:(CGFloat)angleProportion?{
????NSArray?*?startRGBA?=?[self?RGBA_WithColor:_startColor];
????NSArray?*?endRGBA?=?[self?RGBA_WithColor:_endColor];
????CGFloat?currentR?=?[startRGBA[0]?floatValue]?-?([startRGBA[0]?floatValue]-[endRGBA[0]?floatValue])?*?angleProportion;
????CGFloat?currentG?=?[startRGBA[1]?floatValue]?-?([startRGBA[1]?floatValue]-[endRGBA[1]?floatValue])?*?angleProportion;
????CGFloat?currentB?=?[startRGBA[2]?floatValue]?-?([startRGBA[2]?floatValue]-[endRGBA[2]?floatValue])?*?angleProportion;
????CGFloat?currentA?=?[startRGBA[3]?floatValue]?-?([startRGBA[3]?floatValue]-[endRGBA[3]?floatValue])?*?angleProportion;
????return?[UIColor?colorWithRed:currentR?green:currentG?blue:currentB?alpha:currentA];
}

/**
?*??將UIColor對象解析成RGBA?值?的數(shù)組
?*
?*??@param?color?UIColor對象,有RGBA值?初始化的
?*[UIColor?colorWithRed:rValue?green:gValue?blue:bValue?alpha:aValue]
?*
?*??@return?包含RGBA值得數(shù)組[rValue,?gValue,?bValue,?aValue]
?*/
-?(NSArray?*)RGBA_WithColor:(UIColor?*)color?{
????NSString?*?colorStr?=?[NSString?stringWithFormat:@"%@",?color];
????//將RGB值描述分隔成字符串
????NSArray?*?colorValueArray?=?[colorStr?componentsSeparatedByString:@"?"];
????NSString?*?R?=?colorValueArray[1];
????NSString?*?G?=?colorValueArray[2];
????NSString?*?B?=?colorValueArray[3];
????NSString?*?A?=?colorValueArray[4];
????return?@[R,?G,?B,?A];
}

/**?畫圓?*/
-?(UIImage?*)drawCircle?{
????UIGraphicsBeginImageContext(CGSizeMake(_endRadius*2,?_endRadius*2));
????CGContextRef?context?=?UIGraphicsGetCurrentContext();
????CGContextMoveToPoint(context,?CenterX,?CenterY);
????CGContextSetFillColorWithColor(context,?_circleColor.CGColor);
????CGContextAddArc(context,?CenterX,?CenterY,?_endRadius,?0,?-2*M_PI,?1);
????CGContextFillPath(context);
????UIImage?*?img?=?UIGraphicsGetImageFromCurrentImageContext();
????UIGraphicsEndImageContext();
????return?img;
}

-?(void)diffuseAnimation?{
????UIImageView?*?imgView?=?[[UIImageView?alloc]?init];
????imgView.image?=?[self?drawCircle];
????imgView.frame?=?CGRectMake(0,?0,?_startRadius,?_startRadius);
????imgView.center?=?CGPointMake(CenterX,?CenterY);
????[self?addSubview:imgView];
????
????[UIView?animateWithDuration:2?delay:0?options:UIViewAnimationOptionCurveEaseIn?animations:^{
????????imgView.frame?=?CGRectMake(0,?0,?_endRadius*2,?_endRadius*2);
????????imgView.center?=?CGPointMake(CenterX,?CenterY);
????????imgView.alpha?=?0;
????}?completion:^(BOOL?finished)?{
????????[imgView?removeFromSuperview];
????}];
}

@end


3. ViewController.m 中使用的代碼:


#import?"ViewController.h"
#import?"RadarView.h"

@interface?ViewController?()

@property?(nonatomic,?strong)?RadarView?*?scanRadarView;
@property?(nonatomic,?strong)?RadarView?*?diffuseRadarView;

@end

@implementation?ViewController

-?(void)viewDidLoad?{
????[super?viewDidLoad];
????/**?掃描?類型?RadarView?*/
//????_scanRadarView?=?[RadarView?scanRadarViewWithRadius:self.view.bounds.size.width*0.5?angle:400?radarLineNum:5?hollowRadius:0];
????
????/**?擴散?類型?RadarView?*/
????_diffuseRadarView?=?[RadarView?diffuseRadarViewWithStartRadius:7?endRadius:self.view.bounds.size.width*0.5?circleColor:[UIColor?whiteColor]];
}

-?(void)viewDidAppear:(BOOL)animated?{
????[super?viewDidAppear:animated];
????
//????[_scanRadarView?showTargetView:self.view];
//????[_scanRadarView?startAnimatian];
????
????[_diffuseRadarView?showTargetView:self.view];
????[_diffuseRadarView?startAnimatian];
}

-?(void)didReceiveMemoryWarning?{
????[super?didReceiveMemoryWarning];
????//?Dispose?of?any?resources?that?can?be?recreated.
}

@end


現(xiàn)在定義的是能代碼加載使用,等有空了,再封裝一些方法能在Storyboard中直接使用。

本站聲明: 本文章由作者或相關(guān)機構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

LED驅(qū)動電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關(guān)鍵字: 驅(qū)動電源

在工業(yè)自動化蓬勃發(fā)展的當下,工業(yè)電機作為核心動力設(shè)備,其驅(qū)動電源的性能直接關(guān)系到整個系統(tǒng)的穩(wěn)定性和可靠性。其中,反電動勢抑制與過流保護是驅(qū)動電源設(shè)計中至關(guān)重要的兩個環(huán)節(jié),集成化方案的設(shè)計成為提升電機驅(qū)動性能的關(guān)鍵。

關(guān)鍵字: 工業(yè)電機 驅(qū)動電源

LED 驅(qū)動電源作為 LED 照明系統(tǒng)的 “心臟”,其穩(wěn)定性直接決定了整個照明設(shè)備的使用壽命。然而,在實際應(yīng)用中,LED 驅(qū)動電源易損壞的問題卻十分常見,不僅增加了維護成本,還影響了用戶體驗。要解決這一問題,需從設(shè)計、生...

關(guān)鍵字: 驅(qū)動電源 照明系統(tǒng) 散熱

根據(jù)LED驅(qū)動電源的公式,電感內(nèi)電流波動大小和電感值成反比,輸出紋波和輸出電容值成反比。所以加大電感值和輸出電容值可以減小紋波。

關(guān)鍵字: LED 設(shè)計 驅(qū)動電源

電動汽車(EV)作為新能源汽車的重要代表,正逐漸成為全球汽車產(chǎn)業(yè)的重要發(fā)展方向。電動汽車的核心技術(shù)之一是電機驅(qū)動控制系統(tǒng),而絕緣柵雙極型晶體管(IGBT)作為電機驅(qū)動系統(tǒng)中的關(guān)鍵元件,其性能直接影響到電動汽車的動力性能和...

關(guān)鍵字: 電動汽車 新能源 驅(qū)動電源

在現(xiàn)代城市建設(shè)中,街道及停車場照明作為基礎(chǔ)設(shè)施的重要組成部分,其質(zhì)量和效率直接關(guān)系到城市的公共安全、居民生活質(zhì)量和能源利用效率。隨著科技的進步,高亮度白光發(fā)光二極管(LED)因其獨特的優(yōu)勢逐漸取代傳統(tǒng)光源,成為大功率區(qū)域...

關(guān)鍵字: 發(fā)光二極管 驅(qū)動電源 LED

LED通用照明設(shè)計工程師會遇到許多挑戰(zhàn),如功率密度、功率因數(shù)校正(PFC)、空間受限和可靠性等。

關(guān)鍵字: LED 驅(qū)動電源 功率因數(shù)校正

在LED照明技術(shù)日益普及的今天,LED驅(qū)動電源的電磁干擾(EMI)問題成為了一個不可忽視的挑戰(zhàn)。電磁干擾不僅會影響LED燈具的正常工作,還可能對周圍電子設(shè)備造成不利影響,甚至引發(fā)系統(tǒng)故障。因此,采取有效的硬件措施來解決L...

關(guān)鍵字: LED照明技術(shù) 電磁干擾 驅(qū)動電源

開關(guān)電源具有效率高的特性,而且開關(guān)電源的變壓器體積比串聯(lián)穩(wěn)壓型電源的要小得多,電源電路比較整潔,整機重量也有所下降,所以,現(xiàn)在的LED驅(qū)動電源

關(guān)鍵字: LED 驅(qū)動電源 開關(guān)電源

LED驅(qū)動電源是把電源供應(yīng)轉(zhuǎn)換為特定的電壓電流以驅(qū)動LED發(fā)光的電壓轉(zhuǎn)換器,通常情況下:LED驅(qū)動電源的輸入包括高壓工頻交流(即市電)、低壓直流、高壓直流、低壓高頻交流(如電子變壓器的輸出)等。

關(guān)鍵字: LED 隧道燈 驅(qū)動電源
關(guān)閉