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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7940952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:16:45  来源:igfitidea点击:

iOS5 Swipe Gesture to switch images - help requested

xcodeuiviewuiimageviewuigesturerecognizer

提问by David DelMonte

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.

我有许多图像想要使用手势(来回)推送。我知道 PageViewController 非常适合于此,但我无法弄清楚如何在使用 NIB 的现有项目中使用它。

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

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

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

非常感谢

采纳答案by David DelMonte

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 publishedon Nov. 14 but the code samplesare available to pre-purchasers..

我最终为 PageViewController 代码切换了我的原始代码。其中的示例来自 Erica Sadun 的新书:“iOS5 Developer's Cookbook”。该书将于11 月 14 日出版,但代码示例可供预购者使用。

回答by timthetoolman

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:

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

Good Luck!

祝你好运!