課程筆記文集地址:Udemy課程:The Complete iOS 9 Developer Course - Build 18 Apps
這節(jié)課沒有用到 Storyboard等限,全部用代碼實現(xiàn)的嫁蛇。
滑動手勢
創(chuàng)建手勢只需要三行代碼(關(guān)鍵點是 UISwipeGestureRecognizer):
// 一:創(chuàng)建滑動手勢
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.swiped(_:)))
// 二:設置手勢滑動的方向
swipeRight.direction = UISwipeGestureRecognizerDirection.Right
// 三:把手勢添加到當前界面
self.view.addGestureRecognizer(swipeRight)
//再創(chuàng)建一個上滑的手勢
let swipeUp = UISwipeGestureRecognizer(target: self, action: #selector(ViewController.swiped(_:)))
swipeUp.direction = UISwipeGestureRecognizerDirection.Up
self.view.addGestureRecognizer(swipeUp)
給手勢添加方法:
func swiped(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.Right:
print("User swiped Right")
case UISwipeGestureRecognizerDirection.Up:
print("User swiped Up")
default:
break
}
}
}
搖一搖
搖一搖比手勢簡單多了,代碼如下:
override func motionEnded(motion: UIEventSubtype, withEvent event: UIEvent?) {
if event?.subtype == UIEventSubtype.MotionShake {
// 在這里寫搖一搖之后要執(zhí)行的事件
print("Device was shaken")
}
}