源代码
链接:
https://pan.baidu.com/s/16XRHhx0LS_qgZ8FAUjNXkQ 密码:sooq
主要代码:
//
// ViewController.m
// 主题切换
//
// Created by 许磊 on 2019/2/26.
// Copyright © 2019年 xulei. All rights reserved.
//
#import "ViewController.h"
#import "ThemeManager.h"
#import "ThemeImageView.h"
#import "Constants.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//布局
[self UIInit];
}
-(void)UIInit{
//创建图片1
ThemeImageView *v1 = [[ThemeImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-50, 200, 100, 50)];
v1.imageName = @"1.png";
[self.view addSubview:v1];
//创建图片1
ThemeImageView *v2 = [[ThemeImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-50, 300, 100, 50)];
v2.imageName = @"2.png";
[self.view addSubview:v2];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//切换主题
//获取触摸点的坐标
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
if (location.x > self.view.center.x) {
//1.改变Thememanager里面的themeName
[ThemeManager sharedInstance].themeName = @"黑色";
} else {
//2.改变Thememanager里面的themeName
[ThemeManager sharedInstance].themeName = @"蓝色";
}
//3.发通知
[[NSNotificationCenter defaultCenter] postNotificationName:kThemeDidChangeNotificationName object:nil];
}
@end
//
// Constants.h
// 主题切换
//
// Created by 许磊 on 2019/2/27.
// Copyright © 2019年 xulei. All rights reserved.
//
#ifndef Constants_h
#define Constants_h
/**主题切换的消息名*/
#define kThemeDidChangeNotificationName @"kThemeDidChangeNotificationName"
#endif /* Constants_h */
//
// ThemeManger.h
// 主题切换
//
// Created by 许磊 on 2019/2/27.
// Copyright © 2019年 xulei. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ThemeManager : NSObject
/**记录当前的主题*/
@property (nonatomic,strong) NSString *themeName;
/** 获取某张图片在当前主题下对应的图片*/
-(UIImage *)getThemeImage:(NSString *)imageName;
/**提供类方法 快速获取一个对象 保证只有一个*/
+(ThemeManager *)sharedInstance;
@end
//
// ThemeManger.m
// 主题切换
//
// Created by 许磊 on 2019/2/27.
// Copyright © 2019年 xulei. All rights reserved.
//
#import "ThemeManager.h"
static ThemeManager *instance = nil;
@implementation ThemeManager
#pragma mark -------shareInstance ---------
//单例模式
+(ThemeManager *)sharedInstance{
if (instance == nil) {
instance = [[ThemeManager alloc] init];
//提供一个默认的
instance.themeName = @"粉色";
}
return instance;
}
#pragma mark -------getThemeImage: ---------
//获得图片
-(UIImage *)getThemeImage:(NSString *)imageName{
//图片的路径 程序的根路径/theme/主题/imageName
//1.获取程序的根路径
NSString *path = [[NSBundle mainBundle] resourcePath];
//2.拼接路径
NSString *imagePath = [NSString stringWithFormat:@"%@/theme/%@/%@",path,self.themeName,imageName];
NSLog(@"%@",imagePath);
//3.获取图片
UIImage *img = [UIImage imageWithContentsOfFile:imagePath];
return img;
}
@end
//
// ThemeImageView.h
// 主题切换
//
// Created by 许磊 on 2019/2/27.
// Copyright © 2019年 xulei. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ThemeImageView : UIImageView
/**记录需要图片的名称*/
@property (nonatomic,strong) NSString *imageName;
@end
//
// ThemeImageView.m
// 主题切换
//
// Created by 许磊 on 2019/2/27.
// Copyright © 2019年 xulei. All rights reserved.
//
#import "ThemeImageView.h"
#import "ThemeManager.h"
#import "Constants.h"
@implementation ThemeImageView
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self != nil){
//监听消息
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadImage) name:kThemeDidChangeNotificationName object:nil];
}
return self;
}
//重写imageName的set方法
-(void)setImageName:(NSString *)imageName{
_imageName = imageName;
[self loadImage];
}
#pragma mark -------loadImage ---------
//加载图片
-(void)loadImage{
//向ThemeManger获取对应主题的图片
UIImage *img = [[ThemeManager sharedInstance] getThemeImage:self.imageName];
//改变显示的图片
self.image = img;
}
@end
//
// ThemeNavigationController.h
// 主题切换
//
// Created by 许磊 on 2019/2/27.
// Copyright © 2019年 xulei. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ThemeNavigationController : UINavigationController
/**记录需要图片的名称*/
@property (nonatomic,strong) NSString *imageName;
@end
//
// ThemeNavigationController.m
// 主题切换
//
// Created by 许磊 on 2019/2/27.
// Copyright © 2019年 xulei. All rights reserved.
//
#import "ThemeNavigationController.h"
#import "ThemeManager.h"
#import "Constants.h"
@interface ThemeNavigationController ()
@end
@implementation ThemeNavigationController
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadImage) name:kThemeDidChangeNotificationName object:nil];
self.imageName = @"1.png";
}
//重写imageName的set方法
-(void)setImageName:(NSString *)imageName{
_imageName = imageName;
[self loadImage];
}
#pragma mark -------loadImage ---------
//加载图片
-(void)loadImage{
//向ThemeManger获取对应主题的图片
UIImage *img = [[ThemeManager sharedInstance] getThemeImage:self.imageName];
[self.navigationBar setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];
}
@end
运行结果
附一:传递消息-消息通知
1.消息通知的优点是可以一对多进行消息传递,可以隔层传递
2.当发送消息时,注册的观察者就会收到消息执行对应的方法
3.观察者不会自动移除,需要手动移除
附二:视图与视图控制器搭建联系
我们自定义了一个继承于UINagationController的类,而想要其控制导航栏的话,就需要在Main.storyboard找到对应视图的Custom Class的class框选择我们想要搭建联系的类