代碼實(shí)現(xiàn)引導(dǎo)頁(yè)遮罩(1)---繪制箭頭算法

引導(dǎo)頁(yè)基本是每個(gè)項(xiàng)目都需要的,比如上個(gè)新功能需要引導(dǎo)用戶點(diǎn)擊某個(gè)按鈕進(jìn)入某個(gè)頁(yè)面再點(diǎn)擊某個(gè)按鈕啥的量淌。我們一般做法都是通過(guò)一組圖片來(lái)完成這些的塞俱?我以前也是這樣做的,但是總覺(jué)的不夠好赶舆,1.每個(gè)分辨率要有一套圖,而且這種圖基本都是全屏圖祭饭,如果寬高比不完全一致一點(diǎn)也不能將就的,每套圖片都是不小叙量。2.不是真實(shí)界面一些動(dòng)態(tài)數(shù)據(jù)肯定會(huì)不那么一致倡蝙。所以就想著用真實(shí)界面,在上面繪制遮罩引導(dǎo)绞佩。就有了下面的2個(gè)類了寺鸥。一個(gè)是箭頭,一個(gè)是繪制高亮可點(diǎn)擊的那部分品山。


新建HJArrowView類胆建,代碼如下:

/*

畫虛線箭頭和實(shí)線箭頭

傳入?yún)?shù)(起點(diǎn)坐標(biāo),終點(diǎn)坐標(biāo)肘交,父view)

案例:

HJArrowView *arrowView=[[HJArrowView alloc]initWithFrame:_view.frame];

arrowView.startP=CGPointMake(180, 150);

arrowView.endP=CGPointMake(280, 50);

[arrowView drawLine:_view];

2016年3月1號(hào)jing

*/

#import

typedefNS_ENUM(NSInteger, HJArrowType)

{

HJArrowTypeDash,//虛線

HJArrowTypeLine//實(shí)線

};

@interfaceHJArrowView :UIView

@property(nonatomic,assign)CGPointstartP;//起點(diǎn)坐標(biāo)

@property(nonatomic,assign)CGPointendP;//終點(diǎn)坐標(biāo)

@property(nonatomic,assign)HJArrowTypearrowType;

-(void)drawDash:(UIView*)_subView;//畫虛線

-(void)drawLine:(UIView*)_subView;//畫實(shí)線

@property(nonatomic,assign)CGPointimgPoint;//圖片坐標(biāo)

@property(nonatomic,assign)UIImageOrientationorientation;//圖片角度

-(void)imageArrow:(UIView*)_subView;//圖片箭頭

@end

#import"HJArrowView.h"

@interfaceHJArrowView()

@property(assign)CGPointleftPoint;

@property(assign)CGPointrightPoint;

@end

@implementationHJArrowView

#define pi3.14159265358979323846

#define degreesToRadian(x) (pi * x /180.0)

#define radiansToDegrees(x) (180.0* x / pi)

#define radian45//張開的角度

#define radius15//半徑也就是箭頭的長(zhǎng)度

- (instancetype)initWithFrame:(CGRect)frame

{

self= [superinitWithFrame:frame];

if(self) {

[selfsetup];

}

returnself;

}

- (void)setup

{

self.backgroundColor= [UIColorclearColor];

[selfsetUserInteractionEnabled:NO];

}

-(void)dealloc{

NSLog(@"===HJArrowView===dealloc===");

}

// Only override drawRect: if you perform custom drawing.

// An empty implementation adversely affects performance during animation.

