本文主要是介绍8.2 Detecting Rotation Gestures,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
旋转手势
@interface ViewController ()
@property (nonatomic, strong) UIRotationGestureRecognizer *rotationGestureRecognizer;
@property (nonatomic, strong) UILabel *helloWorldLabel;
/* We can remove the nonatomic and the unsafe_unretained marks from this property declaration. On a float value, the compiler will generate both these for us automatically */
@property (nonatomic, unsafe_unretained) CGFloat rotationAngleInRadians;
@end
@implementation ViewController
- (void) handleRotations:(UIRotationGestureRecognizer *)paramSender{
NSLog(@"%@ \nrotation=%f \nvelocity=%f",paramSender,paramSender.rotation,paramSender.velocity);
if (self.helloWorldLabel == nil){
return;
}
/* Take the previous rotation and add the current rotation to it */
self.helloWorldLabel.transform = CGAffineTransformMakeRotation(self.rotationAngleInRadians +
paramSender.rotation);
/* At the end of the rotation, keep the angle for later use */
if (paramSender.state == UIGestureRecognizerStateEnded){
self.rotationAngleInRadians += paramSender.rotation;
NSLog(@"rotationAngleInRadians=%f",self.rotationAngleInRadians);
}
}
- (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:66.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)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
输出:
第一把旋转:
2014-04-10 09:35:01.240 cookbook[507:907] <UIRotationGestureRecognizer: 0x18faf0; state = Began; view = <UIView 0x386250>; target= <(action=handleRotations:, target=<ViewController 0x3842b0>)>>
rotation=0.001488
velocity=0.350469
2014-04-10 09:35:01.255 cookbook[507:907] <UIRotationGestureRecognizer: 0x18faf0; state = Changed; view = <UIView 0x386250>; target= <(action=handleRotations:, target=<ViewController 0x3842b0>)>>
rotation=0.018048
velocity=0.518359
……..
……..
2014-04-10 09:35:02.487 cookbook[507:907] <UIRotationGestureRecognizer: 0x18faf0; state = Changed; view = <UIView 0x386250>; target= <(action=handleRotations:, target=<ViewController 0x3842b0>)>>
rotation=1.026013
velocity=0.039118
2014-04-10 09:35:02.503 cookbook[507:907] <UIRotationGestureRecognizer: 0x18faf0; state = Changed; view = <UIView 0x386250>; target= <(action=handleRotations:, target=<ViewController 0x3842b0>)>>
rotation=1.026013
velocity=0.039118
2014-04-10 09:35:02.519 cookbook[507:907] <UIRotationGestureRecognizer: 0x18faf0; state = Ended; view = <UIView 0x386250>; target= <(action=handleRotations:, target=<ViewController 0x3842b0>)>>
rotation=1.026013
velocity=0.039118
2014-04-10 09:35:02.521 cookbook[507:907] rotationAngleInRadians=1.026013
第二把旋转:
2014-04-10 09:38:56.187 cookbook[507:907] <UIRotationGestureRecognizer: 0x18faf0; state = Began; view = <UIView 0x386250>; target= <(action=handleRotations:, target=<ViewController 0x3842b0>)>>
rotation=0.000381
velocity=0.158696
2014-04-10 09:38:56.203 cookbook[507:907] <UIRotationGestureRecognizer: 0x18faf0; state = Changed; view = <UIView 0x386250>; target= <(action=handleRotations:, target=<ViewController 0x3842b0>)>>
rotation=0.002872
velocity=0.157695
2014-04-10 09:38:56.206 cookbook[507:907] <UIRotationGestureRecognizer: 0x18faf0; state = Changed; view = <UIView 0x386250>; target= <(action=handleRotations:, target=<ViewController 0x3842b0>)>>
rotation=0.002872
velocity=0.157695
……
…...
2014-04-10 09:38:56.619 cookbook[507:907] <UIRotationGestureRecognizer: 0x18faf0; state = Changed; view = <UIView 0x386250>; target= <(action=handleRotations:, target=<ViewController 0x3842b0>)>>
rotation=0.074877
velocity=0.064103
2014-04-10 09:38:56.683 cookbook[507:907] <UIRotationGestureRecognizer: 0x18faf0; state = Ended; view = <UIView 0x386250>; target= <(action=handleRotations:, target=<ViewController 0x3842b0>)>>
rotation=0.074877
velocity=0.064103
2014-04-10 09:38:56.685 cookbook[507:907] rotationAngleInRadians=1.100890
CGAffineTransformMakeRotation
看到这个函数了没,学校里学的转换的知识还记得吗,要不要赶紧复习下
这篇关于8.2 Detecting Rotation Gestures的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!