【问题标题】:rotate an image with touch触摸旋转图像
【发布时间】:2011-03-03 07:54:04
【问题描述】:

我制作了一个应用程序,它设置了带时钟的睡眠定时器。基本上它是一个时钟,它有一只手,用户可以移动它来设置他的睡眠时间。我试图用 uitouch 但它从中间旋转,但我希望它从尖端旋转。其次,我希望图像仅在用户触摸图像尖端时旋转,但在我的项目中,图像在使用时也会旋转触摸屏幕的任何部分。我也想在两个方向上旋转图像,但在我的项目中,由于这种方法,它只能顺时针移动

image.transform = CGAffineTransformRotate(image.transform, degreesToRadians(1));

谁能给我一些提示或解决方法?

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    //

        touch = [[event allTouches] anyObject];
        touchLocation = [touch locationInView:touch.view];

        NSLog(@"began");

    }


    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        // get touch event

        [image.layer setAnchorPoint:CGPointMake(0.0,0.0)];



        if ([touch view] == image) {
            image.transform = CGAffineTransformRotate(image.transform, degreesToRadians(1));

        //image.center = touchLocation;
    }

      }

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
        NSLog(@"end");



    }

【问题讨论】:

    标签: ios cocoa-touch uitouch


    【解决方案1】:

    具体来说,你可以自定义一个rotateView,那么:

    1:在“touchesBegan”的委托方法中,获取手指的initialPoint和initialAngle。

    2:在“touchesMoved”期间,获取新的手指点:

    CGPoint newPoint = [[touches anyObject] locationInView:self];
    [self pushTouchPoint:thePoint date:[NSDate date]];
    double angleDif = [self angleForPoint:newPoint] - [self angleForPoint:initialPoint];
    self.angle = initialAngle + angleDif;
    [[imageView layer] setTransform:CATransform3DMakeRotation(angle, 0, 0, 1)];
    

    3:最后,在“touchesEnded”中可以计算最终的AngularVelocity。

    如果有什么不清楚的地方,可以回信。

    【讨论】:

      【解决方案2】:

      以其他方式旋转它:

      image.transform = CGAffineTransformRotate(image.transform, degreesToRadians(1));
      

      要从提示中旋转,您应该使用 setAnchorPoint(就像您在代码中使用的那样,但我认为您应该这样做:[image setAnchorPoint])。

      关于这个主题的更多信息:setAnchorPoint for UIImage?

      【讨论】:

      • 你能解释一下这样做的整个过程吗?单看代码我不是很明白,你如何设置一切来旋转视图?
      【解决方案3】:

      我尝试使用 uitouch 旋转图像,但它从中间旋转,但我希望它从尖端旋转

      我不知道 SDK 中有什么方法可以在其末端旋转元素。如果您想获得这种效果,请获取长度为两倍且一半透明的时钟句柄图像。这不是直接的解决方案,而是一种解决方法。

      我也想在两个方向上旋转图像,但在我的项目中,由于这种方法,它只能顺时针移动

      根据 iOS 开发者文档,正角度值指定逆时针旋转,负值指定顺时针旋转。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-08
        • 2012-07-09
        • 1970-01-01
        • 1970-01-01
        • 2012-07-15
        • 2023-04-07
        相关资源
        最近更新 更多