滑动手势视图控制器Xcode 5.1.1

时间:2014-06-30 22:58:21

标签: ios objective-c xcode uigesturerecognizer viewcontroller

我是Objective-C和Xcode的新手,我很难改变观点。我知道您可以使用UIScrollView在图片视图之间滑动,但我只想向右滑动以更改视图,然后向左滑动即可返回。我的AppDelegate.hAppDelegate.m尚未更改。 ViewController.hViewController.m如下:

ViewController.h:

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end

ViewController.m:

#import "ViewController.h"
#import "ViewController2.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //sets up gesture recognizer
    UISwipeGestureRecognizer *swipeRightGesture=[[UISwipeGestureRecognizer alloc]               initWithTarget:self action:@selector(handleSwipeGesture:)];
    [self.view addGestureRecognizer:swipeRightGesture];
    swipeRightGesture.direction=UISwipeGestureRecognizerDirectionRight;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)handleSwipeGesture:(UISwipeGestureRecognizer *)sender 
{
    NSUInteger touches = sender.numberOfTouches;
    if (touches == 2)
    {
        if (sender.state == UIGestureRecognizerStateEnded)
        {
            ViewController2 *vc2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
            [self.navigationController pushViewController:vc2 animated:YES];
        }
    }
}

@end

我还有ViewController2.hViewController2.m,我已将其链接到故事板中的第二个视图以及我已链接到上述handleSwipeGesture方法的手势。

2 个答案:

答案 0 :(得分:2)

- (void)viewDidLoad
{
    [super viewDidLoad];

    UISwipeGestureRecognizer *oneFingerSwipeLeft = [[UISwipeGestureRecognizer alloc]
                                                    initWithTarget:self
                                                    action:@selector(oneFingerSwipeLeft:)] ;
    [oneFingerSwipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    [[self view] addGestureRecognizer:oneFingerSwipeLeft];
}

- (void)oneFingerSwipeLeft:(UITapGestureRecognizer *)recognizer
{
    NSLog(@"Swiped left");
  // Add your navigation Code Here
}

如果你想用单指滑动,你可以使用它。如果你想添加两个手指轻扫添加你的

NSUInteger touches = sender.numberOfTouches;
    if (touches == 2)
    {
        if (sender.state == UIGestureRecognizerStateEnded)
        {
        }
    }

(void)onefingerSwipeLeft方法

中的代码

答案 1 :(得分:1)

- (IBAction)handleSwipeGesture:(UISwipeGestureRecognizer *)sender不需要是IBAction,因为它没有连接到IBOutlet。它应该返回void

此外,一旦手势已被识别,您可以进行两次触摸,而不是检查手势的数量,而不是通过使用以下行来识别它:

swipeRightGesture.numberOfTouchesRequired = 2;

因此,方法-(void)handleSwipeGesture不再需要参数,因此您可以从声明手势识别器的行中删除冒号。

最终代码可能与此类似:

UISwipeGestureRecognizer *swipeRightGesture=[[UISwipeGestureRecognizer alloc]               initWithTarget:self action:@selector(handleSwipeGesture)];
swipeRightGesture.direction=UISwipeGestureRecognizerDirectionRight;
swipeRightGesture.numberOfTouchesRequired = 2;
[self.view addGestureRecognizer:swipeRightGesture];

...
...

- (void)handleSwipeGesture {
    ViewController2 *vc2 = [[ViewController2 alloc] initWithNibName:@"ViewController2" bundle:nil];
    [self.navigationController pushViewController:vc2 animated:YES];
}