一些storyboard的trick
- @IBDesignable 可以在storyboard中预览代码绘图效果(有点tricky,如果画不出来也是有可能的(绘图依赖某些复杂依赖关系))
- @IBInspectable 让某些变量的设置可以在Storyboard中进行,类似于storyboard中原生的Attribute aspect
Extensions
- 对于新手来说,Extension主要功能在于
- 增加某些辅助功能,辅助函数
- 注意
- 在extension中overwrite是不行的
- 定义property只能是computed,不能有storage
- 对于大神来说Extension是一种架构的方式,具体可在架构书中学习
Protocols and Delegation
Protocol书写中需要注意的地方
protocol SomeProtocol:(class), InheritedProtocol {
var property:Int {get,set}
func aMethod(arg1:Double, ...) -> SomeType
mutating func change()
init(arg:Type)
}
- class表示只有类可以遵守的协议
- property要标明get,set还是get
- 对于在结构体和枚举中使用的协议,如果要修改property,需要标注 mutating
Delegation思想的使用方法
- Create a delegation protocol(defines what the View wants the Controller to take care of)
- Create a delegate property in the View whose type is that delegation protocol
- Use the delegation property in the View to get/do
- Controller declares that it implements the protocol things it can’t own or control
- Controllers set self as the delegation of the View
??运算符
expression1 ?? expression2
相当于
if expression1 = nil
return expression2
else
return expression1
Gestures
UIPanGestureRecognizer
- translationInView(view:UIView) -> CGPoint
- velocityInView(view:UIView) -> CGPoint
- setTranslation(translation:CGPoint, inView:UIView)
- state:UIGestureRecognizerState
- .Possible
- .Recognized(discrete)
- (continues) .Begin .Changed .Ended
- .Failed
- .Cancelled(Phone call in)
- 对于state的处理
switch gesture.state
case .Changed:fallthrough
case .Ended:
let translation = gesture.translationInView(aView)
//update
gesture.setTranslation(CGPointZero,inView:aView)
default:break
UIPinchGestureRecognizer
- var scale:CGFloat
- var velocity:CGFloat{get}
UIRotationGestureRecognizer
- var rotation:CGFloat //in radians
- var velocity:CGFloat{get} //in radians
UISwipeGestureRecognizer
- var direction(set when init):UISwipeGestureRecognizerDirection
- var numberOfTouchesRequired:Int //finger count
- 观察state .Recognized状态
UITapGestureRecognizer
- var numberOfTapsRequired:Int //点击次数 If Double,check out .Ended
- var numberOfTouchesRequired:Int //Finger Count