O'Reilly logo

iOS 5 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

7.3. Detecting Panning and Dragging Gestures

Problem

You want the users of your application to be able to move GUI elements around using their fingers.

Note

Pan gestures are continuous movements of fingers on the screen; recall that swipe gestures were discrete gestures. This means the method set as the target method of a pan gesture recognizer gets called repeatedly from the beginning to the end of the recognition process.

Solution

Use the UIPanGestureRecognizerclass:

- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; /* Let's first create a label */ CGRect labelFrame = CGRectMake(0.0f, /* X */ 0.0f, /* Y */ 150.0f, /* Width */ 100.0f); /* Height */ self.helloWorldLabel = [[UILabel alloc] initWithFrame:labelFrame]; self.helloWorldLabel.text = @"Hello World"; self.helloWorldLabel.backgroundColor = [UIColor blackColor]; self.helloWorldLabel.textColor = [UIColor whiteColor]; self.helloWorldLabel.textAlignment = UITextAlignmentCenter; /* Make sure to enable user interaction; otherwise, tap events won't be caught on this label */ self.helloWorldLabel.userInteractionEnabled = YES; /* And now make sure this label gets displayed on the view */ [self.view addSubview:self.helloWorldLabel]; /* Create the Pan Gesture Recognizer */ self.panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGestures:)]; /* At least and at most we need only one finger to activate the pan gesture recognizer */ self.panGestureRecognizer.minimumNumberOfTouches ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required