当我们第一次使用一个APP的时候或者APP增加了新功能的时候,我们有时候会设置一些引导语,来告诉用户某个按钮或者某个模块是干什么用的,有什么功能,有什么特色。今天自己写了个demo,记录一下,以后如果项目中用到了,就只能copy了。好了,废话不多说了,先上图看看效果。
先贴上部分代码,说说思路吧。
先定义一个继承UIView的DCPopView,.h
#import <UIKit/UIKit.h>
typedef void(^onDismiss)(void);
typedef enum : NSUInteger {
TOP,
BOTTOM,
} DIRECTION;
@interface DCPopView : UIView
@property (nonatomic,copy) onDismiss block;
@property(nonatomic, assign) DIRECTION direction;
- (instancetype)initWithAlertContentString:(NSString *)contentStr withImage:(NSString *)image withSpecificUI:(UIView *)view;
@end
.m
@interface DCPopView ()
@property(nonatomic,strong)UIView *view;
@property(nonatomic,strong)UILabel *contentLab;
@property(nonatomic,copy)NSString *contentStr;
@property(nonatomic,copy)NSString *image;
@property(nonatomic,strong)UIImageView *arrowImgV;
@property (nonatomic,strong) UIView *backgroundView;
@end
-(instancetype)initWithAlertContentString:(NSString *)contentStr withImage:(NSString *)image withSpecificUI:(UIView *)view
{
if (self = [super init]) {
self.view = view;
self.contentStr = contentStr;
self.image = image;
[self setUpMainView];
}
return self;
}
设置UI布局
-(void)setUpMainView
{
self.backgroundView = [[UIView alloc]init];
self.backgroundView.backgroundColor = [UIColor lightGrayColor];
self.backgroundView.alpha = 0.3;
UITapGestureRecognizer *tapBackgroundView = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapBackgroundView)];
[self.backgroundView addGestureRecognizer:tapBackgroundView];
self.backgroundView.userInteractionEnabled = YES;
[[UIApplication sharedApplication].keyWindow addSubview:self.backgroundView];
self.arrowImgV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:self.image]];
[[UIApplication sharedApplication].keyWindow addSubview:self.arrowImgV];
self.contentLab = [[UILabel alloc]init];
self.contentLab.text = self.contentStr;
self.contentLab.numberOfLines = 0;
self.contentLab.textAlignment = NSTextAlignmentCenter;
self.contentLab.font = [UIFont systemFontOfSize:14];
self.contentLab.textColor = [UIColor whiteColor];
[self.arrowImgV addSubview:self.contentLab];
}
设置frame
-(void)layoutSubviews
{
[super layoutSubviews];
self.backgroundView.frame = [UIScreen mainScreen].bounds;
CGSize contentMaxSize = CGSizeMake(100, CGFLOAT_MAX);
NSDictionary *contentDic = @{NSFontAttributeName:[UIFont systemFontOfSize:16]};
CGSize contentSize = [self.contentStr boundingRectWithSize:contentMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:contentDic context:nil].size;
self.arrowImgV.size = CGSizeMake(contentSize.width + 30, contentSize.height + 30);
if (self.view.frame.origin.x < [UIScreen mainScreen].bounds.size.width * 0.5) {
if (self.direction == BOTTOM) {
_arrowImgV.x = self.view.centerX + self.view.width/2 - self.arrowImgV.width;
}else if (self.direction == TOP){
_arrowImgV.x = self.view.x + self.view.width/4;
}
} else {
_arrowImgV.x = self.view.centerX + self.view.width/3 - self.arrowImgV.width;
}
if (self.direction == BOTTOM) {
self.arrowImgV.y = CGRectGetMaxY(self.view.frame)+5;
}else if (self.direction == TOP){
self.arrowImgV.y = CGRectGetMaxY(self.view.frame)- self.view.height - self.arrowImgV.height - 5;
}
self.contentLab.frame = CGRectMake(5, 10, self.arrowImgV.width - 10, self.arrowImgV.height - 20);
}
点击背景view
-(void)tapBackgroundView
{
__weak typeof(self) weakself = self;
[UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:1 initialSpringVelocity:1 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
weakself.arrowImgV.transform = CGAffineTransformMakeScale(0.01, 0.01);
} completion:^(BOOL finished) {
[weakself.contentLab removeFromSuperview];
[weakself.arrowImgV removeFromSuperview];
[weakself.backgroundView removeFromSuperview];
if (weakself.block) {
weakself.block();
}
}];
}
接下来就是引用
判断是否是第一次进入程序
-(void)judgeIfIsFirstLogin
{
if(!self.firstIn) {
self.firstIn = YES;
NSNumber* version = UserDefault(nil, @"HomeViewController_guide_setup1");
int currentVersion = 1;
if (version == nil || version.intValue < currentVersion) {
UserDefault([NSNumber numberWithInt:currentVersion], @"HomeViewController_guide_setup1");
[self showRemind:1];
}
}
}
如果是第一次进来,就调用引导语
-(void) showRemind:(int)setup
{
UIView* view;
if (setup == 1) {
view = self.firstView;
}else if(setup == 2) {
view = self.secondView;
}else if(setup ==3) {
view = self.thirdView;
}else if (setup == 4){
view = self.forthView;
}else if (setup == 5){
view = self.fiveView;
}else{
return;
}
NSString* content;
NSString* imageStr;
switch (setup) {
case 1:
content = @"1、点击这里加班你想干嘛😀";
imageStr = @"rem_01";
break;
case 2:
content = @"2、点击这里加班你想干嘛😂";
imageStr = @"rem_01";
break;
case 3:
content = @"3、点击这里加班你想干嘛😋";
imageStr = @"rem_05";
break;
case 4:
content = @"4、点击这里加班你想干嘛🙄";
imageStr = @"rem_03";
break;
case 5:
content = @"5、点击这里加班你想干嘛😊";
imageStr = @"rem_01";
break;
default:
break;
}
DCPopView *popView = [[DCPopView alloc] initWithAlertContentString:content withImage:imageStr withSpecificUI:view];
if (setup == 1 || setup == 2 || setup == 5) {
popView.direction = BOTTOM;
}else{
popView.direction = TOP;
}
setup = setup + 1;
__weak typeof(self) weakself = self;
popView.block = ^{
[weakself showRemind:setup];
};
[self.view addSubview:popView];
}