0

I have 2 imageview, and I want them to rotate when they are clicked. This is my code:

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognizerMethod:)];

Then in the tapRecognizerMethod:

- (void)tapRecognizerMethod:(UITapGestureRecognizer *)tapRecognize
{
    if (tapRecognize == tapRecognizer) // tapRecognizer is the first imageviews recognizer
    {
        if (tapRecognize.state == what should go here? 
        {
            //do something
        }
    }
}

What should go after tapRecognize ==?

jszumski
  • 7,430
  • 11
  • 40
  • 53

3 Answers3

0
- (void)tapRecognizerMethod:(UITapGestureRecognizer *)tapRecognize{
  if (tapRecognize == tapRecognizer){
     if (tapRecognize.state == UIGestureRecognizerStateEnded){
            UIImageView *imgView = (UIImageView*)[tapRecognize view];
            //Now need to rotate the image
     }
  }
}

Have a look at the following link: rotating Image

Community
  • 1
  • 1
Shad
  • 1,587
  • 2
  • 20
  • 29
  • It doesn't work, I have tried with a NSLog where it should be rotated it but it doesnt log anything? – user2325183 Apr 27 '13 at 15:54
  • 1
    Please share your code of where you are logging and also where u add the tap-gesture on u r image. – Shad Apr 27 '13 at 15:59
0
- (void)handleTap:(UITapGestureRecognizer *)tapRecognize
    {
        if (tapRecognize == tapRecognizer)
        {
            CGAffineTransform transform = CGAffineTransformRotate(lineImage.transform, 90);
            [lineImage setTransform:transform];
        }
    }

and make sure you have enabled the uesr interaction of imageviews,

  [imageView1 setUserInteractionEnabled:YES];
  [imageView2 setUserInteractionEnabled:YES];
Thilina Chamath Hewagama
  • 9,039
  • 3
  • 32
  • 45
0

If you actually requires a flip rotation you better chose below swipe gestures.

UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc]
                                           initWithTarget:self
                                           action:@selector(handleSwipeLeft:)];
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[imageView addGestureRecognizer:swipeLeft];
[swipeLeft release];

UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc]
                                            initWithTarget:self
                                            action:@selector(handleSwipeRight:)];
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[imageView addGestureRecognizer:swipeRight];
[swipeRight release];

On handler method you give an animation for separate flip right and left

if(isActiveFlipLeft)
{
[UIView transitionWithView:imageSnap
                  duration:1.0f
                   options:UIViewAnimationOptionTransitionFlipFromLeft
                animations:^{
                    imageSnap.backgroundColor = [UIColor colorWithRed:187.0/225.0 green:187.0/225.0 blue:187.0/225.0 alpha:1.0];
                    imageSnap.image = image;                        
} completion:NULL];}
else if (isActiveFlipRight)
{
[UIView transitionWithView:imageSnap
                          duration:1.0f
                           options:UIViewAnimationOptionTransitionFlipFromRight
                        animations:^{
                            imageSnap.backgroundColor = [UIColor colorWithRed:187.0/225.0 green:187.0/225.0 blue:187.0/225.0 alpha:1.0];
                            imageSnap.image = image;                                
} completion:NULL];}

The below code is for just a simple tap gesture. you will get call on handleSingleTap:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];

[singleTap setNumberOfTapsRequired:1];

[imageView addGestureRecognizer:singleTap];

[singleTap release];

Gesture handler

- (void)handleSwipeLeft:(UIGestureRecognizer *)gestureRecognizer;
- (void)handleSwipeRight:(UIGestureRecognizer *)gestureRecognizer;
- (void)handleSingleTap:(UIGestureRecognizer *)gestureRecognizer;

If you requires real rotation you can use

imageview.transform = CGAffineTransformMakeRotation( 270.0/180*M_PI );

something like in your gesture handler

imalvare
  • 295
  • 1
  • 13