在产品的迭代过程中,当产品引入新功能的时候,往往希望能够给新功能加上引导视图,让该功能更醒目,让用户更容易感知到,当然有时也是为了给该功能添加相应的解释说明等等。具体样式类似于下图中的展示:
其中红色部分是需要添加引导的功能视图,我们需要在这个视图的下面加上一个引导视图,也就是白色字体和箭头部分,让这个地方更醒目。具体实现方法如下:
- (void)viewDidLoad {
[super viewDidLoad];
// 这个是功能视图,是我们需要引导的视图。
self.testView = [[UILabel alloc] initWithFrame:CGRectMake(10, 200, [UIScreen mainScreen].bounds.size.width - 20, 50)];
self.testView.backgroundColor = [UIColor whiteColor];
self.testView.layer.borderWidth = 1.0f;
self.testView.layer.borderColor = [UIColor redColor].CGColor;
self.testView.font = [UIFont systemFontOfSize:14.0f];
self.testView.text = @"这是目标视图,我们需要加上引导视图,引导用户注意视图上面的功能";
self.testView.numberOfLines = 0;
self.testView.textColor = [[UIColor redColor] colorWithAlphaComponent:0.6];
[self.view addSubview:self.testView];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self addGuidView];
}
- (void)addGuidView
{
//添加引导视图,在试图上加上一层黑色蒙层,但是要把我们的目标视图抠出来,不能被蒙层遮住
UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
//START - 实现引导功能的核心代码
//获取目标视图在window坐标系的frame
CGRect rect = [self.view convertRect:self.testView.frame toView:[UIApplication sharedApplication].keyWindow];
UIBezierPath *all_path = [UIBezierPath bezierPathWithRect:self.view.bounds];
UIBezierPath *path = [UIBezierPath bezierPathWithRect:rect];
[all_path appendPath:path];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.fillRule = kCAFillRuleEvenOdd;
maskLayer.path = all_path.CGPath;
view.layer.mask = maskLayer;
//END - 实现引导功能的核心代码
UIImageView *imageView1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guide_teach_root_detail"]];
[imageView1 sizeToFit];
CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
imageView1.frame = CGRectMake((screenW - CGRectGetWidth(imageView1.bounds))/2, CGRectGetMaxY(rect), CGRectGetWidth(imageView1.bounds), CGRectGetHeight(imageView1.bounds));
[view addSubview:imageView1];
[[UIApplication sharedApplication].keyWindow addSubview:view];
}
可加群一起交流共同学习:801216530。