ios - Call function when SwipeGestureRecognizer swipe speed passes threshold -
i'm programming app, contains uiview
uiswipegesturerecognizer
i want recognizer recognize how fast user dragging in recognizer's direction. when speed high enough (past specific threshold) custom action should occur. it's same in this post, need written in swift.
how translate swift or there better way it?
current code, xcode errors marked comments.
touches began:
override func touchesbegan(touches: set<nsobject>, withevent event: uievent) { //avoid multi-touch gesture if(touches.count > 1){ return; } if let touch:uitouch = touches.first as? uitouch{ let locator:cgpoint = touch.locationinview(self.view!) start = locator starttime = touch.timestamp }
touches ended:
override func touchesended(touches: set<nsobject>, withevent event: uievent) { if let touch:uitouch = touches.first as? uitouch{ let locator:cgpoint = touch.locationinview(self.view!) var dx:cgfloat = (locator.x - start!.x); var dy:cgfloat = (locator.y - start!.y); var magnitude:cgfloat = sqrt(dx*dx+dy*dy) if (magnitude >= kmindistance) { // determine time difference start of gesture var dt:cgfloat = cgfloat(touch.timestamp - starttime!) if (dt > kminduration) { // determine gesture speed in points/sec var speed:cgfloat = magnitude / dt; if (speed >= kminspeed && speed <= kmaxspeed) { // swipe detected swipedview() }}}}}
var start:cgpoint? var starttime:nstimeinterval? var kmindistance:cgfloat = 25.0 var kminduration:cgfloat = 0.1 var kminspeed:cgfloat = 100.0 var kmaxspeed:cgfloat = 500.0 override func touchesbegan(touches: set<nsobject>, withevent event: uievent) { //avoid multi-touch gesture if(touches.count > 1){ return; } if let touch:uitouch = touches.first as? uitouch{ let location:cgpoint = touch.locationinview(self.view!) start = location starttime = touch.timestamp } } override func touchesended(touches: set<nsobject>, withevent event: uievent) { if let touch:uitouch = touches.first as? uitouch{ let location:cgpoint = touch.locationinview(self.view!) var dx:cgfloat = location.x - start!.x; var dy:cgfloat = location.y - start!.y; var magnitude:cgfloat = sqrt(dx*dx+dy*dy) if (magnitude >= kmindistance) { // determine time difference start of gesture var dt:cgfloat = cgfloat(touch.timestamp - starttime!) if (dt > kminduration) { // determine gesture speed in points/sec var speed:cgfloat = magnitude / dt; if (speed >= kminspeed && speed <= kmaxspeed) { // swipe detected } } } } }
honesty need has not been tested needed code converted on go.
edit: tested , seems work swift 1.2
please read comments underneath question , read how ask next question. lucky time needed code :)
Comments
Post a Comment