- (void)drawRect:(CGRect)rect {

// Drawing code

CGContextRefcontext =UIGraphicsGetCurrentContext();

if(context ==nil) {

return;

}

//[[[UIColor redColor] colorWithAlphaComponent:0.5f] setFill];

//UIRectFill(rect);

if(self.arrowType==HJArrowTypeDash){

CGContextBeginPath(context);

CGContextSetLineWidth(context,1.0);

CGContextSetStrokeColorWithColor(context, [UIColorwhiteColor].CGColor);

CGFloatlengths[] = {10,10};

CGContextSetLineDash(context,0, lengths,2);

CGContextMoveToPoint(context,self.startP.x,self.startP.y);

CGContextAddLineToPoint(context,self.endP.x,self.endP.y);

CGContextMoveToPoint(context,self.endP.x,self.endP.y);//起點(diǎn)坐標(biāo)

CGContextAddLineToPoint(context,self.rightPoint.x,self.rightPoint.y);//終點(diǎn)坐標(biāo)

CGContextMoveToPoint(context,self.endP.x,self.endP.y);//起點(diǎn)坐標(biāo)

CGContextAddLineToPoint(context,self.leftPoint.x,self.leftPoint.y);//終點(diǎn)坐標(biāo)

//CGContextStrokePath(context);

CGContextClosePath(context);

CGContextDrawPath(context,kCGPathFillStroke);

}elseif(self.arrowType==HJArrowTypeLine){

CGContextSetLineCap(context,kCGLineCapRound);

CGContextSetLineWidth(context,1.0);//線寬

CGContextSetAllowsAntialiasing(context,true);

CGContextSetStrokeColorWithColor(context,[UIColorwhiteColor].CGColor);//線的顏色

CGContextBeginPath(context);

CGContextMoveToPoint(context,self.startP.x,self.startP.y);//起點(diǎn)坐標(biāo)

CGContextAddLineToPoint(context,self.endP.x,self.endP.y);//終點(diǎn)坐標(biāo)

CGContextMoveToPoint(context,self.endP.x,self.endP.y);//起點(diǎn)坐標(biāo)

CGContextAddLineToPoint(context,self.rightPoint.x,self.rightPoint.y);//終點(diǎn)坐標(biāo)

CGContextMoveToPoint(context,self.endP.x,self.endP.y);//起點(diǎn)坐標(biāo)

CGContextAddLineToPoint(context,self.leftPoint.x,self.leftPoint.y);//終點(diǎn)坐標(biāo)

//CGContextStrokePath(context);

CGContextClosePath(context);

CGContextDrawPath(context,kCGPathFillStroke);

}

}

#pragma mark //坐標(biāo)計(jì)算

-(CGPoint)getArrowLeftPoint:(CGFloat)_radian{

CGPointpoint=CGPointMake(self.endP.x+radius*cos(degreesToRadian(-(radian+_radian))),self.endP.y+radius*sin(degreesToRadian(-(radian+_radian))));

returnpoint;

}

-(CGPoint)getArrowRightPoint:(CGFloat)_radian{

CGPointpoint=CGPointMake(self.endP.x+radius*cos(degreesToRadian((radian-_radian))),self.endP.y+radius*sin(degreesToRadian((radian-_radian))));

returnpoint;

}

//計(jì)算角度

-(CGFloat)angleBetweenPoints{

CGFloatheight =self.endP.y-self.startP.y;

CGFloatwidth =self.startP.x-self.endP.x;

CGFloatrads =atan(height/width);

CGFloatradians=radiansToDegrees(rads);

if(self.startP.x

radians=180+radians;

}

returnradians;

//degs = degrees(atan((top - bottom)/(right - left)))

}

//獲取箭頭的左右點(diǎn)

-(void)getArrowPoint{

CGFloatloc_radian=[selfangleBetweenPoints];//計(jì)算線的角度

self.leftPoint=[selfgetArrowLeftPoint:loc_radian];

self.rightPoint=[selfgetArrowRightPoint:loc_radian];

}

#pragma mark //public

//畫虛線

-(void)drawDash:(UIView*)_subView{

self.arrowType=HJArrowTypeDash;

[selfgetArrowPoint];

[_subViewaddSubview:self];

}

//畫實(shí)線

-(void)drawLine:(UIView*)_subView{

self.arrowType=HJArrowTypeLine;

[selfgetArrowPoint];

[_subViewaddSubview:self];

}

//圖片箭頭

-(void)imageArrow:(UIView*)_subView{

UIImageView*loc_imgView=[[UIImageViewalloc]initWithFrame:CGRectMake(self.imgPoint.x,self.imgPoint.y,108,112)];

//UIImage *loc_image=[UIImage imageWithCGImage:[UIImage imageNamed:@"ydArrow"].CGImage scale:1 orientation:self.orientation];

[loc_imgViewsetImage:[UIImageimageNamed:@"ydArrow"]];

[selfaddSubview:loc_imgView];

[_subViewaddSubview:self];

//旋轉(zhuǎn)角度

//loc_imgView.transform=CGAffineTransformMakeRotation();

//翻轉(zhuǎn)

if(self.orientation==UIImageOrientationRight){

loc_imgView.transform=CGAffineTransformMakeScale(-1.0,1.0);

}elseif(self.orientation==UIImageOrientationLeft){

loc_imgView.transform=CGAffineTransformMakeScale(-1.0,-1.0);

}elseif(self.orientation==UIImageOrientationDown){

loc_imgView.transform=CGAffineTransformMakeScale(1.0,-1.0);

}

}

