个月前,完成了个人App的2.0版本,也在普天同庆的六一儿童节这天上架了。因为是个人开发,很多实现都是边探索边做。现在完成之后再回顾,发现自己走了些弯路。所以写了这篇总结,概览了从想法、设计、开发到最终发布的过程。希望读者参考本文,可以少走一些弯路;另外,本文也给列出了开发中具体思路和资源列表。
知识从何而来?
Apple的知识又是从何而来?是哪些人在创造这些机制,又是哪些人在传播这些机制?为什么要这样设计呢?为什么要这样编码呢?
iOS开发是在询问什么问题?技术的实现,究竟是在问什么?为什么要这样做?那样做?评价的标准为何?
资源
我在开发过程中常使用的资源:
- 相关书籍寻找大致实现方向,我有庞大的电子书库,在此感谢学校提供的优质资源。很多书,都会先检视阅读一遍,这样心中有地图,开发时就可快速定位。书本是理论的简单系统化表示。
- Apple 官方文档 视频 示例代码系统化的概览,具体可使用内容的查找。
- StackOverflow主要是查找一些细节问题。
- Github上的相关项目看具体的代码实现,分析不同实现的优缺点,取其精华去其糟粕。
- Raywenderlich可以很快上手入门新知识点。
- 博客
- 论文
- 对于想深入理解的知识,会参考相关论文。
搜索时使用google或者bing,绝对可以节约你的时间。时间即是生命。
我选择的简单是易用SS,我的推介链接。
想法
- 目标:简洁优雅易用节拍器
- 用户:学习乐器演奏的群体
- 使用场景:乐器演奏
- 做什么:小而美。好看,好用,占内存小。
- 不做什么:不做专业程度极高,功能十分完备的节拍器。
设计
我一般使用Sketch进行快速原型设计。
同时思考,是否可实现?
开发
我使用的是coding的仓库,git进行版本管理。
主要介绍2.0版本中的一些开发过程。
你可以免费下载,看看有哪些基本功能。
多主题设计
配色
参考网站:
- https://coolors.co/browser
- http://uigradients.com/#Jonquil
- http://www.rocket-design.fr/color-template/
- http://www.shejidaren.com/examples/tools/color-scheme/
- http://www.shejidaren.com/mbe-style.html
- http://www.peise.net/
扁平化颜色库:
Chameleon is a lightweight, yet powerful, color framework for iOS (Objective-C & Swift). It is built on the idea that software applications should function effortlessly while simultaneously maintaining their beautiful interfaces.
https://github.com/ViccAlexander/Chameleon
多主题实现
OC版:
https://github.com/Draveness/DKNightVersion
Swift版:
http://www.jianshu.com/p/a5cd0176bcf5
https://github.com/zhangbozhb/ChameleonSwift
- theme
- view
部分配色表:
Name | defaultColor | SeaColor | GreenColor | CoffeeColor | |
---|---|---|---|---|---|
backgroundColor | |||||
BlockColor | rgba(184, 184, 184, 1) | #D5EBE9 | #F4ADA2 | #D4C38F | |
BlockColorFill | rgba(251, 251, 251, 1) | #F5FAF9 | #F07973 | #EFDFAF | |
BlockBdrColor | rgba(57, 57, 57, 1) | #38465F | #38465F | #272727 | |
shadowColor | rgba(41, 44, 48, 1) | #38465F | #A0785C | #5D4531 | |
Sliderstart | rgba(184, 184, 184, 1) | #F5FAF9 | #F2F2F2 | #88DEF2 | |
Sliderend | rgba(185, 200, 245, 1) | #B0D5C2 | #F4ADA2 | #FAD199 | |
SliderBackground | Black | #364960 | #3A4C39 | #2B2B2B | |
nameIncDecTextColor | Black | White | Black | Black | |
labelColor | Black | white | Black | Black |
根据图片配色举例:
代码实现:
1.Struct方式
public protocol YXYTheme {
// MetreView
var blockColor : UIColor { get set }
var blockFillColor : UIColor { get set }
var blockBdrColor : UIColor { get set }
var blockShadowColor : UIColor { get set }
// View
var backgroundColor : UIColor { get set }
var nameIncDecTextColor : UIColor { get set }
var labelColor : UIColor { get set }
var incAndDecLabelTextColor : UIColor { get set }
// Slider
var sliderBackgroundColor : UIColor { get set }
var sliderStartColor : UIColor { get set }
var sliderEndColor : UIColor { get set }
var sliderHandleColor : UIColor { get set }
}
struct DarkTheme : YXYTheme, AnyObjectConvertible {
var blockColor = UIColor(red:0.72, green:0.72, blue:0.72, alpha:1)
var blockFillColor = UIColor(red:0.95, green:0.95, blue:0.95, alpha:1)
var blockBdrColor = UIColor(red:0.21, green:0.21, blue:0.21, alpha:1)
var blockShadowColor = UIColor(red:0.16, green:0.17, blue:0.19, alpha:1)
var backgroundColor = UIColor(red:0.34, green:0.34, blue:0.34, alpha:1)
var nameIncDecTextColor = UIColor(red:0.95, green:0.95, blue:0.95, alpha:1)
var labelColor = UIColor.whiteColor()
var incAndDecLabelTextColor = UIColor.whiteColor()
var sliderBackgroundColor = UIColor.lightGrayColor()
var sliderStartColor = UIColor.grayColor()
var sliderEndColor = UIColor.greenColor()
var sliderHandleColor = UIColor.greenColor()
}
2.class 方式
//
// LightTheme.swift
// GuitarFere
//
// Created by youxinyu on 16/3/10.
// Copyright © 2016年 yogayu.github.io. All rights reserved.
//
import UIKit
class LightTheme : NSObject, YXYTheme, AnyObjectConvertible
{
var blockColor = UIColor(red:0.72, green:0.72, blue:0.72, alpha:1)
var blockFillColor = UIColor(red:0.95, green:0.95, blue:0.95, alpha:1)
var blockBdrColor = UIColor(red:0.21, green:0.21, blue:0.21, alpha:1)
var blockShadowColor = UIColor(red:0.16, green:0.17, blue:0.19, alpha:1)
var backgroundColor = UIColor.whiteColor()
var nameIncDecTextColor = UIColor(red:0.95, green:0.95, blue:0.95, alpha:1)
var labelColor = UIColor.whiteColor()
var incAndDecLabelTextColor = UIColor.whiteColor()
var sliderBackgroundColor = UIColor.lightGrayColor()
var sliderStartColor = UIColor.grayColor()
var sliderEndColor = UIColor.redColor()
var sliderHandleColor = UIColor.greenColor()
init( blockColor:UIColor, blockFillColor:UIColor, blockBdrColor:UIColor, blockShadowColor:UIColor,
backgroundColor:UIColor, nameIncDecTextColor:UIColor, labelColor:UIColor, incAndDecLabelTextColor:UIColor,
sliderBackgroundColor:UIColor, sliderStartColor:UIColor, sliderEndColor:UIColor, sliderHandleColor:UIColor){
self.blockColor = blockColor
self.blockFillColor = blockFillColor
self.blockBdrColor = blockBdrColor
self.blockShadowColor = blockShadowColor
self.backgroundColor = backgroundColor
self.nameIncDecTextColor = nameIncDecTextColor
self.labelColor = labelColor
self.incAndDecLabelTextColor = incAndDecLabelTextColor
self.sliderBackgroundColor = sliderBackgroundColor
self.sliderStartColor = sliderStartColor
self.sliderEndColor = sliderEndColor
self.sliderHandleColor = sliderHandleColor
}
}
本地化
参见我之前博文:
http://www.jianshu.com/p/782aaf3bf7da
或
http://azureyu.com/2016-05-14-iOS-Localizable.html
保持用户设置
let userDefaultsLastTempoKey = "DefaultsTempoKey"
let defaults = NSUserDefaults.standardUserDefaults()
func saveTempo(tempo:Int){
defaults.setInteger(tempo, forKey: userDefaultsLastTempoKey)
defaults.synchronize()
}
读取:
func initialTempo(){
let savedTempo = NSUserDefaults.standardUserDefaults().objectForKey( userDefaultsLastTempoKey) as? Int
if let tempo = savedTempo {
metronome.tempo = tempo
tempoLabel.text = "\(metronome.tempo)"
}else {
tempoLabel.text = "\(metronome.tempo)"
}
Struct如何转为AnyObject?
参考:
https://github.com/tarunon/AnyObjectConvertible
class Box<T> {
let value: T
init(value: T) {
self.value = value
}
}
NSNotificationCenter.defaultCenter().postNotificationName("foo", object: Box(value: YourOwnStruct())) // OK
But Box<T> unwrap is too lazy.
let value = (notification.object as? Box<YourOwnStruct>)?.value
You can cast your struct/enum directory if implement AnyObjectConvertible at that type.
extension YourOwnStruct: AnyObjectConvertible {}
NSNotificationCenter.defaultCenter().postNotificationName("foo", object: YourOwnStruct()) // OK
let value = notification.object as? YourOw
存储用户当前主题设置
初始显示,无法使用函数更改,为什么?
解决:因为存的内容不对,主题是一个Struct或Class。
func initTheme() {
let savedTheme = retrieveTheme()
if let theme = savedTheme {
UIApplication.ch_switchTheme(lightTheme)
}else{
// ...
}
}
GuitarFere[20074:736310] * Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘Attempt to insert non-property list object GuitarFere.Box<GuitarFere.YXYTheme> for key DefaultThemeKey’
The code you posted tries to save an array of custom objects to NSUserDefaults. You can’t do that. Implementing the NSCoding methods doesn’t help. You can only store things like NSArray, NSDictionary, NSString, NSData, NSNumber, and NSDate in NSUserDefaults.
You need to convert the object to NSData (like you have in some of the code) and store that NSData in NSUserDefaults. You can even store an NSArray of NSData if you need to.
When you read back the array you need to unarchive the NSData to get back your BC_Personobjects.
http://stackoverflow.com/questions/19720611/attempt-to-set-a-non-property-list-object-as-an-nsuserdefaults
func saveTheme(theme:LightTheme){
// let archivedObject = NSKeyedArchiver.archivedDataWithRootObject((theme as? NSObject)!)
let archivedObject = NSKeyedArchiver.archiveRootObject(theme as NSObject, toFile: userDefaultsLastThemeKey)
defaults.setObject(archivedObject, forKey: userDefaultsLastThemeKey)
defaults.synchronize()
}
存:
func saveTheme(theme:LightTheme){
// let archivedObject = NSKeyedArchiver.archivedDataWithRootObject((theme as? NSObject)!)
let archivedObject = NSKeyedArchiver.archiveRootObject(theme as NSObject, toFile: userDefaultsLastThemeKey)
defaults.setObject(archivedObject, forKey: userDefaultsLastThemeKey)
defaults.synchronize()
}
上面解决方式还是有问题。
突然想到,不用保存主题本身,直接保存是第几个(Int)主题就好。
问题就这样解决了。
摇一摇换肤
override func canBecomeFirstResponder() -> Bool {
return true
}
override func motionBegan(motion: UIEventSubtype, withEvent event: UIEvent?) {
if(event?.subtype == UIEventSubtype.MotionShake) {
randomTheme()
print("shacked")
self.setNeedsStatusBarAppearanceUpdate()
}
}
func randomTheme() {
let max = themes.count - 1
let index = randomIn(min: 0, max: max)
let randomTheme = themes[index]
saveTheme(index)
UIApplication.ch_switchTheme(randomTheme)
}
钟摆绘制
主要使用图像绘制。
例如绘制三角形:
func drawTriangle() {
//1.获得图形上下文
let context = UIGraphicsGetCurrentContext()
//绘制三角形
let height = self.frame.height
let width = self.frame.width
CGContextMoveToPoint(context, 0, 0)
CGContextAddLineToPoint(context, width, height/2)
CGContextAddLineToPoint(context, 0, height)
//关闭路径,闭环,(连接起点和最后一个点)
powerOffColor.setFill()
CGContextClosePath(context)
//显示在view上
CGContextFillPath(context)
}