我想让我的iPhone应用程序能够在皮肤(或设计主题,或外观和感觉,如木制,金属,泥土颜色,男性,女孩等)之间切换。
我将准备一些包含按钮、背景、声音和文本颜色的图像的皮肤集,并让用户决定应用程序设置要使用哪组皮肤。
实现这一点的最佳做法是什么?
这些条件是:
。
我正在考虑用我的应用程序中的所有UIViewControllers创建一个超类,覆盖它加载Nib文件的部分,而不是从主包加载资源,而是从保存在文档目录中的皮肤加载资源.但我不知道怎么做..。Nib加载方法的默认行为总是从主包中加载资源,并且在阅读完资源文件名后,有关资源文件名的信息将丢失。:(
提前谢谢你的帮助。
发布于 2011-10-29 16:51:01
我不确定最佳做法..。但是,如果您的应用程序不够大,那么结构良好的plist就是您的朋友。
最初,你可以选择:金属主题。应采取以下措施:
您要么拥有一个单例ThemeManager
,要么在适当的情况下将一个NSDictionary
粘贴到您的一个单件上。
ThemeManager背后的要点是资产和主题之间的映射。
一些示例代码(直接写在SOF上)。不要介意语法错误):
#define kThemeMap(__x__) [[ThemeManager sharedManager] assetForCurrentTheme:__x__]
...
-(void)doUselessStuff {
UIImage* backgroundImage = [UIImage imageNamed:kThemeMap(@"FirstViewBG")];
...
}
//in the ThemeManager:
//returns the appropriate name of the asset based on current theme
-(NSString*)assetForCurrentTheme:(NSString*)asset {
//_currentTheme is an NSDictionary initialized from a plist. Plist can be downloaded, too.
NSString* newAsset = [_currentTheme objectForKey:asset];
if(newAsset == nil) {
newAsset = [_defaultTheme objectForKey:asset];
}
return asset;
}
//Let us assume the user selects Metal Theme somewhere .. Still coding ThemeManager:
-(void)selectedNewTheme:(NSString*)newTheme {
//First, get the full path of the resource .. Either The main bundle, or documents directory or elsewhere..
NSString* fullPath = ...;
self.currentTheme = [NSDictionary dictionaryWithContentsOfFile:fullPath];
}
plist文件只是一个具有字符串到字符串映射的字典.就像这样:
//Default.plist
@"FirstViewBG" : @"FirstViewBG_Default.png"
@"SecondViewBG" : @"SecondViewBG_Default.png"
@"WinSound" : @"WinSound_Default.aiff"
//Metal.plist
@"FirstViewBG" : @"FirstViewBG_Metal.png"
@"SecondViewBG" : @"SecondViewBG_Metal.png"
@"WinSound" : @"WinSound_Metal.aiff"
或者,您可以保存后缀,如果这对您来说足够好的话。但是,它将需要字符串操作,通过切片扩展->添加后缀->添加扩展。
或者把它作为前缀?
发布于 2013-03-19 03:52:05
您可以在UIImage上使用imageNamed:,而不是默认的imageNamed:。在自定义方法中,按主题选择图像。
https://stackoverflow.com/questions/7939981
复制相似问题