【问题标题】:Smooth rotation of UIView with touch使用触摸平滑旋转 UIView
【发布时间】:2011-03-18 12:54:50
【问题描述】:

我想知道如何在我的代码中平滑UITouch。我能够在我的UIView 上检测到UItouch,但是当我尝试使用CGAffineTransform 旋转视图时,它不会平滑旋转。我必须在 iPhone 上按下或长指触摸才能进行这种旋转。 如何执行平滑旋转,例如 Roambi Visualizer 应用程序。 感谢您的帮助。

【问题讨论】:

  • UIVIew Animation 可以根据需要使旋转非常平滑,使用动画的持续时间

标签: iphone objective-c iphone-sdk-3.0 uitouch cgaffinetransform


【解决方案1】:

transform是UIView的动画属性,所以你可以使用Core Animation来平滑旋转:

CGAffineTransform newTransform = //...construct your desired transform here...
[UIView animateWithDuration:0.2
                 animations:^{view.transform = newTransform;}];

【讨论】:

    【解决方案2】:

    大家好,我在 touchesMoved 中找到了以下问题的解决方案及其对我的工作.....

    这里是代码......

    UITouch *touch = [touches anyObject];
    CGPoint currentLocation = [touch locationInView:self.superview];
    CGPoint pastLocation = [touch previousLocationInView:self.superview];
    currentLocation.x = currentLocation.x - self.center.x;
    currentLocation.y = self.center.y - currentLocation.y;
    pastLocation.x = pastLocation.x - self.center.x;
    pastLocation.y = self.center.y - currentLocation.y;
    CGFloat angle = atan2(pastLocation.y, pastLocation.x) - atan2(currentLocation.y, currentLocation.x); 
    CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
    
    // Apply the affine transform
    
    [[self.superview viewWithTag:ROTATE_VIEW_TAG] setTransform:transform] ;
    

    【讨论】:

    • 如果你想逐渐旋转它,你应该使用[view setTransform:CGAffineTransformRotate(view.transform, angle)]。否则,这很好用!
    【解决方案3】:

    这可能会或可能不会长久,但 cam 的答案存在一些小问题。

     pastLocation.y = self.center.y - currentLocation.y;
    

    需要

     pastLocation.y = self.center.y - pastLocation.y;
    

    如果您想快速执行此操作,我使用 Cam 的回答来确定以下内容:

    override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
        var touch: UITouch = touches.first as! UITouch
    
        var currentTouch = touch.locationInView(self.view)
        var previousTouch = touch.previousLocationInView(self.view)
    
        currentTouch.x = currentTouch.x - self.view.center.x
        currentTouch.y = self.view.center.y - currentTouch.y
    
        previousTouch.x = previousTouch.x - self.view.center.x
        previousTouch.y = self.view.center.y - previousTouch.y
    
        var angle = atan2(previousTouch.y, previousTouch.x) - atan2(currentTouch.y, currentTouch.x)
    
        UIView.animateWithDuration(0.1, animations: { () -> Void in
            bigCircleView?.transform = CGAffineTransformRotate(bigCircleView!.transform, angle)
        })
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-01
      • 1970-01-01
      • 2016-02-12
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      相关资源
      最近更新 更多