您的位置:首页 > 移动开发 > IOS开发

iOS 旋转手势

2016-03-09 15:53 375 查看
iOS的手势功能很强大,大家都知道。但是如何使用了?

这里 我们简单的以一个UILabel测试一下旋转

下节 我们将简单讲一讲 拖动手势 有兴趣可以看看:http://blog.csdn.net/lwjok2007/article/details/50836883

使用Xcode 创建一个工程 起名testRotation





我们在默认生成的ViewController中写代码



首先我们创建两个变量

UILabel *testLable; //label用来测试旋转
CGFloat testLabelRotation; //label的旋转角度


在viewDidLoad 中创建

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

//创建lable 添加到View
testLable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 120, 36)];
testLable.center=CGPointMake(self.view.bounds.size.width/2.0, self.view.bounds.size.height/2.0);
testLable.textAlignment=NSTextAlignmentCenter;
testLable.text = @"金轱辘棒棒辘轱金";
[testLable sizeToFit];
[self.view addSubview:testLable];

//创建UIRotationGestureRecognizer 用来实现旋转手势
UIRotationGestureRecognizer *rotationGR = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAct:)];
[self.view addGestureRecognizer:rotationGR];

}


在创建方法实现接收旋转事件后的操作

//当旋转时执行此方法
- (void)rotationAct:(UIRotationGestureRecognizer *)sender{

//根据传回的旋转角度修改lable的当前角度
testLable.transform = CGAffineTransformMakeRotation(testLabelRotation+sender.rotation);

//当旋转结束后更新lable的角度,以备下次使用
if (sender.state == UIGestureRecognizerStateEnded) {
testLabelRotation += sender.rotation;
}
}


好了 运行项目试试 看能不能旋转

源代码我会上传到群空间,大家有兴趣可以去下载

源代码名称:【60309旋转手势Rotation.zip】

好了,大家可以去试试

苹果开发群 :414319235  欢迎加入,共同学习
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息