美文网首页
UIRotationGestureRecognizer旋转手势

UIRotationGestureRecognizer旋转手势

作者: 浅_若清风 | 来源:发表于2018-06-01 14:01 被阅读0次

    概述:UIRotationGestureRecognizer旋转手势,继承于UIGestureRecognizer类,相对于UIGestureRecognizer多了两个属性值。

     /*获取旋转的弧度*/
    @property (nonatomic)          CGFloat rotation; 
     /*获取旋转的速度,单位为弧度/秒*/
    @property (nonatomic,readonly) CGFloat velocity;  
    

    案例:图像的旋转

    /*声明*/
    @property CGFloat rotateScale;
    @property CGFloat rotateRotation;
    @property UIImage* rotateImage;
    
        /*设置初始的缩放比例*/ 
        rotateScale = 1.0;
        rotateRotation = 0;
        /*设置imageView允许用户交互,支持多点触碰*/ 
        self.image_view.userInteractionEnabled = YES;
        self.image_view.multipleTouchEnabled = YES;
        rotateImage = [UIImage imageNamed:@"rotateImage.png"];
        self.image_view.image = rotateImage;
       UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotaGestureDetected:)];
        [rotationRecognizer setDelegate:self];
       /*加载到要旋转的图片*/
        [self.image_view addGestureRecognizer:rotationRecognizer];
    
    -(void) rotaGestureDetected:(UIRotationGestureRecognizer *)recognizer {
       /*获取手势旋转的弧度*/ 
        CGFloat rotation = recognizer.rotation;
        /*根据当前缩放比计算图片缩放后的目标大小*/ 
        CGSize targetSize = CGSizeMake(rotateImage.size.width * rotateScale,
                                       rotateImage.size.height * rotateScale);
        /*对图片进行缩放、旋转*/ 
        self.image_view.image = [[rotateImage imageByScalingToSize:targetSize]
                                imageRotatedByRadians: rotateRotation + rotation];
        /*如果旋转手势结束*/ 
        if(recognizer.state == UIGestureRecognizerStateEnded)
        {
            rotateRotation = rotateRotation + rotation;
        }
    }
    
    

    注:了解UIGestureRecognizer类请跳转https://www.jianshu.com/p/e206dc86f89a

    相关文章

      网友评论

          本文标题:UIRotationGestureRecognizer旋转手势

          本文链接:https://www.haomeiwen.com/subject/vweajftx.html