如何使图像平滑旋转?

时间:2011-11-07 16:07:02

标签: iphone ipad uigesturerecognizer image-rotation

目前我正在使用UIRotationGestureRecognizer来旋转我的图像,而且我的图像目前正在顺畅地旋转。

我正在使用的代码是

   CGFloat imageRotationDegree;

    if ([gesture state] == UIGestureRecognizerStateBegan || [gesture state] == UIGestureRecognizerStateChanged) 
    {
        [gesture view].transform = CGAffineTransformRotate([[gesture view] transform], [gesture rotation]);
        [gesture setRotation:0];
         imageRotationDegree=[gesture rotation];
    }

    NSLog(@"Rotation in Degrees: %f", imageRotationDegree);

所以问题是它总是将旋转度打印为零。所以我无法保存当前的旋转度。

此外,如果我将[gesture setRotation:0];更改为其他程度,则轮换不顺畅。

那么如何以平滑的旋转方式打印不同的旋转度。

2 个答案:

答案 0 :(得分:0)

尝试

CGFloat imageRotationDegree;

if ([gesture state] == UIGestureRecognizerStateBegan || [gesture state] == UIGestureRecognizerStateChanged) 
{
    imageRotationDegree = [gesture rotation];
    [gesture view].transform = CGAffineTransformRotate([[gesture view] transform], imageRotationDegree);
    [gesture setRotation:0];
}

NSLog(@"Rotation in Degrees: %f", imageRotationDegree);

答案 1 :(得分:0)

[Export("RotateImage")]
void RotateImage (UIRotationGestureRecognizer gestureRecognizer)
{
    AdjustAnchorPointForGestureRecognizer (gestureRecognizer);

    if (gestureRecognizer.State == UIGestureRecognizerState.Began || gestureRecognizer.State == UIGestureRecognizerState.Changed)
    {
        gestureRecognizer.View.Transform *= CGAffineTransform.MakeRotation (gestureRecognizer.Rotation);

        // Reset the gesture recognizer's rotation - the next callback will get a delta from the current rotation.
        gestureRecognizer.Rotation = 0;
    }
}

void AdjustAnchorPointForGestureRecognizer (UIGestureRecognizer gestureRecognizer)
{
    if (gestureRecognizer.State == UIGestureRecognizerState.Began)
    {
        var image = gestureRecognizer.View;
        var locationInView = gestureRecognizer.LocationInView (image);
        var locationInSuperview = gestureRecognizer.LocationInView (image.Superview);

        image.Layer.AnchorPoint = new PointF (locationInView.X / image.Bounds.Size.Width, locationInView.Y / image.Bounds.Size.Height);
        image.Center = locationInSuperview;
    }
}

[Export("RotateImage")]
void RotateImage (UIRotationGestureRecognizer gestureRecognizer)
{
    AdjustAnchorPointForGestureRecognizer (gestureRecognizer);

    if (gestureRecognizer.State == UIGestureRecognizerState.Began || gestureRecognizer.State == UIGestureRecognizerState.Changed)
    {
        gestureRecognizer.View.Transform *= CGAffineTransform.MakeRotation (gestureRecognizer.Rotation);

        // Reset the gesture recognizer's rotation - the next callback will get a delta from the current rotation.
        gestureRecognizer.Rotation = 0;
    }
}

// Zoom the image by the current scale

[Export("ScaleImage")]
void ScaleImage (UIPinchGestureRecognizer gestureRecognizer)
{
    AdjustAnchorPointForGestureRecognizer (gestureRecognizer);

    if (gestureRecognizer.State == UIGestureRecognizerState.Began || gestureRecognizer.State == UIGestureRecognizerState.Changed)
    {
        gestureRecognizer.View.Transform *= CGAffineTransform.MakeScale (gestureRecognizer.Scale, gestureRecognizer.Scale);

        // Reset the gesture recognizer's scale - the next callback will get a delta from the current scale.
        gestureRecognizer.Scale = 1;
    }
}

// Shift the image's center by the pan amount

[Export("PanImage")]
void PanImage (UIPanGestureRecognizer gestureRecognizer)
{           
    gestureRecognizer.Enabled = true;
    AdjustAnchorPointForGestureRecognizer (gestureRecognizer);
    var image = gestureRecognizer.View;

    if (gestureRecognizer.State == UIGestureRecognizerState.Began || gestureRecognizer.State == UIGestureRecognizerState.Changed)
    {           
        var translation = gestureRecognizer.TranslationInView (this.window);

        gestureRecognizer.View.Center = new PointF (gestureRecognizer.View.Center.X + translation.X, gestureRecognizer.View.Center.Y + translation.Y);

        //image.Center = new PointF (image.Center.X + translation.X, image.Center.Y + translation.Y);

        // Reset the gesture recognizer's translation to {0, 0} - the next callback will get a delta from the current position.
        gestureRecognizer.SetTranslation (PointF.Empty, image);
    }
}