Adding gesturerecongnizer programmatically

I know how to add a gesture recognizer through IB, but I'm trying to figure it out with IB.

So basically what is now

blue1.userInteractionEnabled = YES; UIPanGestureRecognizer *pgr = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]; [blue1 addGestureRecognizer:pgr]; [pgr release]; 

and my handlePan

 -(IBAction)handlePan:(UIPanGestureRecognizer *)recognizer { CGPoint translation = [recognizer translationInView:self.view]; recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y); [recognizer setTranslation:CGPointMake(0, 0) inView:self.view]; } 

this works great if I do it on IB and I can shoot the fragment around.

I do not understand what makes it not move, as if it is now encoded.

Any help is appreciated.

I also tried - (void) instead of - (IBAction) in my handlePan, but that didn't work either.

+6
source share
1 answer

The fact that this works in IB but not in the code suggests that the error is in the first block of code. My suspicion would be that blue1 is nil . Make sure you are not trying to change the views before viewDidLoad . All of them will be nil in initWithFrame: and awakeFromNib .

+5
source

Source: https://habr.com/ru/post/908844/


All Articles