app 主题切换

1.添加主题包到工程中,选中Create Folder references生成蓝色的文件夹

2.定义一个主题管理者(当主题切换时,发送通知)单例;

3.使用工厂模式自定义控件(接受通知)

1..


2..

ThemeManager.h

#define kThemeChangeNotification @"ThemeChangeNotification"


//偏好设置里保存的主题信息的key

#define kThemeKey @"ThemeKey"


#define kDefaultThemeName @"猫爷"


//根据主题名字,去plist文件查找相应的主题包文件所在的路径

@interface ThemeManager : NSObject


@property (nonatomic, copy)NSString *themeName; //主题的名字


@property (nonatomic, strong) NSDictionary *themeDic; //主题文件的字典


@property (nonatomic, strong) NSDictionary *colorDic;


+ (ThemeManager *)sharedManager;


//工厂方法

- (UIImage *)loadThemeImage:(NSString *)imageName;


- (UIColor *)loadThemeColor:(NSString *)colorName;


ThemeManager.m

+ (ThemeManager *)sharedManager

{

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        instance = [[ThemeManager alloc] init];

    });

    

    return instance;

}


- (instancetype)init

{

    self = [super init];

    if (self) {

        

        //1.读取主题信息的plist文件

        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"theme" ofType:@"plist"];

        self.themeDic = [NSDictionary dictionaryWithContentsOfFile:filePath];

        

        //2.初始的主题

        //_themeName = @"Blue Moon";

        NSString *savedThemeName = [[NSUserDefaults standardUserDefaults] objectForKey:kThemeKey];

        if (savedThemeName) {

            _themeName = savedThemeName;

        } else {

            _themeName = kDefaultThemeName;

        }


        

        //3.初始主题的路径信息

        //如果沙盒中没有主题,就从bundle移动到沙盒里

        

    }

    return self;

}


//每次切换主题,修改themeName

- (void)setThemeName:(NSString *)themeName

{

    if (_themeName != themeName) {

        _themeName = [themeName copy];

        

        //1.保存主题信息到偏好设置

        [self saveTheme];

        

        //2.发送主题改变的通知

        [[NSNotificationCenter defaultCenter] postNotificationName:kThemeChangeNotification object:nil];

        

        //取得保存主题颜色的字典

        NSString *fullPath = [self getThemePath];

        _colorDic = [NSDictionary dictionaryWithContentsOfFile:[fullPath stringByAppendingString:@"/config.plist"]];

    }

}


//取得 主题文件夹 的路径

- (NSString *)getThemePath

{

    // WXWeibo.app/skins/cat/**.png

    

    //1.根路径

    NSString *bundle = [[NSBundle mainBundle] resourcePath];

    

    //2.拼接图片的路径

    //找到图片对应的路径

    NSString *themePath = [self.themeDic objectForKey:self.themeName];

    

    //3.完整路径 WXWeibo.app/ skins/cat/

    NSString *fullPath = [bundle stringByAppendingPathComponent:themePath];


    return fullPath;

}


//切换主题,使用主题管家去加载相应的图片

- (UIImage *)loadThemeImage:(NSString *)imageName

{

    //会把图片先缓存在内存中, 对于同一张图片,只会缓存一次

    //UIImage *img = [UIImage imageNamed:@"bg_home"];

    

    //直接从文件中加载图片,不会缓存, 必须要添加文件的后缀名

    //UIImage *img = [UIImage imageWithContentsOfFile:];

    

    //具体图片的路径

    NSString *imagePath = [[self getThemePath] stringByAppendingFormat:@"/%@", imageName];

    

    UIImage *image = [UIImage imageWithContentsOfFile:imagePath];

    

    return image;

}


- (UIColor *)loadThemeColor:(NSString *)colorName

{

    //colorname对应的r g b 的字典取出来

    NSDictionary *rgbDic = [_colorDic objectForKey:colorName];

    float r = [[rgbDic objectForKey:@"R"] floatValue];

    float g = [[rgbDic objectForKey:@"G"] floatValue];

    float b = [[rgbDic objectForKey:@"B"] floatValue];

    

    return [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1];

}




//保存主题信息

- (void)saveTheme

{

    [[NSUserDefaults standardUserDefaults] setObject:self.themeName forKey:kThemeKey];

    //同步,保证上一步设置信息之后,马上保存

    [[NSUserDefaults standardUserDefaults] synchronize];

}

自定义控件( ThemeButton)


ThemeButton.h

#import <UIKit/UIKit.h>


@interface ThemeButton : UIButton


@property (nonatomic, copy) NSString *imageName;


ThemeButton.m

- (instancetype)init{

    

   

    return [self initWithFrame:CGRectZero];

}

- (instancetype)initWithFrame:(CGRect)frame{

    

    self = [super initWithFrame:frame];

    if (self) {

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changButton) name:kThemeChangeNoti object:nil];

    }

    return self;

}


- (void)awakeFromNib{

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changButton) name:kThemeChangeNoti object:nil];

}

- (void)setFrame:(CGRect)frame{

    [super setFrame:frame];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changButton) name:kThemeChangeNoti object:nil];

    

}

- (void)setImgName:(NSString *)imgName{

    

    if (_imgName!=imgName) {

        _imgName = [imgName copy];

//        UIImage *img = [[ThemeManager sharedManager] loadImage:_imgName];

//        [self setImage:img forState:UIControlStateNormal];

        [self changButton];

    

        

    }

}

- (void)changButton{

    

   UIImage *img = [[ThemeManager sharedManager] loadImage:_imgName];

    [self setImage:img forState:UIControlStateNormal];

    

}

- (void)dealloc{

    

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

使用自定义的控件时要设置控件的 imageName的属性


效果图







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值