Sunday, 15 February 2015

iphone - Making a rubbing screen gesture in iOS? -



iphone - Making a rubbing screen gesture in iOS? -

basically, want create mobile game using custom hand gesture. gesture kind of rubbing screen, left right , right left.

if gesture moves left right, phone call method , add together point. , 1 time gesture rubs right left, user can 1 point.

but problem is, when utilize swipe gesture recognizer, have release finger screen, in order phone call method. if rub gesture, unable phone call method add together points.

instead, seek touchesbegan, touchesmoved method observe finger's position. however, touchesmoved create many points compare starting point, leads calling methods many times instead of once.

override func touchesbegan(touches: nsset, withevent event: uievent) { touch: anyobject in touches { self.touchstartpoint = touch.locationinview(self.myview).x } } override func touchesmoved(touches: nsset, withevent event: uievent) { touch: anyobject in touches { self.touchoffsetpoint = touch.locationinview(self.myview).x - touchstartpoint if temptouchoffsetpoint < touchoffsetpoint { var xvalueincreasearray: nsmutablearray = [] xvalueincreasearray.addobject(touchoffsetpoint) var maxvalue: double = (xvalueincreasearray anyobject).valueforkeypath("@max.self") double println("\(maxvalue)") if maxvalue - double (self.touchstartpoint) > 50 { println("right") } println("right") } else if temptouchoffsetpoint > touchoffsetpoint { /* var xvaluedecreasearray: nsmutablearray = [] xvaluedecreasearray.addobject(touchoffsetpoint)*/ println("left") } else if temptouchoffsetpoint == touchoffsetpoint { println("remain") } temptouchoffsetpoint = touchoffsetpoint }

are there ways observe rubbing gesture? , every time finger turns direction, phone call method 1 time add together score user? give thanks much!

this works me:

let deadzone:cgfloat = 10.0 var previoustouchpoint: cgfloat! var ismovingleft:bool? = nil override func touchesmoved(touches: nsset, withevent event: uievent) { allow point = touches.anyobject()?.locationinview(self) if allow newpoint = point?.x { if previoustouchpoint == nil { println("started") previoustouchpoint = newpoint } else { // check if have moved beyond dead zone if (newpoint < (previoustouchpoint - deadzone)) || (newpoint > (previoustouchpoint + deadzone)) { allow newdirectionisleft = newpoint < previoustouchpoint // check if direction has changed if ismovingleft != nil && newdirectionisleft != ismovingleft! { println("direction changed: point") } println((newdirectionisleft) ? "moving left" : "moving right") ismovingleft = newdirectionisleft previoustouchpoint = newpoint } } } } override func touchescancelled(touches: nsset!, withevent event: uievent!) { previoustouchpoint = nil ismovingleft = nil } override func touchesended(touches: nsset, withevent event: uievent) { previoustouchpoint = nil ismovingleft = nil }

ios iphone swift uigesturerecognizer gesture

No comments:

Post a Comment