代碼實(shí)現(xiàn)引導(dǎo)頁遮罩(2)

新建HJHoledView類犹芹,代碼如下:

/*

繪制遮罩可以畫(圓形,矩形,圓角矩形,自定義視圖)

添加箭頭和提示文字

案例:

//添加遮罩指示

self.holedView = [[HJHoledView alloc]initWithFrame:self.view.frame];

[self.holedView setDimingColor: [[UIColor redColor] colorWithAlphaComponent:0.75f]];

//self.holedView.holeViewDelegate = self;

[self.view addSubview:_holedView];

//按鈕-圓角矩形

[self.holedView addHoleRoundedRectOnRect:_myButton.frame withCornerRadius:_myButton.frame.size.width/2];

[self.holedView addFocusView:_myButton];

//圓形

[self.holedView addHoleCircleCenteredOnPosition:CGPointMake(190, 110) andDiameter:130];

[self.holedView addHoleCircleCenteredOnPosition:CGPointMake(190, 310) andDiameter:130];

//文字說明

//[self.holedView addHCustomView:[self viewForDemo] onRect:CGRectMake(20.0f, 250.0f, 200.0f, 50.0f)];

[self.holedView addPromptLabelText:@"哈哈哈哈哈哈哈哈" onRect:CGRectMake(20.0f, 150.0f, 200.0f, 50.0f)];

[self.holedView addPromptLabelText:@"嘻嘻嘻嘻嘻嘻嘻" onRect:CGRectMake(20.0f, 200.0f, 200.0f, 50.0f)];

//添加箭頭

[self.holedView addArrowView:CGPointMake(100, 150) endPoint:CGPointMake(150, 100)];

[self.holedView addArrowView:CGPointMake(100, 250) endPoint:CGPointMake(150, 300)];

2016年3月1號jing

*/

#import

#import"RTLabel.h"

typedefNS_ENUM(NSInteger, HJHoleType)

{

HJHoleTypeCirle,

HJHoleTypeRect,

HJHoleTypeRoundedRect,

HJHoleTypeCustomRect

};

@classHJHoledView;

@protocolHJHoledViewDelegate

@optional

-(void)animationViewRemove:(HJHoledView*)_animationView;

-(void)animationDidOver:(HJHoledView*)_animationView;

-(void)exitButtonAction:(HJHoledView*)_animationView;

@end

@interfaceHJHoledView :UIView{

__weakid_delegate;

}

@property(weak)id delegate;

@property(strong,nonatomic)UIColor*dimingColor;//顏色默認(rèn)黑色半透明

- (NSInteger)addHoleCircleCenteredOnPosition:(CGPoint)centerPoint andDiameter:(CGFloat)diamter;

- (NSInteger)addHoleRectOnRect:(CGRect)rect;

- (NSInteger)addHoleRoundedRectOnRect:(CGRect)rect withCornerRadius:(CGFloat)cornerRadius;

- (NSInteger)addHCustomView:(UIView*)customView onRect:(CGRect)rect;//自定義視圖

- (UILabel*)addPromptLabelText:(NSString*)_textStr onRect:(CGRect)rect;//提示文字返回UILabel方便修改字體大小顏色等

-(void)addArrowView:(CGPoint)_startP endPoint:(CGPoint)_endP;//添加箭頭

-(void)addImgArrowView:(CGPoint)_point rotation:(UIImageOrientation)_orientation;//添加圖片箭頭

- (void)addFocusView:(UIView*)focus;//添加聚焦按鈕之類的

- (void)removeHoles:(BOOL)_isAnimation;

-(void)addExitBtn;//默認(rèn)位置右上角

-(void)addExitBtn:(CGRect)rect;

@end

//

//HJHoledView.h

//HoledViewTest

//

//Created by hsmob on 16/2/29.

//Copyright ? 2016年WeiMob. All rights reserved.

//

#import"HJHoledView.h"

#import"HJArrowView.h"

#pragma mark - holes objects

@interfaceHJHole :NSObject

