• 定时改变App主题的方案


    1.将接口返回的图片缓存到本地,由于写data到本地是耗时操作,为了不阻塞主线程,可开启子线程来做此操作

     dispatch_queue_t queue = dispatch_queue_create("cn.xxx.queue", DISPATCH_QUEUE_SERIAL);
        // 异步存到沙盒
        dispatch_async(queue, ^{
            
            for (XWMainBtnSkinModel *skinM in self.models) {
                
                //  btn_Bottom_4
                NSString *path =  [skinM.btnKey cacheDirWithSubpath:@"tabbar"];
                
                // 下载图片
                NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:skinM.img]];
    
                [data writeToFile:path atomically:YES];
                
               if([skinM.type isEqualToString:@"1"]){
                    
                    // btn_Bottom_4_sel
                    NSString *selImgKey = [NSString stringWithFormat:@"%@_sel",skinM.btnKey];
                    NSString *selpath =  [selImgKey cacheDirWithSubpath:@"tabbar"];
                    
                    // 下载图片
                    NSData *selImgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:skinM.selImg]];
                    
                    
                    [selImgData writeToFile:selpath atomically:YES];
                }
            }
            
        });

    2.将请求到的图片,保存到内存,并给UI控件赋值,此处以tabbar为例

        UIImage *img = [[UIImage alloc] init];
        UIImage *selImg = [[UIImage alloc] init];
        int i = 0;
         for (UITabBarItem *item in self.tabBar.items) {
             if(isNet){// 如果是从网络上获取,要告诉系统是@2x的图片
                 img =  [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imgs[i]]] scale:2.0];
                 selImg = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:selImgs[i]]] scale:2.0];
             }else{
                 img =  [UIImage imageNamed:imgs[i]];
                 selImg = [UIImage imageNamed:selImgs[i]];
             }
            
            item.image = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            
            item.selectedImage = [selImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
            i++;
        }

    3.第二次打开app时,判断本地是否有缓存文件,如果有,就取之前缓存中的文件(这样用户体验更好,省去去网络上下载图片)

        if (!isExist) { // 如果cache/tabbar这个目录不存在,就用默认主题
            [self setUpChildController:mainPage title:@"首页" image:@"img_btn_mainpage_normal.png" selectedImage:@"img_btn_mainpage_select.png"];
        
            [self setUpChildController:departClass title:@"分类" image:@"img_btn_class_normal" selectedImage:@"img_btn_class_select"];
            
            [self setUpChildController:theme title:@"" image:@"img_btn_theme" selectedImage:@"img_btn_theme"];
        
            [self setUpChildController:message title:@"消息" image:@"img_btn_message_normal" selectedImage:@"img_btn_message_select"];
      
            [self setUpChildController:me title:@"" image:@"img_btn_me_normal" selectedImage:@"img_btn_me_select"];
       
            self.selectedIndex = 0;
            
            // 矫正TabBar图片位置,使之垂直居中显示
            CGFloat offset = 3.0;
            for (UITabBarItem *item in self.tabBar.items) {
                item.imageInsets = UIEdgeInsetsMake(offset, 0, -offset, 0);
                if([item.title isEqualToString:@""])
                {
                    item.imageInsets = UIEdgeInsetsMake(7, 0, -7, 0);
                }
            }
        } else {    //存在
            
            [self setUpNetDataChildController:mainPage title:@"首页" image:@"btn_Bottom_1" selectedImage:@"btn_Bottom_1_sel"];
            [self setUpNetDataChildController:departClass title:@"分类" image:@"btn_Bottom_2" selectedImage:@"btn_Bottom_2_sel"];
            [self setUpNetDataChildController:theme title:@"" image:@"btn_Bottom_3" selectedImage:@"btn_Bottom_3_sel"];
            [self setUpNetDataChildController:message title:@"消息" image:@"btn_Bottom_4" selectedImage:@"btn_Bottom_4_sel"];
            [self setUpNetDataChildController:me title:@"" image:@"btn_Bottom_5" selectedImage:@"btn_Bottom_5_sel"];
    
            
            self.selectedIndex = 0;
            
            // 矫正TabBar图片位置,使之垂直居中显示
            CGFloat offset = 3.0;
            for (UITabBarItem *item in self.tabBar.items) {
                item.imageInsets = UIEdgeInsetsMake(offset, 0, -offset, 0);
                if([item.title isEqualToString:@""])
                {
                    item.imageInsets = UIEdgeInsetsMake(7, 0, -7, 0);
                }
            }
    
        }

    4.如果到了主题的过期时间,就应该切换回默认的主题:从接口中获取到主题到期时间,并设置定时器,当到期后,就删除本地缓存,并不在处理接口返回的图片数据。(直到接口中的到期时间重设,才处理接口中的图片)

        [[NetRequest request]dataRequest:nil apiURL:__getSkinBtnImg needToCache:nil completeBlock:^(id result) {
            
            self.models = [XWMainBtnSkinModel mj_objectArrayWithKeyValuesArray:result[@"btnImg"]];
            if(self.models.count > 0 ){
                
                XWMainBtnSkinModel *model = self.models[0];
                NSDateFormatter *date=[[NSDateFormatter alloc] init];
                //        endTime: "2016-04-16 16:45:59"
                [date setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                
               /**服务返回的主题到期时间*/
                NSDate *d=[date dateFromString:model.endTime];
                NSTimeInterval late = [d timeIntervalSince1970];
                
                NSDate *now = [NSDate date];
                NSTimeInterval nowtiem = [now timeIntervalSince1970];
                NSTimeInterval cha = late - nowtiem;
                /**到期时间和当前时间的差值*/
                self.cha = cha;
                // 定时器(到期了就切回之前的默认主题)
                NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:cha target:self selector:@selector(returnSkan) userInfo:nil repeats:NO];
                [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];;
    
                
                if(self.cha <=0){
                    
                    // 时间过了就不处理接口返回的数据了
                    return ;
                }
                
                if (!isExist) {//目录不存在(第一次从网络上直接获取图片)
                    // 通知更改主题
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"change" object:self userInfo:@{@"info":self.models}];
                    
                    // 将图片  写入本地缓存起来,第二次就不用从网络上获取
                    [self writeFileToCache];
                    
                    NSMutableArray *imgs = [NSMutableArray array];
                    NSMutableArray *selImgs = [NSMutableArray array];
                    
                    
                    for (XWMainBtnSkinModel *model in self.models) {
                        if ([model.type isEqualToString:@"1"]) {
                            [imgs addObject:model.img];
                            [selImgs addObject:model.selImg];
                        }
                    }
                    [self setUpTabbarWithImgs:imgs selImgs:selImgs isNet:YES];
                    [self.view setNeedsLayout];
                }
                
                
            }else { // 如果没有请求到数据,就删除网络数据
                [self returnSkan];
            }
       
        } failedBlock:^(id error) {
            [SVProgressHUD showErrorWithStatus:error];
        }];
    // 到时间后,就换回之前的皮肤
    - (void)returnSkan
    {
        if ([[NSFileManager defaultManager] fileExistsAtPath:self.fileCache]) {
            
            // 删除沙盒的数据
            [[NSFileManager defaultManager] removeItemAtPath:self.fileCache error:NULL];
        }
        // 该切回之前的主题了
        [[NSNotificationCenter defaultCenter] postNotificationName:@"cha" object:self userInfo:nil];
    
        
        NSMutableArray *normalImgs = [NSMutableArray arrayWithArray:@[@"img_btn_mainpage_normal.png",@"img_btn_class_normal",@"img_btn_theme",@"img_btn_message_normal",@"img_btn_me_normal"]];
        NSMutableArray *selImgs = [NSMutableArray arrayWithArray:@[@"img_btn_mainpage_select.png",@"img_btn_class_select",@"img_btn_theme",@"img_btn_message_select",@"img_btn_me_select"]];
        
        
        [self setUpTabbarWithImgs:normalImgs selImgs:selImgs isNet:NO];
    }
  • 相关阅读:
    (原)试问那些不劳而获的人?
    (原)资源互换原则
    (原)关于与人沟通时的几个中文语法
    osgviewer读取dxf文件,拣选高亮功能小结
    qt的OSG视图中拣选对象事件消息
    1)OSG与QT单文档视图的结合
    三维渲染引擎设计与实践(八)
    三维渲染引擎设计与实践(七)
    三维渲染引擎设计与实践(六)
    三维渲染引擎设计与实践(五)
  • 原文地址:https://www.cnblogs.com/heyode/p/5442447.html
Copyright © 2020-2023  润新知