我想让我的 iPhone 应用程序能够在皮肤(或设计主题,或外观和感觉,如木质、金属、大地色、男士、女孩等)之间切换。
我将准备一些皮肤组,其中包含按钮和背景的图像、声音和文本颜色,并让用户通过应用程序设置决定他们想要使用哪组皮肤。
实现这一点的最佳实践是什么?
条件是:
- 我想使用界面生成器
- 我需要支持 iOS 3.1.3 及更高版本
- 我想让这些皮肤集可以从互联网下载(我无法将所有皮肤捆绑在应用程序中,因为一组皮肤需要大量图像,如果这样做,应用程序文件大小可能会变得很大......我也不想硬编码有关特定皮肤的任何信息。)
- 如果自定义皮肤不包含一个或某些元素(例如图像或声音文件),我希望它使用默认皮肤集中缺少的元素。
- 我不想为每个皮肤创建 Nib 文件。一个屏幕的 Nib 文件应该是主包中唯一的文件,以便于维护。
我正在考虑在我的应用程序中创建所有 UIViewController 的超类,并覆盖它加载 Nib 文件的部分,而不是从主包加载,而是从保存在 Document 目录中的皮肤加载资源...但我不知道该怎么做... Nib 加载方法的默认行为始终从主包加载资源,并且有关资源文件名的信息在读取后会丢失... :(
在此先感谢您的帮助。
我不确定最佳实践。但是,如果您的应用程序不够大,那么结构良好的 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"
或者,您可以只保存后缀,如果这对您来说足够好的话。但是,它将需要字符串操作,通过切片扩展名 - >添加后缀 - >添加扩展名..
或者也许将其作为前缀?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)