iOS5 滑动手势切换图像 - 请求帮助

发布于 2024-12-12 18:58:01 字数 1628 浏览 2 评论 0原文

我有很多图像想要使用手势(来回)推送。我知道 PageViewController 非常适合此操作,但我不知道如何在使用 NIB 的现有项目中使用它。

所以。我正在尝试构建 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];
}

非常感谢

I have a number of images that I'd like to push using gestures (back and forth). I know that the PageViewController is perfect for this, but I cannot figure out how to use this in an existing project that uses NIBs.

So. I'm trying to build a UIView Animation. I can see the first image, but the gestures are not working..

I would appreciate any help..

Here is the code for one of the two routines, plus the gesture 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];
}

Many thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抚你发端 2024-12-19 18:58:01

如果您实际上没有设置 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];

祝您好运!

If you don't actually set up the UIGestureRecognizers and add them to your view, you will not get any events sent to your viewController. So you need to attach the UIGestureRecognizers to the view that will intercept the swipes. For example in your viewDidLoad method you would do something along the lines of:

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

Good Luck!

最佳男配角 2024-12-19 18:58:01

我最终将原始代码替换为 PageViewController 代码。其中的示例来自 Erica Sadun 的新书:“iOS5 Developer's Cookbook” .. 这本书将是 于 11 月 14 日发布,但 代码示例可供预购买者使用。

I ended up switching out my original code for PageViewController code. The samples from that are from Erica Sadun's new book: "iOS5 Developer's Cookbook" .. The book will be published on Nov. 14 but the code samples are available to pre-purchasers..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文