引導(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