AdaptiveController 是一个用于向原生或自定义 UI 元素添加自定义状态的 '渐进式减少' Swift 模块。
AdaptiveController 是一个模块,可以向原生或自定义 UI 元素添加自定义状态。UI 元素会随着时间的推移和用户的适应而演变。提供了一个自适应标签栏的示例。
首先,您应该设置安装日期、当前日期、过渡到小文本状态的日数以及过渡到大图像状态的日数。AdaptiveDateState 对象将自动确定显示哪个 UI 状态。
var installDate = NSDate(dateString:"2014-09-18")
var adaptiveState = AdaptiveDateState(installDate: installDate, currentDate:NSDate(), countDaysToSmallTextState:countDaysToSmallTextState, countDaysToImageState:countDaysToImageState)
或者对于不同状态的使用次数
var curentCountLaunches = 6
var adaptiveState = AdaptiveLaunchesState(curentCountLaunches:curentCountLaunches, countLaunchesToSmallTextState:5, countLaunchesToImageState:7)
然后,您应该设置一个外观数组。您应该为每个外观对象配置每个状态下按钮的图像。
var buttonsAppearances = buttonsAppearancesGenerate() //func butonsAppearancesGenerate() -> [AdaptiveButtonApperance]
然后,您应该设置一个项目外观对象数组。对于每个外观对象,为每个状态设置按钮的图像。
首先,为每个 UI 状态设置图像扩展的一组字典。
var imageExtensionsForStates:Dictionary = [ kSmallTitleAdaptiveState:"_smalltitle", kImageAdaptiveState:"_bigimage", kSmallTitleAdaptiveState+selected :"_smalltitle", kImageAdaptiveState+selected:"_bigimage" ]
然后,为每个状态设置每个 UITabBarItem 标题的 Appearance、每个状态的字体、以及每个状态描述扩展时默认图像或图像扩展,如果没有描述扩展,应该为每个状态设置图像。
首先,初始化外观对象并为每个状态设置文本。如果文本对每个状态都相同,可以为默认状态设置文本。
var watchAppearance = AdaptiveButtonAppearance();
watchAppearance.setButonTitle("watch", state: kDefaultAdaptiveState)
watchAppearance.setButonTitle("watch", state: kSmallTitleAdaptiveState)
watchAppearance.setButonTitle("", state: kImageAdaptiveState)
然后,为每个状态设置字体。
watchAppearance.setButonTitleFontForState(yourDefaultFont, state: kDefaultAdaptiveState)
watchAppearance.setButonTitleFontForState(yourDefaultSmallTitleModeFont, state: kSmallTitleAdaptiveState)
然后,为每个状态设置默认图像和扩展。一个函数可以自动设置每个状态中的图像。
watchAppearance.setImageNamesForStatesImageExtesions("watch", imageExtensionsForState:imageExtensionsForStates)
然后您应该为每个状态设置外观内边距
watchAppearance.setImageInsets(defaultInsets, state: kDefaultAdaptiveState);
watchAppearance.setImageInsets(defaultSmallTitleModeImageInsets, state: kSmallTitleAdaptiveState)
watchAppearance.setTitleOffset(defaultOffset, state: kDefaultAdaptiveState)
watchAppearance.setImageInsets(defaultImageModeInsets, state: kImageAdaptiveState);
然后您应该设置符合 AdaptiveButtonsProtocol 的自定义 UITabBarItems
var arrayButtons = tabBar.items as [AdaptiveTabBarItem]
在您的自定义标签项中,您应该实现协议中的这些方法
@objc protocol AdaptiveApperanceProtocol {
optional func setFontToAdaptiveButton(font: UIFont)
optional func setTitleToAdaptiveButton(text: NSString)
optional func setImageToAdaptiveButton(image: UIImage?)
optional func setHighlightedToAdaptiveButton(image: UIImage?)
optional func setBackgroundImageToAdaptiveButton(image: UIImage?)
optional func setSelectedImageToAdaptiveButton(image: UIImage?)
optional func setImageInsetsToAdaptiveButton(insets: UIEdgeInsets)
optional func setTitleOffsetToAdaptiveButton(offset: UIOffset)
optional func setTitleColorToAdaptiveButton(titleColor: UIColor)
}
来自我们自定义标签栏项的协议方法的一个示例实现
func setTitleToAdaptiveButton(text: NSString) {
self.title = text
}
最后,您应该使用您已设置的实体初始化 Adaptive 状态管理器
AdaptiveButtonsStateManager(state: adaptiveState, buttonsAray:arrayButtons, buttonsAppearance:butonsAppearances)
如同 UITabBar 对于不同的自定义应用程序状态
我们有默认的 AdaptiveState,在继承的帮助下,我们可以向我们的状态类中添加新的自定义状态。在我们的情况下,这个新的类名为 AdaptiveDateState
在 AdaptiveDateState 中,我们可以这样添加新的自定义状态
let kSmallTitleAdaptiveState = "kImageAdaptiveState"
let kImageAdaptiveState = "kNormalImageAdaptiveState"
并添加决定状态如何与输入参数相关联的方法
var adaptiveState = AdaptiveDateState(installDate: installDate,currentDate:NSDate(),countDaysToSmallTextState:countDaysToSmallTextState,countDaysToImageState:countDaysToImageState)
在 AdaptiveDateState 中,我们添加自定义初始化方法,这些方法决定了根据输入参数哪些状态具有 Adaptive 状态对象
private func stateRemainDays(remainDays:Int, countDaysToSmallTextState:Int, countDaysToImageState:Int)->String {
var mode:String = kDefaultAdaptiveState
print(" DAYS \(remainDays) ")
if remainDays > countDaysToSmallTextState && remainDays < countDaysToImageState{
mode = kSmallTitleAdaptiveState
} else if remainDays > countDaysToImageState {
mode = kImageAdaptiveState
}
print(mode)
return mode
}
如果它们采用 AdaptiveApperanceProtocol,则可以自定义不同的 UI 组件。
Ramotion 是一家 iPhone 应用设计开发公司。我们随时准备迎接新的有趣的 iOS App 开发项目。
在 twitter 上关注我们。