Cover 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

O'Reilly logo

7.2. Detecting Rotation Gestures

Problem

You want to detect when a user is attempting to rotate an element on the screen using her fingers.

Solution

Create an instance of the UIRotationGestureRecognizer class and attach it to your target view:

- (void)viewDidLoad {
  [super viewDidLoad];
  
  self.view.backgroundColor = [UIColor whiteColor];
    self.helloWorldLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  self.helloWorldLabel.text = @"Hello, World!";
  self.helloWorldLabel.font = [UIFont systemFontOfSize:16.0f];
  [self.helloWorldLabel sizeToFit];
  self.helloWorldLabel.center = self.view.center;
  [self.view addSubview:self.helloWorldLabel];
  
  self.rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] 
                                    initWithTarget:self
                                    action:@selector(handleRotations:)];
  
  [self.view addGestureRecognizer:self.rotationGestureRecognizer];
  
}

- (void) viewDidUnload{
  [super viewDidUnload];  
  self.helloWorldLabel = nil;
  self.rotationGestureRecognizer = nil;
}

Discussion

The UIRotationGestureRecognizer, as its name implies, is the perfect candidate among gesture recognizers to detect rotation gestures and to help you build more intuitive graphical user interfaces. For instance, when the user encounters an image on the screen in your application in full-screen mode, it is quite intuitive for him to attempt to correct the orientation by rotating the image.

The UIRotationGestureRecognizer class implements a property named rotation that specifies the total amount and direction of rotation requested by the user’s gesture, ...

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