@property(assign)HJHoleTypeholeType;

@end

@implementationHJHole

@end

@interfaceHJCircleHole :HJHole

@property(assign)CGPointholeCenterPoint;

@property(assign)CGFloatholeDiameter;

@end

@implementationHJCircleHole

@end

@interfaceHJRectHole :HJHole

@property(assign)CGRectholeRect;

@end

@implementationHJRectHole

@end

@interfaceHJRoundedRectHole :HJRectHole

@property(assign)CGFloatholeCornerRadius;

@end

@implementationHJRoundedRectHole

@end

@interfaceHJCustomRectHole :HJRectHole

@property(strong)UIView*customView;

@end

@implementationHJCustomRectHole

@end

@interfaceHJHoledView()

@property(strong,nonatomic)NSMutableArray*holes;//高亮區(qū)域數(shù)組

@property(strong,nonatomic)NSMutableArray*focusView;//點(diǎn)擊區(qū)域數(shù)組

@end

@implementationHJHoledView

#pragma mark - LifeCycle

- (void)awakeFromNib

{

[superawakeFromNib];

[selfsetup];

}

- (instancetype)initWithCoder:(NSCoder*)aDecoder

{

self= [superinitWithCoder:aDecoder];

if(self) {

[selfsetup];

}

returnself;

}

- (instancetype)initWithFrame:(CGRect)frame

{

self= [superinitWithFrame:frame];

if(self) {

[selfsetup];

[selfshowAnimation];

}

returnself;

}

- (void)setup

{

_holes= [NSMutableArraynew];

_focusView= [NSMutableArraynew];

self.backgroundColor= [UIColorclearColor];

_dimingColor= [[UIColorblackColor]colorWithAlphaComponent:0.75f];

}

-(void)addExitBtn{

floatwidth=40;

floatx=ScreenWidth-width-20;

floaty=20;

CGRectrect=CGRectMake(x,y, width,35);

[selfaddExitBtn:rect];

}

-(void)addExitBtn:(CGRect)rect{

UIButton*exitButton=[[UIButtonalloc]initWithFrame:rect];

[exitButtonsetBackgroundColor:[UIColorcolorWithRed:0.011green:0.000blue:0.000alpha:.000]];

[exitButtonsetTitle:@"退出"forState:UIControlStateNormal];

[exitButtonaddTarget:selfaction:@selector(exitButtonAction:)forControlEvents:UIControlEventTouchUpInside];

[selfaddSubview:exitButton];

}

-(void)exitButtonAction:(UIButton*)exitButton{

HJLog(@"退出按鈕事件---");

if([_delegaterespondsToSelector:@selector(exitButtonAction:)]) {

[_delegateexitButtonAction:self];

}

}

-(void)dealloc{

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

}

#pragma mark - UIView Overrides

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event

{

UIView*hitView = [superhitTest:pointwithEvent:event];

HJLog(@"===hitTest===");

if(hitView ==self)

{

for(HJHole* holeinself.holes) {

if(hole.holeType==HJHoleTypeRoundedRect) {

HJRoundedRectHole*rectHole = (HJRoundedRectHole*)hole;

if(CGRectContainsPoint(rectHole.holeRect, point)){

HJLog(@"===點(diǎn)擊到1===");

returnnil;

}

}elseif(hole.holeType==HJHoleTypeRect) {

HJRectHole*rectHole = (HJRectHole*)hole;

if(CGRectContainsPoint(rectHole.holeRect, point)){

HJLog(@"===點(diǎn)擊到2===");

returnnil;

}

}elseif(hole.holeType==HJHoleTypeCirle) {

HJCircleHole*circleHole = (HJCircleHole*)hole;

CGRectrectInView =CGRectMake(floorf(circleHole.holeCenterPoint.x- circleHole.holeDiameter*0.5f),

floorf(circleHole.holeCenterPoint.y- circleHole.holeDiameter*0.5f),

circleHole.holeDiameter,

circleHole.holeDiameter);

if(CGRectContainsPoint(rectInView, point)){

HJLog(@"===點(diǎn)擊到3===");

returnnil;

}

}

}

//for (UIView *focus in self.focusView) {

//if (CGRectContainsPoint(focus.frame, point))

//{HJLog(@"focus.frame:%f,point:%f",focus.frame.size.height,point.y);

//return focus;

//}

//}

}

returnhitView;

}

