Developing iOS 8 Apps with Swift Lesson 11學習
Lesson11:Unwind Segues,Alerts,Timers,View Animation
1、UIAlertController簡介
UIAlertController有Alert和ActionSheet兩種樣式,在iOS系統中,Alert樣式是直接彈窗到螢幕中間,起到一個提示警告的作用。而ActionSheet是從螢幕底部彈出來的,給使用者提供更多的選項功能。
下面通過做一個Alert樣式的Demo來看看如何用UIAlertController。
Demo描述:點選導航欄右上角的+號,彈出Alert框,同時Alert窗中有文字輸入框,通過點選Login,可以獲得文字框的值並且在控制檯打印出來。
關鍵程式碼如下:
func loadAlert(){ let alertController = UIAlertController(title: "Alert", message: "Alert Message", preferredStyle: UIAlertControllerStyle.Alert) //登入action let loginAciton = UIAlertAction(title: "Login", style: .Default) { (action:UIAlertAction) -> Void in guard let text = alertController.textFields?.first?.text else{ return } //輸出輸入文字框的內容 print(text) } alertController.addAction(loginAciton) //取消action let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil) alertController.addAction(cancelAction) //給Alert新增文字輸入框 alertController.addTextFieldWithConfigurationHandler { (textField:UITextField) -> Void in } presentViewController(alertController, animated: true, completion: nil) }
PS:iPad上書寫ActionSheet樣式和iPhone上有點不同,ipad上面ActionSheet的UIAlertController是從右上角彈出來的,並且懸浮。在書寫的時候,需要多添幾行程式碼。而iPhone和iPad上面的Alert樣式編寫程式碼的時候是一致。
2、NSTimer
NSTimer定時器主要用到的技術是runloop,在主執行緒之中使用。經常會用到NSTimer,比如說在做迴圈滾動新聞的時候,需要讓它每隔一小段時間自動執行滾動操作。
NSTimer使用示例如下:
override func viewDidLoad() { super.viewDidLoad() //這樣採用Alert樣式,actionsheet同理 self.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: self, action: "loadAlert") /** 定時器 - parameter <Tti: 多長時間間隔執行一次 - parameter target: 執行的物件 - parameter selector: 方法 - parameter userInfo: 使用者資訊 - parameter repeats: 是否重複執行 */ NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: "fire:", userInfo: "zengchao", repeats: true) } //tolerance:容忍誤差,讓系統選擇一個更高效的時間來執行 func fire(time:NSTimer){ time.tolerance = 0.3 print(time.userInfo as! String) }
NSTimer的tolerant屬性是容忍誤差,讓系統選擇一個更高效的時間來執行。
3、Animation
1)UIView 檢視動畫
UIView檢視動畫其實就是對Core Animation進行封裝,使用起來很簡單。只需要設定好動畫之前和動畫之後的屬性,然後動畫是由系統生成的,稱為補間動畫。一般的動畫用UIView Animation就夠了,只是自由度會降低。UIView的基礎動畫中,使用的變化屬性通常有位置和大小如bounds/frame/center,外觀如backgroundColor/alpha,轉換transform如translation/rotation/scale。
下面看幾個UIView的檢視動畫示例:
A:transform屬性的型別為CGAffineTransform,其中包括:rotation旋轉和scale縮放
案例:
let rotation = CGAffineTransformMakeRotation(CGFloat(M_PI))
let scale = CGAffineTransformMakeScale(0.5, 0.5)//縮放
UIView.animateWithDuration(1, delay: 0.5, options: [UIViewAnimationOptions.Repeat,UIViewAnimationOptions.Autoreverse ], animations: { () -> Void in
self.button.frame = CGRectMake(0, self.gHeight / 2, self.gWidth / 2, 50)
self.button.backgroundColor = UIColor(red: 23/255, green: 233/255, blue: 233/255, alpha: 1)
self.button.transform = scale
}, completion: nil)
B、Spring Animation 彈性動畫
UIView.animateWithDuration(2, delay: 0.5, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions.Repeat, animations: { () -> Void in
}, completion: nil)
C、Transition Animation 轉場動畫
UIView.transitionFromView(UIView, toView: UIView, duration: NSTimeInterval, options: UIViewAnimationOptions, completion: nil)
D、Keyframe Animation 關鍵幀動畫
UIView.animateKeyframesWithDuration(NSTimeInterval, delay: NSTimeInterval, options: UIViewKeyframeAnimationOptions, animations: { () -> Void in
UIView.addKeyframeWithRelativeStartTime(Double, relativeDuration: Double, animations: { () -> Void in
//code
})
}, completion: nil)
E、貝塞爾曲線運動
2)ViewController轉場動畫
enum UIModalTransitionStyle: Int {
case CoverVertical // 底部滑入,預設
case FlipHorizontal // 水平翻轉
case CrossDissolve // 隱出隱現
case PartialCurl // 翻頁
}
3)Core Animation
Core Animation核心動畫也包括基本動畫、轉場動畫、關鍵幀動畫、組動畫、彈性動畫。
CAAnimation實現了CAMediaTiming協議(三個子類)如下:
CAPropertyAnimation:CABasicAnimation/CAKeyFrameAnimation
CAAnimationGroup
CATransition(
4)Dynamic Animation(Lesson12內容)