gpt4 book ai didi

iphone - 如何在 iOS 应用程序中切换皮肤(或设计主题)?

转载 作者:可可西里 更新时间:2023-11-01 03:32:34 24 4
gpt4 key购买 nike

我想让我的 iPhone 应用程序能够在皮肤(或设计主题,或外观和感觉,例如木质、金属、大地色、男士、女士等...)之间切换。

我将准备一些包含按钮和背景图像、声音和文本颜色的皮肤集,并让用户通过应用程序设置来决定他们想要使用的皮肤集。

实现这个的最佳实践是什么?

条件是:

  • 我想使用 Interface Builder
  • 我需要支持 iOS 3.1.3 及更高版本
  • 我想让皮肤组可以从互联网上下载(我不能将所有皮肤都捆绑到应用程序中,因为一组皮肤需要大量图像,如果这样做,应用程序文件可能会变得很大。 .. 我也不想硬编码有关特定皮肤的任何信息。)
  • 如果自定义皮肤不包含一个或某些元素(例如图像或声音文件),我希望它使用默认皮肤集中缺少的元素。
  • 我不想为每个皮肤创建 Nib 文件。一个屏幕的 Nib 文件应该是主包中的唯一文件,以便于维护。

我正在考虑在我的应用程序中创建所有 UIViewControllers 的父类(super class)并覆盖它加载 Nib 文件的部分,而不是从主包加载,从保存在 Document 目录中的皮肤加载资源...但我不知道该怎么做...Nib 加载方法的默认行为总是从主包加载资源,并且有关资源文件名的信息在阅读后丢失...:(

预先感谢您的帮助。

最佳答案

我不确定最佳实践..但是,如果您的应用不够大,那么结构良好的 plist 是您的 friend 。

最初,您可以选择:Metal Theme。以下内容应成立:

你要么有一个 Singleton ThemeManager,要么在合适的情况下将 NSDictionary 粘贴到你的一个 Singletons 上。

ThemeManager 背后的重点是 Assets 和主题之间的映射..

一些示例代码(直接写在 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"

或者,您可以只保存后缀,如果这对您来说足够好的话。但是,它需要字符串操作,通过切片扩展 -> 添加后缀 -> 添加扩展..

或者让它成为一个前缀?

关于iphone - 如何在 iOS 应用程序中切换皮肤(或设计主题)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7939981/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com