- (void)drawRect:(CGRect)rect

{

[selfremoveCustomViews];

CGContextRefcontext =UIGraphicsGetCurrentContext();

if(context ==nil) {

return;

}

[self.dimingColorsetFill];

UIRectFill(rect);

for(HJHole* holeinself.holes) {

[[UIColorclearColor]setFill];

if(hole.holeType==HJHoleTypeRoundedRect) {

HJRoundedRectHole*rectHole = (HJRoundedRectHole*)hole;

CGRectholeRectIntersection =CGRectIntersection( rectHole.holeRect,self.frame);

UIBezierPath*bezierPath = [UIBezierPathbezierPathWithRoundedRect:holeRectIntersection

cornerRadius:rectHole.holeCornerRadius];

CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), [[UIColorclearColor]CGColor]);

CGContextAddPath(UIGraphicsGetCurrentContext(), bezierPath.CGPath);

CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeClear);

CGContextFillPath(UIGraphicsGetCurrentContext());

}elseif(hole.holeType==HJHoleTypeRect) {

HJRectHole*rectHole = (HJRectHole*)hole;

CGRectholeRectIntersection =CGRectIntersection( rectHole.holeRect,self.frame);

UIRectFill( holeRectIntersection );

}elseif(hole.holeType==HJHoleTypeCirle) {

HJCircleHole*circleHole = (HJCircleHole*)hole;

CGRectrectInView =CGRectMake(floorf(circleHole.holeCenterPoint.x- circleHole.holeDiameter*0.5f),

floorf(circleHole.holeCenterPoint.y- circleHole.holeDiameter*0.5f),

circleHole.holeDiameter,

circleHole.holeDiameter);

CGContextSetFillColorWithColor( context, [UIColorclearColor].CGColor);

CGContextSetBlendMode(context,kCGBlendModeClear);

CGContextFillEllipseInRect( context, rectInView );

}

}

[selfaddCustomViews];

}

#pragma mark - Add methods

- (NSInteger)addHoleCircleCenteredOnPosition:(CGPoint)centerPoint andDiameter:(CGFloat)diameter

{

HJCircleHole*circleHole = [HJCircleHolenew];

circleHole.holeCenterPoint= centerPoint;

circleHole.holeDiameter= diameter;

circleHole.holeType=HJHoleTypeCirle;

[self.holesaddObject:circleHole];

[selfsetNeedsDisplay];

return[self.holesindexOfObject:circleHole];

}

- (NSInteger)addHoleRectOnRect:(CGRect)rect

{

HJRectHole*rectHole = [HJRectHolenew];

rectHole.holeRect= rect;

rectHole.holeType=HJHoleTypeRect;

[self.holesaddObject:rectHole];

[selfsetNeedsDisplay];

return[self.holesindexOfObject:rectHole];

}

- (NSInteger)addHoleRoundedRectOnRect:(CGRect)rect withCornerRadius:(CGFloat)cornerRadius

{

HJRoundedRectHole*rectHole = [HJRoundedRectHolenew];

rectHole.holeRect= rect;

rectHole.holeCornerRadius= cornerRadius;

rectHole.holeType=HJHoleTypeRoundedRect;

[self.holesaddObject:rectHole];

[selfsetNeedsDisplay];

return[self.holesindexOfObject:rectHole];

}

- (NSInteger)addHCustomView:(UIView*)customView onRect:(CGRect)rect

{

HJCustomRectHole*customHole = [HJCustomRectHolenew];

customHole.holeRect= rect;

customHole.customView= customView;

customHole.holeType=HJHoleTypeCustomRect;

[self.holesaddObject:customHole];

[selfsetNeedsDisplay];

return[self.holesindexOfObject:customHole];

}

