【问题标题】:Swipe Gesture View Controller Xcode 5.1.1滑动手势视图控制器 Xcode 5.1.1
【发布时间】:2014-06-30 22:58:21
【问题描述】:

我是 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 方法的手势。

【问题讨论】:

  • 你想用两根手指滑动吗?还是只用一根手指?

标签: ios objective-c xcode uigesturerecognizer viewcontroller


【解决方案1】:
- (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 方法中的上述代码

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-23
      • 1970-01-01
      • 2015-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-15
      相关资源
      最近更新 更多