如何在iOS应用中切换皮肤(或设计主题)? [英] How to switch skins (or design themes) in iOS app?

查看:295
本文介绍了如何在iOS应用中切换皮肤(或设计主题)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'd like to make my iPhone app to be able to switch between skins (or design theme, or look and feel, such as wooden, metal, earth color, men's, girls, etc...).

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

I'll prepare some sets of skins that contains images for buttons and backgrounds, sounds, and text color, and let the user decide which set of skin they want to use by the application settings.

实现此目的的最佳做法是什么?

What is the best practice to implement this?

条件是:


  • 我想使用Interface Builder

  • 我需要支持iOS 3.1.3及更高版本

  • 我想从互联网上下载皮肤集(我不能捆绑应用程序中的所有皮肤,因为一组皮肤需要大量图像,并且应用程序文件大小可能变得巨大如果我这样做...我也不想硬编码任何有关特定皮肤的信息。)

  • 如果自定义皮肤不包含一个或一些元素,(例如图像或声音文件),我想要它使用默认皮肤集中的缺失元素。

  • 我不想为每个皮肤创建Nib文件。一个屏幕的Nib文件应该是主软件包中唯一的一个,以便于维护。

  • I'd like to use Interface Builder
  • I need to support iOS 3.1.3 and later
  • I want to make the sets of skins downloadable from the internet (I can't bundle all the skins in the app, as one set of skin requires lots of images and the app file size could become huge if I do so... I also don't want to hardcode any information about specific skins.)
  • If a custom skin does not contain one or some elements, (such as an image or sound file), I want it to use the missing element from the default set of skin.
  • I don't want to create Nib files for each skin. The Nib file for one screen should be the only one in the main bundle for easier maintenance.

我正在考虑建立一个超类在我的应用程序中的所有UIViewControllers和覆盖它加载Nib文件的部分,而不是从主包加载,从文档目录中保存的皮肤加载资源...但我不知道如何这样做... Nib加载方法的默认行为总是从主包中加载资源,并且在阅读...之后有关资源文件名的信息丢失:(

I'm thinking about making a superclass of all the UIViewControllers in my app and override the part that it loads Nib file, and instead of loading from the main bundle, load the resources from the skin that is saved in the Document directory... but I don't know how to do it... The default behavior of the Nib-loading methods always loads resources from the main bundle and the information about resource file names are lost after reading... :(

提前感谢您的帮助。

推荐答案

我不确定最佳做法..但是,如果你的应用程序不大足够的,然后一个结构良好的plist是你的朋友。

Am not sure about best practice .. But, if your app is not big enough, then a well structured plist is your friend.

最初,你可以选择:金属主题。以下应该成立:

Initially, you could choose: Metal Theme. The following should hold:

您要么拥有Singleton ThemeManager ,要么只需将 NSDictionary 添加到如果合适,你的单身人士的权利。

You either have a Singleton ThemeManager, or just stick an NSDictionary to one of your Singletons if appropriate.

ThemeManager背后的点是资产和主题之间的映射..

The point behind the ThemeManager is the mapping between the asset and the theme..

一些示例代码(直接在SOF上编写..不要介意语法错误):

Some sample code (written directly on SOF .. Don't mind Syntax mistakes):

#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文件只是一个字符串,字符串到字符串映射......像这样:

The plist files are just a dictionary with string to string mapping... something like this:

//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"

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

Alternatively, you can just save the postfix, if that is good enough for you.. But, it will require string manipulation, by slicing the extension -> adding the postfix -> adding the extension ..

或者可以将其作为前缀?

Or maybe make it a prefix?

这篇关于如何在iOS应用中切换皮肤(或设计主题)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