- (void)addFocusView:(UIView*)focus

{

//[self.focusView addObject:focus];

[selfaddSubview:focus];

}

- (void)removeHoles:(BOOL)_isAnimation

{

if(_isAnimation){

//添加動畫

[selfdisappearAnimation];

}else{

[selfendAnimationFinish];

}

}

#pragma mark - Overided setter

- (void)setDimingColor:(UIColor*)dimingColor

{

_dimingColor= dimingColor;

[selfsetNeedsDisplay];

}

#pragma mark - Tap Gesture

- (NSUInteger)holeViewIndexForAtPoint:(CGPoint)touchLocation

{

__blockNSUIntegeridxToReturn =NSNotFound;

[self.holesenumerateObjectsUsingBlock:^(HJHole*hole,NSUIntegeridx,BOOL*stop) {

if(hole.holeType==HJHoleTypeRoundedRect||

hole.holeType==HJHoleTypeRect||

hole.holeType==HJHoleTypeCustomRect) {

HJRectHole*rectHole = (HJRectHole*)hole;

if(CGRectContainsPoint(rectHole.holeRect, touchLocation)) {

idxToReturn = idx;

*stop =YES;

}

}elseif(hole.holeType==HJHoleTypeCirle) {

HJCircleHole*circleHole = (HJCircleHole*)hole;

CGRectrectInView =CGRectMake(floorf(circleHole.holeCenterPoint.x- circleHole.holeDiameter*0.5f),

floorf(circleHole.holeCenterPoint.x- circleHole.holeDiameter*0.5f),

circleHole.holeDiameter,

circleHole.holeDiameter);

if(CGRectContainsPoint(rectInView, touchLocation)) {

idxToReturn = idx;

*stop =YES;

}

}

}];

returnidxToReturn;

}

#pragma mark - Custom Views

- (void)removeCustomViews

{

[self.holesenumerateObjectsUsingBlock:^(idobj,NSUIntegeridx,BOOL*stop) {

if([objisKindOfClass:[HJCustomRectHoleclass]]) {

HJCustomRectHole*hole = (HJCustomRectHole*)obj;

[hole.customViewremoveFromSuperview];

}

}];

}

- (void)addCustomViews

{

[self.holesenumerateObjectsUsingBlock:^(idobj,NSUIntegeridx,BOOL*stop) {

if([objisKindOfClass:[HJCustomRectHoleclass]]) {

HJCustomRectHole*hole = (HJCustomRectHole*)obj;

[hole.customViewsetFrame:hole.holeRect];

[selfaddSubview:hole.customView];

}

}];

}

#pragma mark Prompt label

//提示文字返回UILabel方便修改字體大小顏色等

- (UILabel*)addPromptLabelText:(NSString*)_textStr onRect:(CGRect)rect{

UILabel*promptLabel=[[UILabelalloc]initWithFrame:rect];

[promptLabelsetFont:[UIFontsystemFontOfSize:20]];

[promptLabelsetTextColor:[UIColorwhiteColor]];

[promptLabelsetTextAlignment:NSTextAlignmentCenter];

promptLabel.text=_textStr;

[selfaddSubview:promptLabel];

returnpromptLabel;

}

//添加箭頭

-(void)addArrowView:(CGPoint)_startP endPoint:(CGPoint)_endP{

HJArrowView*arrowView=[[HJArrowViewalloc]initWithFrame:self.frame];

arrowView.startP=_startP;

arrowView.endP=_endP;

[arrowViewdrawLine:self];

}

//添加圖片箭頭

-(void)addImgArrowView:(CGPoint)_point rotation:(UIImageOrientation)_orientation{

HJArrowView*arrowView=[[HJArrowViewalloc]initWithFrame:self.frame];

arrowView.imgPoint=_point;

arrowView.orientation=_orientation;

[arrowViewimageArrow:self];

}

