2013-04-27 124 views
0

我有2个imageview,我希望它们在点击时旋转。这是我的代码:UITapGestureRecognizer检测单点触摸

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

然后:

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

应该tapRecognize ==去后怎么办?

回答

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

看一看下面的链接: rotating Image

+0

一点也没有”吨的工作,我已经尝试用NSLog它应该旋转它,但它没有记录任何东西? – user2325183 2013-04-27 15:54:28

+1

请分享您在哪里登录的代码,以及在您的图像上添加轻击手势的位置。 – Shad 2013-04-27 15:59:56

0
- (void)handleTap:(UITapGestureRecognizer *)tapRecognize 
    { 
     if (tapRecognize == tapRecognizer) 
     { 
      CGAffineTransform transform = CGAffineTransformRotate(lineImage.transform, 90); 
      [lineImage setTransform:transform]; 
     } 
    } 

,并确保已启用imageviews的UESR互动,

[imageView1 setUserInteractionEnabled:YES]; 
    [imageView2 setUserInteractionEnabled:YES]; 
0

如果你确实需要一个翻盖旋转你最好选择下面的滑动手势。

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

在处理方法你给单独翻转权的动画和左

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];} 

下面的代码是只是一个简单的点触手势。你会得到呼叫handleSingleTap:

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

[singleTap setNumberOfTapsRequired:1]; 

[imageView addGestureRecognizer:singleTap]; 

[singleTap release]; 

手势处理机

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

如果你需要真正的旋转,你可以使用

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

像你手势处理机