//UIImage旋轉(zhuǎn)----目前沒(méi)有用

+ (UIImage*)image:(UIImage*)image rotation:(UIImageOrientation)orientation

{

longdoublerotate =0.0;

CGRectrect;

floattranslateX =0;

floattranslateY =0;

floatscaleX =1.0;

floatscaleY =1.0;

switch(orientation) {

caseUIImageOrientationLeft:

rotate =M_PI_2;

rect =CGRectMake(0,0, image.size.height, image.size.width);

translateX =0;

translateY = -rect.size.width;

scaleY = rect.size.width/rect.size.height;

scaleX = rect.size.height/rect.size.width;

break;

caseUIImageOrientationRight:

rotate =3*M_PI_2;

rect =CGRectMake(0,0, image.size.height, image.size.width);

translateX = -rect.size.height;

translateY =0;

scaleY = rect.size.width/rect.size.height;

scaleX = rect.size.height/rect.size.width;

break;

caseUIImageOrientationDown:

rotate =M_PI;

rect =CGRectMake(0,0, image.size.width, image.size.height);

translateX = -rect.size.width;

translateY = -rect.size.height;

break;

default:

rotate =0.0;

rect =CGRectMake(0,0, image.size.width, image.size.height);

translateX =0;

translateY =0;

break;

}

UIGraphicsBeginImageContext(rect.size);

CGContextRefcontext =UIGraphicsGetCurrentContext();

//做CTM變換

CGContextTranslateCTM(context,0.0, rect.size.height);

CGContextScaleCTM(context,1.0, -1.0);

CGContextRotateCTM(context, rotate);

CGContextTranslateCTM(context, translateX, translateY);

CGContextScaleCTM(context, scaleX, scaleY);

//繪制圖片

CGContextDrawImage(context,CGRectMake(0,0, rect.size.width, rect.size.height), image.CGImage);

UIImage*newPic =UIGraphicsGetImageFromCurrentImageContext();

returnnewPic;

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末笆载,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子涯呻,更是在濱河造成了極大的恐慌凉驻,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,509評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件复罐,死亡現(xiàn)場(chǎng)離奇詭異涝登,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)效诅,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門胀滚,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人乱投,你說(shuō)我怎么就攤上這事咽笼。” “怎么了戚炫?”我有些...
    開封第一講書人閱讀 163,875評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵褐荷,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我嘹悼,道長(zhǎng)叛甫,這世上最難降的妖魔是什么层宫? 我笑而不...
    開封第一講書人閱讀 58,441評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮其监,結(jié)果婚禮上萌腿,老公的妹妹穿的比我還像新娘。我一直安慰自己抖苦,他們只是感情好毁菱,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著锌历,像睡著了一般贮庞。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上究西,一...
    開封第一講書人閱讀 51,365評(píng)論 1 302
  • 那天窗慎,我揣著相機(jī)與錄音,去河邊找鬼卤材。 笑死遮斥,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的扇丛。 我是一名探鬼主播术吗,決...
    沈念sama閱讀 40,190評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼帆精!你這毒婦竟也來(lái)了较屿?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,062評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤卓练,失蹤者是張志新(化名)和其女友劉穎吝镣,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體昆庇,經(jīng)...
    沈念sama閱讀 45,500評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡末贾,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評(píng)論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了整吆。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片拱撵。...
    茶點(diǎn)故事閱讀 39,834評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖表蝙,靈堂內(nèi)的尸體忽然破棺而出拴测,到底是詐尸還是另有隱情,我是刑警寧澤府蛇,帶...
    沈念sama閱讀 35,559評(píng)論 5 345
  • 正文 年R本政府宣布集索,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏务荆。R本人自食惡果不足惜妆距,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望函匕。 院中可真熱鬧娱据,春花似錦、人聲如沸盅惜。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)抒寂。三九已至结啼,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間屈芜,已是汗流浹背郊愧。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留沸伏,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,958評(píng)論 2 370
  • 正文 我出身青樓动分,卻偏偏與公主長(zhǎng)得像毅糟,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子澜公,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容