ios - Issue with UIControlEventTouchDragOutside implementation -
i have 2-d button array in main view , want able move them dragging inside view. buttons pretty narrow, around 23x23 pixels. have added targets control events touchdragexit
, touchdragoutside
fired many pixels after moved finger outside button. hence subclassed uibutton
, overrode continuetrackingwithtouch
code found in so. notice don't call superclass implementation.
-(bool)continuetrackingwithtouch:(uitouch *)touch withevent:(uievent *)event { cgfloat boundsextension = 5.0f; cgrect outerbounds = cgrectinset(self.bounds, -1 * boundsextension, -1 * boundsextension); bool touchoutside = !cgrectcontainspoint(outerbounds, [touch locationinview:self]); if(touchoutside) { bool previoustouchinside = cgrectcontainspoint(outerbounds, [touch previouslocationinview:self]); if(previoustouchinside) { [self sendactionsforcontrolevents:uicontroleventtouchdragexit]; } else { [self sendactionsforcontrolevents:uicontroleventtouchdragoutside]; } } return self.tracking; }
both events fired correctly uicontroleventtouchdragoutside
action receives wrong touch info when action called. here first part of implementation:
-(void) dragoutside:(uibutton *) sender withevents:(uievent *) event { uitouch *touch = [[event alltouches] anyobject]; cgpoint relativeloctoinitialcell = [touch locationinview:sender]; nslog(@"translation %f %f numtouch %d",relativeloctoinitialcell.x, relativeloctoinitialcell.y, [[event alltouches] count]); ... }
[[event alltouches] count]
still returns 0 until move finger away button. of course relativeloctoinitialcell
fields x
, y
both 0, values use later computations. idea might problem?
edit: deleted answer suggested using uipangesturerecognizer
instead of touchdrag events, couldn't use because firing rapidly before handling 1 event got one.
i ended having handle uicontroleventtouchdraginside
event. though looks workaround, thinking on fundamentals of these touch drag events makes reasonable solution. drag events sourced touchesbegan, touchesmoved , touchesended
. apple added threshold before uicontroleventtouchdragoutside
sent, until threshold uicontroleventtouchdraginside
event sent.
my solution remove overridden continuetrackingwithtouch
method , go on default implementation. handled both inside , outside events, , moved initialization code exit event inside event, , added control statements in inside event. abstracted code looks below:
-(void) draginside:(uibutton *) sender withevents:(uievent *) event { if(stillinsideoriginalcontrol) return; uitouch *touch = [[event alltouches] anyobject]; cgpoint relativeloctoinitialcell = [touch locationinview:sender]; [self processtouch]; } -(void) dragoutside:(uibutton *) sender withevents:(uievent *) event { uitouch *touch = [[event alltouches] anyobject]; cgpoint relativeloctoinitialcell = [touch locationinview:sender]; [self processtouch]; }
Comments
Post a Comment