iOS技术资料ios实用开发技巧iOS开发进阶

iOS简单实现一天一次弹窗

2017-10-23  本文已影响1156人  凉风起君子意如何

产品需求

  1. app首页一天中,用户第一次打开应用时,展示活动弹窗(红包或者一些h5活动等),之后都不弹窗。效果图如下(红色部分为imageview,图片url和图片活动链接待请求接口后台返回)
  2. 弹窗具体为一张图片。当点击图片时,若链接url不为空,则webview加载该url;若为空,跳转到个人中心未登录页面,已登录则跳转到个人中心红包列表页面
测试暂时以红色背景代替

主要代码逻辑

弹窗视图(注意注释)

- (void)imgTapped{
    [self removeFromSuperview];
    if (self.blockH5) {
        self.blockH5();
    }
}
//AlertType控制多种弹框样式。当为ALertToH5, andDetailsString参数为urlString
-(instancetype)initWithFrame:(CGRect)frame alertType:(AlertType)AlertType andDetailsString:(NSString *)string{
    
    if ( self = [super initWithFrame:frame]) {
        //保存remarkString
        if (string.length>0) {
            self.loginRemarkString = string;
        }else{
            self.loginRemarkString = @" ";
        }
        
        self.backgroundColor = kRGBColor(1, 1, 1, 0.4);
        UIView *centerView = [UIView new];
        centerView.center = self.center;
        centerView.bounds =CGRectMake(0, 0, kScreenWidth - 50,0);
        centerView.layer.cornerRadius = 10.f;
        centerView.layer.masksToBounds = YES;
        [self addSubview:centerView];
        self.centerView = centerView;
        /*弹框begin*/
        if (AlertType == ALertToH5) {
            [centerView setFrame:CGRectMake(0, 0, kScreenWidth - 50,200)];
            UIImageView *iv =  [[UIImageView alloc]initWithFrame:centerView.bounds];
            iv.center = self.center;
            iv.backgroundColor = [UIColor redColor];
            //使用sdWebImage 加载后台返回图片url  TODO
            //添加手势
            iv.userInteractionEnabled = YES;
            UITapGestureRecognizer *ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imgTapped)];
            [iv addGestureRecognizer:ges];
            [self addSubview:iv];
            UIButton *deleteBtn = [UIButton new];
            CGPoint center =  self.center;
            deleteBtn.center =CGPointMake(center.x, center.y+self.centerView.frame.size.height/2.f +24);
            deleteBtn.bounds = CGRectMake(0, 0, 30, 48);
            [deleteBtn setBackgroundImage:[UIImage imageNamed:@"featureDepsit_11"] forState:UIControlStateNormal];
            [deleteBtn addTarget:self action:@selector(removeSelf) forControlEvents:UIControlEventTouchUpInside];
            deleteBtn.adjustsImageWhenHighlighted = NO;
            [self addSubview:deleteBtn];
            return self;
        }
       /*弹框end*/
}

首页vc(注意一天一次弹框判断逻辑)

- (void)viewDidLoad {
    
    [super viewDidLoad];

    ...

    [self showH5AlertView];
}
-(void)showH5AlertView{
    //showalert之前进行 一天一次判断
    NSDate *now = [NSDate date];
    NSDate *agoDate = [[NSUserDefaults standardUserDefaults] objectForKey:@"nowDate"];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *ageDateString = [dateFormatter stringFromDate:agoDate];
    NSString *nowDateString = [dateFormatter stringFromDate:now];
    NSLog(@"日期比较:之前:%@ 现在:%@",ageDateString,nowDateString);
    if ([ageDateString isEqualToString:nowDateString]) {
        NSLog(@"一天就显示一次");
    }else{
        //记录弹窗时间
        NSDate *nowDate = [NSDate date];
        NSUserDefaults *dataUser = [NSUserDefaults standardUserDefaults];
        [dataUser setObject:nowDate forKey:@"nowDate"];
        [dataUser synchronize];
        //请求图片url和链接url,并showalert;
        //此时andDetailsString为图片url。下面linkUrl测试用先写死,主要看逻辑
        NSString *linkUrl = @"https://www.baidu.com/";
        DepositedAlertView *alertView = [[DepositedAlertView alloc]initWithFrame:[UIScreen mainScreen].bounds alertType: ALertToH5 andDetailsString:nil];
        [[UIApplication sharedApplication].keyWindow addSubview:alertView];
        __weak typeof(self) weakself = self;
        alertView.blockH5 = ^ {
            if (linkUrl) {
                [BannerHrefVC showWithControl:weakself withUrlStr:linkUrl withTitle:@""];
            }else{
                //linkUrl为空的话,跳转到个人中心页
            }
        };
    }
}
上一篇下一篇

猜你喜欢

热点阅读