Monday, 15 February 2010

ios - UIPanGestureRecognizer stop calling the selector -



ios - UIPanGestureRecognizer stop calling the selector -

i'm having problem work uipangesturerecognizer calls selector when have finger moving, want maintain calling selector finger standing @ same place.

there 4 objects on screen 1 @ top, 1 @ right side, 1 @ left side , 1 @ bottom. have object @ center of screen (this 1 i'm moving pangesture). when object touches others want give me log, works when touches if maintain finger @ same place stops give me logs, if move little starts give me logs again.

is there anyway can maintain calling selector finger @ same place?

here code example:

- (void)moveobject:(uipangesturerecognizer *)sender { cgpoint translation = [sender translationinview:self.limitedirecional]; [sender settranslation:cgpointmake(0, 0) inview:self.limitedirecional]; cgpoint center = sender.view.center; center.y += translation.y; int ymin = 0; int ymax = self.limitedirecional.frame.size.height; if (center.y < ymin || center.y > ymax ) return; sender.view.center = center; center.x += translation.x; int xmin = self.limitedirecional.frame.size.width; int xmax = 0; if (center.x > xmin || center.x < xmax) return; sender.view.center = center; if (cgrectintersectsrect(sender.view.frame,self.top.frame)) { nslog(@"top"); } if (cgrectintersectsrect(sender.view.frame,self.botton.frame)) { nslog(@"botton"); } if (cgrectintersectsrect(sender.view.frame,self.right.frame)) { nslog(@"right"); } if (cgrectintersectsrect(sender.view.frame,self.left.frame)) { nslog(@" left"); } if (sender.state == uigesturerecognizerstateended) { sender.view.center = cgpointmake(self.view.frame.size.width / 2, self.view.frame.size.height / 2); } }

i'm not exclusively next logic of routine, i'll provide generic template of solution might when want continuous events in middle of gesture, whether user moving finger or not. can adapt technique own purposes.

this uses cadisplaylink, considered improve technique animation older technique of using nstimer. utilize cadisplaylink, though, need add needed framework, quartzcore.framework, project, if haven't already. note in gesture recognizer, i'm checking state of gesture, know whether we're starting gesture, in middle of one, or ending one:

#import "viewcontroller.h" #import <quartzcore/quartzcore.h> @interface viewcontroller () @property (nonatomic, strong) cadisplaylink *displaylink; @property (nonatomic) cgpoint translationinview; @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; uipangesturerecognizer *gesture = [[uipangesturerecognizer alloc] initwithtarget:self action:@selector(handlegesture:)]; // i'm adding main view, add together whatever want [self.view addgesturerecognizer:gesture]; } - (void)startdisplaylink { self.displaylink = [cadisplaylink displaylinkwithtarget:self selector:@selector(handledisplaylink:)]; [self.displaylink addtorunloop:[nsrunloop currentrunloop] formode:nsdefaultrunloopmode]; } - (void)stopdisplaylink { [self.displaylink invalidate]; self.displaylink = nil; } - (void)handledisplaylink:(cadisplaylink *)displaylink { nslog(@"%s translationinview = %@", __function__, nsstringfromcgpoint(self.translationinview)); // here whatever need happen continuously while user in // middle of gesture, whether finger moving or not. } - (void)handlegesture:(uipangesturerecognizer *)gesture { self.translationinview = [gesture translationinview:gesture.view]; if (gesture.state == uigesturerecognizerstatebegan) { [self startdisplaylink]; // whatever other initialization stuff user starts gesture // (e.g. might alter appearance of joystick provide // visual feedback they're controlling joystick). } else if (gesture.state == uigesturerecognizerstatechanged) { // here stuff changes user moving // finger in middle of gesture, don't need have // repeatedly done while user's finger not moving (e.g. maybe // visual motion of "joystick" command on screen). } else if (gesture.state == uigesturerecognizerstateended || gesture.state == uigesturerecognizerstatecancelled || gesture.state == uigesturerecognizerstatefailed) { [self stopdisplaylink]; // whatever other cleanup want when user stops gesture // (e.g. maybe animating moving of joystick center). } } @end

you can accomplish similar effect if utilize nstimer, too. whatever works improve you.

ios objective-c uigesturerecognizer uipangesturerecognizer

No comments:

Post a Comment