ios - 滑动手势 View Controller Xcode 5.1.1

标签 ios objective-c xcode uigesturerecognizer viewcontroller

我是 Objective-C 和 Xcode 的新手,并且在更改 View 时遇到困难。我知道您可以使用 UIScrollView 在 ImageView 之间滑动,但我想简单地向右滑动以更改 View ,然后向左滑动以返回。我有 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,我已将它们链接到 Storyboard 中的第二个 View ,以及链接到 的手势上面的handleSwipeGesture方法。

最佳答案

- (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方法中

关于ios - 滑动手势 View Controller Xcode 5.1.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24500159/

相关文章:

objective-c - 从 PID 获取名称?

ios - 替代 iOS 10.0 核心数据中的 NSPersistentStoreUbiquitousContentNameKey 键

c# - C# 是否具有与 Objective-c 的类别等效的功能?

ios - 使用 Core Plot 库在 IOS 中绘制 ECG。

ios - 运行 pod update 会更改 Pod build设置

ios - 如何使用 CKModifyRecordsOperation.perRecordProgressBlock 更新进度

ios - ngf-select 不适用于 iphone5

ios - 将数据传递给 Call Directory Extension

ios - 为 Xcode 安装 OpenSSL 库

iphone - 在 UImage 中显示渲染的图标