iOS之实用技术Objective C开发

主题换肤的实现

2016-08-27  本文已影响79人  fuxi

本文主要实现主题换肤的管理以及两个常用控件的封装
<h5>大致思路:</h5>
1.建立一个单例类(ThemeManager)专门用来进行主题皮肤的管理,专门负责查找图片和颜色。
2.封装出需要换肤的主要控件,包括UIButton、UIImageView等,方便在以后调用。
3.应用到各个控制器中。</br>

<h5>一、ThemeManager的实现</h5>
1.思路分析:</br>
(1)这是一个单例类,提供相同且唯一的实例。
(2)它只做两件事,一是查找图片,二查找颜色。
2.具体实现:

//.h文件
//1.设置属性
@property (copy, nonatomic) NSString *themeName; 
@property (strong , nonatomic) ThemeManager *manager;
@property (strong, nonatomic) NSDictionary *colorDic;
//2.方法声明
 + (id) shareThemeManger;

//找图片
- (UIImage *) getImageWithImageName:(NSString *) imageName;

//找颜色
- (UIColor *)getThemeColorWithColorName:(NSString *) colorName;

//.m文件
//构造方法的实现
+ (id) shareThemeManger {
    //设置静态变量为空
    static ThemeManager *instance = nil;

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        instance = [[ThemeManager alloc] init];
    });
    return instance;
}

//初始化
- (instancetype) init {
    
    self = [super init];
    if (self) {

        //存储当前主题
        _themeName = [[NSUserDefaults standardUserDefaults]objectForKey:@"themeInfo"];
        
        if (_themeName == nil) {
            //设置默认主题
            _themeName = @"猫爷";
        }
        [self loadConfig];
    }
    return self;
}

//加载主题路径
- (NSString *) loadThemePath {
    //图片保存的路径是“.../Skins/主题名字”
    //查找plist文件
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Theme.plist" ofType:nil];
    //获取主题
    NSDictionary *DicTheme = [NSDictionary dictionaryWithContentsOfFile:filePath];
    
    //获取主题的短路径“/Skins/主题名字”
    NSString *themePath = [DicTheme objectForKey:_themeName];
    
    //拼接完整路径--将短路径拼接在大的路径后面
    NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:themePath];
    return path;
}
//得到图片名字
- (UIImage *) getImageWithImageName:(NSString *) imageName {
   //获取主题完整路径
    NSString *themePath = [self loadThemePath];
    //查找图片路径
    NSString *imgPath = [themePath stringByAppendingPathComponent:imageName];
    
    return [UIImage imageWithContentsOfFile:imgPath];
}
//复写ThemeName的set方法
- (void) setThemeName:(NSString *)themeName {
    
    //判断主题名字是否改变
    if (_themeName != themeName) {
        _themeName = themeName;
        //将改变后的主题存进UserDefault中
        NSUserDefaults *userD = [NSUserDefaults standardUserDefaults];
        [userD synchronize];//立即存储
        [userD setObject:_themeName forKey:@"themeInfo"];
    }
    
    //改变就发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"ThemeChange" object:nil];
}

//获取config.plist---颜色
- (void) loadConfig {
    //拿到主题的完整路径
    NSString *themePath = [self loadThemePath];
    
    //获取config.plist文件
    NSString *filePath = [themePath stringByAppendingPathComponent:@"config.plist"];
    //获取plist文件中的字典
    _colorDic = [NSDictionary dictionaryWithContentsOfFile:filePath];

}
//找颜色
- (UIColor *)getThemeColorWithColorName:(NSString *) colorName {
    
    NSDictionary *rgbDic = _colorDic[colorName];
    
    double R = [rgbDic[@"R"] doubleValue];
    double G = [rgbDic[@"G"] doubleValue];
    double B = [rgbDic[@"B"] doubleValue];
    //判断一下有没有alpha,没有的话默认为1
    double alpha = [rgbDic[@"alpha"] doubleValue]  ?  : 1;
    
    return [UIColor colorWithRed:R/255 green:G/255 blue:B/255 alpha:alpha];
}

</br>

<h5>二、控件的封装</h5></br>

<h6>1.ThemeButton的封装</h6></br>

//设置属性--图片名
@property (nonatomic, strong) NSString *imageName;
//导入头文件
#import "ThemeManager.h"
- (void) setImageName:(NSString *)imageName {
    
    if (_imageName != imageName) {
        _imageName = imageName;
        
    }
    [self loadImage];
}
//加载图片
- (void) loadImage {
    
    ThemeManager *manager = [ThemeManager shareThemeManger];
    //调用getImageWithImageName:方法
    [self setImage:[manager getImageWithImageName:_imageName] forState:UIControlStateNormal];
}
//初始化
- (instancetype) initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        //接收通知并实现方法,这里再次调用图片加载方法
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadImage) name:@"ThemeChange" object:nil];
    }
    return self;
}

<h6>2..ThemeImageView的封装</h6>

@property (nonatomic, strong) NSString *imageName;
- (void) setImageName:(NSString *)imageName {
    
    if (_imageName != imageName) {
        _imageName = imageName;
        
    }
    [self loadImage];
}
- (void) loadImage {
    
    ThemeManager *manager = [ThemeManager shareThemeManger];
    UIImage *img =[manager getImageWithImageName:_imageName];

    self.image = [img stretchableImageWithLeftCapWidth:30 topCapHeight:30];

}
//初始化
- (instancetype) initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadImage) name:@"ThemeChange" object:nil];
    }
    return self;
}
- (void) dealloc {
    
     [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ThemeChange" object:nil];
}



<h5>三、应用</h5>

//设置标签栏背景图片
    ThemeImageView *backImgView = [[ThemeImageView alloc] initWithFrame:CGRectMake(0, -6, kScreenWidth, 55)];
    backImgView.imageName = @"图片名";
    [self.tabBar addSubview:backImgView];
float width = kScreenWidth/imaArr.count;
    //创建button
    for (int i=0; i < imaArr.count; i++) {
        NSString *imageName = imaArr[i];
        ThemeButton *button = [ThemeButton buttonWithType:UIButtonTypeCustom];
        button.imageName = imageName;
        button.frame = CGRectMake(i*width, 2, width, 45);
        button.tag = 2016 + i;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabBar addSubview:button];
    }

 //设置选中图片
    _selectedImage = [[ThemeImageView alloc] initWithFrame:CGRectMake(0, 4, width, 45)];
    _selectedImage.imageName = @"图片名";
    [self.tabBar addSubview:_selectedImage];
//设置内容视图背景图片
- (void) viewDidLoad {
    
    //1.加载图片
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loadImag) name:@"ThemeChange" object:nil];
    
    [self loadImag];
}

- (void)loadImag{
    UIImage*img = [[ThemeManager shareThemeManger] getImageWithImageName:@"图片名"];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:img];
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ThemeChange" object:nil];
}
//设置导航栏背景图片
- (void) viewDidLoad {

    //设置标题颜色
    self.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],
                                               NSFontAttributeName : [UIFont boldSystemFontOfSize:20]};
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loadImg) name:@"ThemeChange" object:nil];
    
    [self loadImg];
}

- (void)loadImg{
    ThemeManager *manager = [ThemeManager shareThemeManger];
    UIImage *img = [manager getImageWithImageName:@"图片名"];
    //设置背景颜色
    [self.navigationBar setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"ThemeChange" object:nil];
}
上一篇 下一篇

猜你喜欢

热点阅读