有天忙太晚所以我的鐵人卡在7天了
無法當鐵人了已哭
來源: http://furnacedigital.blogspot.tw/2012/06/uigesturerecognizer.html
//點擊動作手勢辨識
UITapGestureRecognizer;
//掐擠動作的手勢辨識
UIPinchGestureRecognizer;
//旋轉動作的手勢辨識
UIRotationGestureRecognizer;
//滑動動作的手勢辨識
UISwipeGestureRecognizer;
//拖曳動作的手勢辨識
UIPanGestureRecognizer;
//久按動作的手勢辨識
UILongPressGestureRecognizer;
利用didSet
action 你要執行的方法當手勢成功觸碰
self 你要提醒的目標
@IBOutlet var faceView: FaceView! {
didSet{
let recognizer = UIPanGestureRecognizer(target: self, action: #selector(FaceView.SomeFunc(_:)))
faceView.addGestureRecognizer(recognizer)
}
}
另一種是直接從storyboard拉出來使用在hook到controller裡
這裡新增了手勢控制臉部表情
嘴巴控制微笑
這裡加入兩個手勢分別是上跟下
let happierSwapRecognizer = UISwipeGestureRecognizer (target: self, action: #selector(FaceViewController.IncreaseHappy(_:)))
happierSwapRecognizer.direction = .up
faceView.addGestureRecognizer(happierSwapRecognizer)
let saderSwapRecognizer = UISwipeGestureRecognizer (target: self, action: #selector(FaceViewController.IncreaseSad(_:)))
saderSwapRecognizer.direction = .down
faceView.addGestureRecognizer(saderSwapRecognizer)
成功觸發則會呼叫這兩個func控制嘴巴
func IncreaseHappy(_ recognizer : UISwipeGestureRecognizer) {
expression.mouth = FacialExpression.MouthSmile.Happy
print("up")
}
func IncreaseSad(_ recognizer : UISwipeGestureRecognizer) {
expression.mouth = FacialExpression.MouthSmile.Sad
print("down")
}
再來又加了縮放功能
faceView.addGestureRecognizer( UIPinchGestureRecognizer(target: self.faceView, action: #selector(FaceView.ChangeScale(_:))))
一樣成功會呼叫FaceView裡的縮放
這邊scale會重置變1好讓縮放比例等量前進
public func ChangeScale(_ recognizer : UIPinchGestureRecognizer){
switch recognizer.state {
case .changed :
scale *= recognizer.scale
print("scaling...\(scale)")
recognizer.scale = 1.0
default:
break
}
}
最後是點按手勢控制眼睛閉合
這是從StoryBoard直接拉的
@IBAction func eyeToggle(_ sender: UITapGestureRecognizer) {
if sender.state == .ended{
switch expression.eye {
case .Open:
expression.eye = .Closed
default:
expression.eye = .Open
}
}
}