2011-10-29 32 views
0

我有许多想用手势(来回)推动的图像。我知道PageViewController对此非常完美,但我无法弄清楚如何在使用NIB的现有项目中使用它。iOS5轻扫手势切换图像 - 请求帮助

所以。我试图构建一个UIView动画。我可以看到第一个形象,但姿势不工作..

我希望得到任何帮助..

下面是两个程序中的一个代码,再加上手势初始化:

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

非常感谢

+0

您是否已将手势识别器附加到视图中有问题吗? – timthetoolman

+0

谢谢。是的,我更新了上面的代码。 –

回答

0

我结束了切换出我的PageViewController代码的原始代码。这些样本来自Erica Sadun的新书:“iOS5开发人员指南”。该书将于11月14日发布,发行日期为published,但code samples已提供给预购者。

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

祝您好运!

+0

蒂姆,谢谢,但他的手势在那里。事实上,我的手势工作,但网页是空白的。我已经获得了显示图像的页面。 (至少在日志文件中),但现在手势已停止。我将检查手势是否与此视图相关联。他们可能在应用程序代表... –

+0

timthetoolman - 这不适用于iOS 6.1与SB – codejunkie