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