Capture rotation gesture in IOS development (69)

Source: Internet
Author: User

1 Preface

Create a UIRotationGestureRecognizer monitor and bind it to your view to capture the gesture that the user uses his fingers to rotate on the screen. The UIRotationGestureRecognizer class has a rotation attribute, which can be used to set the orientation of rotation and the arc degree of rotation. When the user's hands start and end, the two properties, such as UIGestureRecognizerStateBegan and UIGestureRecognizerStateEnded, are used.


2. code example
ZYViewController. m

[Plain]
// RotationGestureRecognizer is an instance object for monitoring and capturing our rotating gestures.
@ Synthesize rotationGestureRecognizer;
@ Synthesize helloWorldLabel;
@ Synthesize rotationAngleInRadians;
 
-(Void) viewDidLoad
{
[Super viewDidLoad];
Self. view. backgroundColor = [UIColor whiteColor];
/****** Add the Label component start ******/
Self. helloWorldLabel = [[UILabel alloc] initWithFrame: CGRectZero];
Self. helloWorldLabel. text = @ "Hello, World! ";
Self. helloWorldLabel. font = [UIFont systemFontOfSize: 16366f];
[Self. helloWorldLabel sizeToFit];
// Center
Self. helloWorldLabel. center = self. view. center;
[Self. view addSubview: self. helloWorldLabel];
/****** Add the Label component end ******/
// UIRotationGestureRecognizer gesture identification tool. This class can be used to listen to and capture rotating gestures and add a rotating gesture identification tool, so that users can use gestures to adjust the image position smoothly.
Self. rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget: self action: @ selector (handleRotations :)];
// Add a gesture
[Self. view addGestureRecognizer: self. rotationGestureRecognizer];

}
 
-(Void) handleRotations :( UIRotationGestureRecognizer *) paramSender {
If (self. helloWorldLabel = nil) {return;
}
// Use the CGAffineTransformMakeRotation method to transfer a rotation gesture event. Add the arc of the previous rotation to the current rotation radian.
// The rotationAngleInRadians object is a location object that we need to set for our tag object during rotation, each time we rotate, we save a new position value in this object to achieve a Rotating effect.
Self. helloWorldLabel. transform = CGAffineTransformMakeRotation (self. rotationAngleInRadians + paramSender. rotation );
// Adjust the view position at the end of the gesture
If (paramSender. state = UIGestureRecognizerStateEnded ){
Self. rotationAngleInRadians + = paramSender. rotation;
}
}

// RotationGestureRecognizer is an instance object for monitoring and capturing our rotating gestures.
@ Synthesize rotationGestureRecognizer;
@ Synthesize helloWorldLabel;
@ Synthesize rotationAngleInRadians;

-(Void) viewDidLoad
{
[Super viewDidLoad];
Self. view. backgroundColor = [UIColor whiteColor];
/****** Add the Label component start ******/
Self. helloWorldLabel = [[UILabel alloc] initWithFrame: CGRectZero];
Self. helloWorldLabel. text = @ "Hello, World! ";
Self. helloWorldLabel. font = [UIFont systemFontOfSize: 16366f];
[Self. helloWorldLabel sizeToFit];
// Center
Self. helloWorldLabel. center = self. view. center;
[Self. view addSubview: self. helloWorldLabel];
/****** Add the Label component end ******/
// UIRotationGestureRecognizer gesture identification tool. This class can be used to listen to and capture rotating gestures and add a rotating gesture identification tool, so that users can use gestures to adjust the image position smoothly.
Self. rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget: self action: @ selector (handleRotations :)];
// Add a gesture
[Self. view addGestureRecognizer: self. rotationGestureRecognizer];

}

-(Void) handleRotations :( UIRotationGestureRecognizer *) paramSender {
If (self. helloWorldLabel = nil) {return;
}
// Use the CGAffineTransformMakeRotation method to transfer a rotation gesture event. Add the arc of the previous rotation to the current rotation radian.
// The rotationAngleInRadians object is a location object that we need to set for our tag object during rotation, each time we rotate, we save a new position value in this object to achieve a Rotating effect.
Self. helloWorldLabel. transform = CGAffineTransformMakeRotation (self. rotationAngleInRadians + paramSender. rotation );
// Adjust the view position at the end of the gesture
If (paramSender. state = UIGestureRecognizerStateEnded ){
Self. rotationAngleInRadians + = paramSender. rotation;
}
}
Running result

 
 


After rotation (rotation method, hold down the Option key, hold down the left mouse button, two small balls appear, and then drag to rotate) Result

 
 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.