项目需要引导图,简单介绍实现记录一下
问题:IOS自定义如何添加引导遮罩?
解决:思路为当前需要显示遮罩的View,添加一层半透明遮罩类似于蒙版(PS中常用),在需要引导的控件上,勾勒出镂空效果,并在上,下,左右不分根据自己需要,显示文字效果。
注意:如需添加图片效果,了解大体思路应该不难。
-(void)showGuidView{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if(![[defaults objectForKey:@"showGuidViewStatues"] isEqualToString:@"1"]){
[self setGuidViewTextView:self.directionGradleView andContent:@"xxxxx,点击屏幕下一项" andTag:1001 andOffsetUp:YES];
}
}
-(void)setGuidViewTextView:(UIView*)attachView andContent:(NSString*)content andTag:(int)tag andOffsetUp:(BOOL) upOffset{
//创建需要显示遮罩空间路径
UIBezierPath *tempPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(attachView.frame.origin.x, attachView.frame.origin.y, attachView.bounds.size.width, attachView.bounds.size.height) byRoundingCorners:(UIRectCornerTopLeft |UIRectCornerTopRight |UIRectCornerBottomRight|UIRectCornerBottomLeft) cornerRadii:CGSizeMake(4, 4)];
//获取手机屏幕大小并填充背景颜色,半透明
UIView *guideView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
guideView.backgroundColor = [UIColor blackColor];
guideView.alpha = 0.6;
guideView.layer.mask = [self addTransparencyViewWith:tempPath];
//用来区分每个引导标签,后续删除使用
guideView.tag =tag;
//控件文字显示
UITextView *textView = nil;
// 文字内容
if(upOffset){
if(tag == 1002){
textView = [[UITextView alloc] initWithFrame:CGRectMake(attachView.frame.origin.x-attachView.bounds.size.width, attachView.frame.origin.y-50, guideView.bounds.size.width, attachView.bounds.size.height)];
}else{
textView = [[UITextView alloc] initWithFrame:CGRectMake(attachView.frame.origin.x, attachView.frame.origin.y-50, guideView.bounds.size.width, attachView.bounds.size.height)];
}
}else{
textView = [[UITextView alloc] initWithFrame:CGRectMake(attachView.frame.origin.x, attachView.frame.origin.y+50, guideView.bounds.size.width/2, attachView.bounds.size.height*2)];
}
textView.backgroundColor = UIColor.clearColor;
textView.selectable = NO;
textView.font =[UIFont fontWithName:@"System" size:16];
textView.font = [UIFont boldSystemFontOfSize:16];
textView.textColor = UIColor.whiteColor;
textView.text = content;
[guideView addSubview:textView];
//显示引导页面
[[UIApplication sharedApplication].keyWindow addSubview:guideView];
//为引导页面添加点击事件,方便更换下一个
UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(GuiViewEvent:)];
//设置需要连续点击几次才响应,默认点击1次
[tapGesture setNumberOfTapsRequired:1];
[guideView addGestureRecognizer:tapGesture];
}
/**
引导页面点击事件
*/
-(void)GuiViewEvent:(UITapGestureRecognizer *)gesture
{
//根据自定TAG 检查并删除上一个引导页面
if([[UIApplication sharedApplication].keyWindow viewWithTag:1001] != nil){
//处理事件
[[[UIApplication sharedApplication].keyWindow viewWithTag:1001]removeFromSuperview];
NSString* guidNextStr =@"xxxxxxx,点击屏幕下一项";
[self setGuidViewTextView:self.throttleGradleView andContent:guidNextStr andTag:1002 andOffsetUp:YES];
}else if([[UIApplication sharedApplication].keyWindow viewWithTag:1002] != nil){
//处理事件
[[[UIApplication sharedApplication].keyWindow viewWithTag:1002]removeFromSuperview];
NSString* guidNextStr=@"xxxxxxx点击屏幕下一项";
[self setGuidViewTextView:self.settingBtn andContent: guidNextStr andTag:1003 andOffsetUp:NO];
}else if([[UIApplication sharedApplication].keyWindow viewWithTag:1003] != nil){
//处理事件
[[[UIApplication sharedApplication].keyWindow viewWithTag:1003]removeFromSuperview];
NSString* guidNextStr=@"xxxxxxxxx,点击屏幕开始游戏";
[self setGuidViewTextView:self.onHawkeyeBtn andContent: guidNextStr andTag:1004 andOffsetUp:NO];
}else if([[UIApplication sharedApplication].keyWindow viewWithTag:1004] != nil){
[[[UIApplication sharedApplication].keyWindow viewWithTag:1004]removeFromSuperview];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:@"1" forKey:@"showGuidViewStatues"];
[defaults synchronize];
}
}
//返回引导依附View区域
- (CAShapeLayer *)addTransparencyViewWith:(UIBezierPath *)tempPath{
UIBezierPath *path = [UIBezierPath bezierPathWithRect:[UIScreen mainScreen].bounds];
[path appendPath:tempPath];
path.usesEvenOddFillRule = YES;
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.path = path.CGPath;
// shapeLayer.fillColor= [UIColor blackColor].CGColor; //其他颜色都可以,只要不是透明的
// shapeLayer.fillColor =[UIColor clearColor].CGColor;
shapeLayer.fillRule=kCAFillRuleEvenOdd;
return shapeLayer;
}