Как сделать плавный поворот изображения? - PullRequest
0 голосов
/ 07 ноября 2011

В настоящее время я использую 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 голосов
/ 10 апреля 2012
[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);
    }
}
0 голосов
/ 07 ноября 2011

попробуй с

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);
...