#pragma mark動畫

-(void)showAnimation{

self.alpha=0;

[UIViewbeginAnimations:@"endAnimation"context:nil];

[UIViewsetAnimationBeginsFromCurrentState:YES];//保持當(dāng)前位置狀態(tài)

[UIViewsetAnimationDuration:0.5f];

[UIViewsetAnimationCurve:UIViewAnimationCurveLinear];

self.alpha=1;

[UIViewcommitAnimations];

}

-(void)disappearAnimation{

if(_delegate&& [_delegaterespondsToSelector:@selector(animationViewRemove:)]){

[_delegateanimationViewRemove:self];

}

[UIViewbeginAnimations:@"endAnimation"context:nil];

[UIViewsetAnimationBeginsFromCurrentState:YES];//保持當(dāng)前位置狀態(tài)

[UIViewsetAnimationDelegate:self];

[UIViewsetAnimationDidStopSelector:@selector(endAnimationFinish)];

[UIViewsetAnimationDuration:0.5f];

[UIViewsetAnimationCurve:UIViewAnimationCurveLinear];

self.alpha=0;

[UIViewcommitAnimations];

}

-(void)endAnimationFinish{

if([_delegaterespondsToSelector:@selector(animationDidOver:)]) {

[_delegateanimationDidOver:self];

}

[self.holesremoveAllObjects];

[self.focusViewremoveAllObjects];

[selfremoveCustomViews];

[selfsetNeedsDisplay];

[selfremoveFromSuperview];

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末度气,一起剝皮案震驚了整個(gè)濱河市拓诸,隨后出現(xiàn)的幾起案子短条,更是在濱河造成了極大的恐慌呼猪,老刑警劉巖捞高,帶你破解...
    沈念sama閱讀 217,509評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蹦锋,死亡現(xiàn)場離奇詭異炕置,居然都是意外死亡荣挨,警方通過查閱死者的電腦和手機(jī)男韧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來垦沉,“玉大人煌抒,你說我怎么就攤上這事〔薇叮” “怎么了寡壮?”我有些...
    開封第一講書人閱讀 163,875評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長讹弯。 經(jīng)常有香客問我况既,道長,這世上最難降的妖魔是什么组民? 我笑而不...
    開封第一講書人閱讀 58,441評論 1 293
  • 正文 為了忘掉前任棒仍,我火速辦了婚禮,結(jié)果婚禮上臭胜,老公的妹妹穿的比我還像新娘莫其。我一直安慰自己,他們只是感情好耸三,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評論 6 392
  • 文/花漫 我一把揭開白布乱陡。 她就那樣靜靜地躺著,像睡著了一般仪壮。 火紅的嫁衣襯著肌膚如雪憨颠。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,365評論 1 302
  • 那天积锅,我揣著相機(jī)與錄音爽彤,去河邊找鬼。 笑死缚陷,一個(gè)胖子當(dāng)著我的面吹牛适篙,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播蹬跃,決...
    沈念sama閱讀 40,190評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼匙瘪,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蝶缀?” 一聲冷哼從身側(cè)響起丹喻,我...
    開封第一講書人閱讀 39,062評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎翁都,沒想到半個(gè)月后碍论,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,500評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡柄慰,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評論 3 335
  • 正文 我和宋清朗相戀三年鳍悠,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了税娜。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,834評論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡藏研,死狀恐怖敬矩,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情蠢挡,我是刑警寧澤弧岳,帶...
    沈念sama閱讀 35,559評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站业踏,受9級特大地震影響禽炬,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜勤家,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評論 3 328
  • 文/蒙蒙 一腹尖、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧伐脖,春花似錦热幔、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,779評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至巫俺,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間肿男,已是汗流浹背介汹。 一陣腳步聲響...
    開封第一講書人閱讀 32,912評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留舶沛,地道東北人嘹承。 一個(gè)月前我還...
    沈念sama閱讀 47,958評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像如庭,于是被迫代替她去往敵國和親叹卷。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評論 2 354

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