iOS5 Swipe Gesture可切换图像 - 请求帮助

时间:2011-10-29 18:51:24

标签: xcode uiview uiimageview uigesturerecognizer

我有很多想要用手势推动的图像(来回)。我知道PageViewController是完美的,但我无法弄清楚如何在使用NIB的现有项目中使用它。

因此。我正在尝试构建一个UIView动画。我可以看到第一张图片,但手势不起作用..

我将不胜感激任何帮助..

以下是两个例程之一的代码,以及手势init:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    NSLog(@"%s", __FUNCTION__);
    return YES;
}


- (void)handleSwipe:(UISwipeGestureRecognizer *)recognizer {

     NSLog(@"%s", __FUNCTION__);
     switch (recognizer.direction)
     {
          case (UISwipeGestureRecognizerDirectionRight):
               [self performSelector:@selector(previousPage:)];
               //[self previousPage];
               break;               

          case (UISwipeGestureRecognizerDirectionLeft): 
               [self performSelector:@selector(nextPage:)];
               //[self nextPage];
               break;

          default:
               break;
     }     
}     



- (void)nextPage {
     [UIView beginAnimations:@"fadeOut" context:nil];
     [UIView setAnimationDelay:0.0f];
     [UIView setAnimationDuration:0.5f];
     [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
     pageNumber ++;
     if(pageNumber > maxInfoPages)
     {
          pageNumber = 1;
     }
     NSString *imageName = [NSString stringWithFormat:@"infoPage%i.png", pageNumber];
     imageView.image = [UIImage imageNamed:imageName];     
     NSLog(@"imageName is: %@", imageName);                                    
     NSLog(@"imageView is: %@", imageView);
     imageView.clipsToBounds=YES;     
     self.view = imageView;
     [UIView commitAnimations];
}

非常感谢

2 个答案:

答案 0 :(得分:0)

如果您实际上没有设置UIGestureRecognizers并将它们添加到视图中,则不会将任何事件发送到您的viewController。因此,您需要将UIGestureRecognizers附加到将拦截滑动的视图。例如,在viewDidLoad方法中,您可以执行以下操作:

UISwipeGestureRecognizer *rightSwipe=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
rightSwipe.direction=UISwipeGestureRecognizerDirectionRight;

UISwipeGestureRecognizer *leftSwipe=[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
leftSwipe.direction=UISwipeGestureRecognizerDirectionLeft;

[self.view addGestureRecognizer:leftSwipe];
[self.view addGestureRecognizer:rightSwipe];

// leave out the following if using ARC
[leftSwipe release];
[rightSwipe release];

祝你好运!

答案 1 :(得分:0)

我最终切换出了PageViewController代码的原始代码。其中的样本来自Erica Sadun的新书:“iOS5 Developer's Cookbook”..这本书将于11月14日published,但code samples可供预购商使用..