-1

Currently i am using UIRotationGestureRecognizer to rotate my image and my image currently rotating smoothly.

The code which i am using is

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

So the problem is it is always printing rotation degree as zero.So that i am unable to save the current rotation degree.

Also if i change [gesture setRotation:0]; to some other degree then rotation is not smooth.

So How can i print different rotation degree with smooth rotation.

user930195
  • 432
  • 1
  • 5
  • 19

2 Answers2

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);
    }
}
Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
0

try with

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);
Mathieu Hausherr
  • 3,485
  • 23
  • 30
  • Ya its working fine but when i stop rotating the image its again showing 0.000000 . So is that means my imageRotationDegree contain 0.000000 . – user930195 Nov 07 '11 at 16:16
  • `[gesture setRotation:0];` sets the rotation to 0, so what are you expecting? – vikingosegundo Nov 07 '11 at 16:24
  • I am expecting whenever i stop rotating it should hold the last valid value. – user930195 Nov 07 '11 at 16:26
  • Let say Rotation in Degrees: 0.004770 Rotation in Degrees: 0.008461 Rotation in Degrees: 0.003762 Rotation in Degrees: 0.003798 Rotation in Degrees: 0.004724 Rotation in Degrees: 0.000000 Rotation in Degrees: 0.000000 i need to save 0.04724 here – user930195 Nov 07 '11 at 16:27
  • So don't overwrite it in very step or write it to an ivar. – vikingosegundo Nov 07 '11 at 16:27
  • an ivar is, what is called member in other languages. If you don't know, what I mean, read this from the beginning to the end, as this is the foundation of oop with obj-c: http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/OOP_ObjC/Introduction/Introduction.html – vikingosegundo Nov 07 '11 at 16:36
  • 1
    and this might be helpful as well: http://developer.apple.com/library/ios/#samplecode/Touches/Introduction/Intro.html . Make sure, you look at the GestureRecognizer version. – vikingosegundo Nov 07 '11 at 16:39