Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍
1.UITapGestureRecognizer点击/双击手势
vartapGesture=UITapGestureRecognizer(target:self,action:"handleTapGesture:") //设置手势点击数,双击:点2下 tapGesture.numberOfTapsRequired=2 self.view.addGestureRecognizer(tapGesture)
2.UIPinchGestureRecognizer捏(放大/缩小)手势
varpinchGesture=UIPinchGestureRecognizer(target:self,action:"handlePinchGesture:") self.view.addGestureRecognizer(pinchGesture)
3.UIRotationGestureRecognizer旋转手势
varrotateGesture=UIRotationGestureRecognizer(target:self,action:"handleRotateGesture:") self.view.addGestureRecognizer(rotateGesture)
4.UIPanGestureRecognizer拖动手势
varpanGesture=UIPanGestureRecognizer(target:self,action:"handlePanGesture:") self.view.addGestureRecognizer(panGesture)
5.UISwipeGestureRecognizer划动手势
varswipeGesture=UISwipeGestureRecognizer(target:self,action:"handleSwipeGesture:") swipeGesture.direction=UISwipeGestureRecognizerDirection.Left//不设置是右 self.view.addGestureRecognizer(swipeGesture)
6.UILongPressGestureRecognizer长按手势
varlongpressGesutre=UILongPressGestureRecognizer(target:self,action:"handleLongpressGesture:") //长按时间 //longpressGesutre.minimumPressDuration //所需触摸次数 ///longpressGesutre.numberOfTouchesRequired self.view.addGestureRecognizer(longpressGesutre) UIGestureRecognizerState枚举定义如下
enumUIGestureRecognizerState:Int{
casePossible//therecognizerhasnotyetrecognizeditsgesture,butmaybeevaluatingtouchevents.thisisthedefaultstate
caseBegan//therecognizerhasreceivedtouchesrecognizedasthegesture.theactionmethodwillbecalledatthenextturnoftherunloop caseChanged//therecognizerhasreceivedtouchesrecognizedasachangetothegesture.theactionmethodwillbecalledatthenextturnoftherunloop caseEnded//therecognizerhasreceivedtouchesrecognizedastheendofthegesture.theactionmethodwillbecalledatthenextturnoftherunloopandtherecognizerwillberesettoUIGestureRecognizerStatePossible caseCancelled//therecognizerhasreceivedtouchesresultinginthecancellationofthegesture.theactionmethodwillbecalledatthenextturnoftherunloop.therecognizerwillberesettoUIGestureRecognizerStatePossible
caseFailed//therecognizerhasreceivedatouchsequencethatcannotberecognizedasthegesture.theactionmethodwillnotbecalledandtherecognizerwillberesettoUIGestureRecognizerStatePossible }