IOS开发(69)之捕获旋转的手势

1 前言

创建一个 UIRotationGestureRecognizer 的监视器,然后在绑定到你的视图中,用来捕获用户利用手指在屏幕上做旋转的手势 。UIRotationGestureRecognizer 这个类有一个 rotation 的属性,这个属性可以用来设置旋转的方向和旋转的弧 度 。 当 用 户 的 手 势 开 始 和 结 束 的 时 候 分 别 会 用 到 如 下 的 两 个 属 性 .UIGestureRecognizerStateBegan, UIGestureRecognizerStateEnded.

2 代码实例

ZYViewController.m
//rotationGestureRecognizer 是我们的旋转手势的监听捕获实例对象。
@synthesize rotationGestureRecognizer;
@synthesize helloWorldLabel;
@synthesize rotationAngleInRadians;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    /******添加Label组件 start******/
    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];
    /******添加Label组件 end******/
    //UIRotationGestureRecognizer 手势识别器,这个类能用来监听和捕获旋转的手势,添加一个旋转的手势识别器,这样用户就可以顺利的使 用手势来调整图片的位置。
    self.rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotations:)];
    //添加手势
    [self.view addGestureRecognizer:self.rotationGestureRecognizer];
    
}

- (void) handleRotations:(UIRotationGestureRecognizer *)paramSender{
    if (self.helloWorldLabel == nil){ return;
    }
    //利用 CGAffineTransformMakeRotation 这 个方法来进行一个旋转手势的事件传递.将前一个旋转的弧度和当前的旋转弧度相加
    //rotationAngleInRadians 这个对象就是我们在旋转的时候需要为我们的标签对象设置的一个位置对象信息,当我们每一次旋转的时 候我们都会把一个新的位置值保存在这个对象里面,达到一个旋转的效果。
    self.helloWorldLabel.transform = CGAffineTransformMakeRotation(self.rotationAngleInRadians + paramSender.rotation);
    //手势结束时候,调整视图位置
    if (paramSender.state == UIGestureRecognizerStateEnded){
        self.rotationAngleInRadians += paramSender.rotation;
    }
}

运行结果


旋转后(旋转方法,按住Option键,按住鼠标左键,出现两个小圆球,然后拖动旋转)结果


3 结语

以上是所有内容,希望对大家有所帮助。

Demo代码下载:http://download.csdn.net/detail/u010013695/5365289

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值