如何使用滑动手势 XCode 交换视图

发布于 2024-11-01 07:46:57 字数 1883 浏览 3 评论 0原文

我正在使用 XCode 为 iOS 平台开发 Cocoa 触摸应用程序,但无法找到如何实现滑动手势,以允许用户向左或向右滑动手指以更改为新的 ViewController(nib/xib 文件)。我已经使用按钮和模式转换完成了 swapView IBAction ,并且我已阅读有关 Apple 的 TouchGestureRecognizer 的内容,但我不知道如何实现允许视图的滑动操作改变。

我不想使用滚动视图,因为我有几十个视图控制器,我希望用户能够滑动。

这是一个示例:

First View Controller.xib: SwipeRight- 转到第二个 View Controller.xib

第二个 View Controller.xib:
SwipeLeft- 转到第一个视图 Controller.xib
SwipeRight- 转到第三个 View Controller.xib

Touch 手势,但我使用过 IBAction 方法通过模态转换按钮来切换视图(见下文):

-(IBAction)swapViews; { 
    SecondViewController *second2 =[[SecondViewController alloc initWithNibName:@"SecondViewController" bundle:nil];
    second2.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:second2 animated:YES];
    [second2 release];
}

我之前没有使用过 UISwipe / 使用滑动来执行类似的方法但格式不同?如果是这样,我该如何整理并格式化它。

谢谢

编辑 - 根据问题的评论回答

将其放入您的 viewDidLoad

UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftDetected:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];

然后添加一个选择器,将以下代码粘贴到您的主...

- (IBAction)swipeLeftDetected:(UIGestureRecognizer *)sender {
    NC2ViewController *second2 =[[NC2ViewController alloc] initWithNibName:@"NC2ViewController" bundle:nil];
    second2.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:second2 animated:YES];
    [second2 release];
}

然后只需确保导入您要交换到的 otherViewController 使用

#import "SecondViewController"

在主文件的顶部 。希望这有帮助。

结束编辑

I am using XCode to develop a Cocoa touch application for the iOS platform but have had trouble finding out how to get a swipe gesture implemented that would allow the user to swipe their finger left or right to change to a new ViewController (nib/xib file). I have done a swapView IBAction using a button and modal transitioning and I have read about Apple's TouchGestureRecognizer but I don't know how to implement a swipe action that would allow a view change.

I do NOT want to use a scroll view, as I have several dozen view controllers, that I want the user to be able to swipe through.

Here is an example:

First View Controller.xib:
SwipeRight- Go to second View Controller.xib

Second View Controller.xib:
SwipeLeft- Go to first View Controller.xib
SwipeRight- Go to third View Controller.xib

etc, etc

I have not used UISwipe/Touch Gestures before but I have used an IBAction method to switch views using a button with Modal Transitioning (see below):

-(IBAction)swapViews; { 
    SecondViewController *second2 =[[SecondViewController alloc initWithNibName:@"SecondViewController" bundle:nil];
    second2.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:second2 animated:YES];
    [second2 release];
}

Is using a swipe to do a similar method formatted differently? If so, how do I sort this out and format it.

Thank You

Edit - Answer as Per Comment on Question

Place this in your viewDidLoad

UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeftDetected:)];
swipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeRecognizer];
[swipeRecognizer release];

Then add a selector as by pasting the following code into your main...

- (IBAction)swipeLeftDetected:(UIGestureRecognizer *)sender {
    NC2ViewController *second2 =[[NC2ViewController alloc] initWithNibName:@"NC2ViewController" bundle:nil];
    second2.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:second2 animated:YES];
    [second2 release];
}

Then just make sure you import the otherViewController you are swapping to using

#import "SecondViewController"

at the top of your main file. Hope this helps.

End Edit

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

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

发布评论

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

评论(2

小忆控 2024-11-08 07:46:57

这听起来是使用 UIGestureRecognizer< 的最佳时机/a> 或者,更具体地说, UISwipeGestureRecognizer

有关如何使用它们的更多信息,请阅读 手势识别器 部分。

This sounds like a perfect time to use UIGestureRecognizer or, more specifically, UISwipeGestureRecognizer.

For more info on how to use them, read up in the Gesture Recognizers section of the Event Handling Guide.

从此见与不见 2024-11-08 07:46:57

假设您想向左滑动以从右侧调出另一个视图。

在故事板中,拖放滑动手势识别器。它将在视图控制器下方创建一个图标;将此图标拖放到您想要导航到的 ViewController 上。这将添加一个segue,选择自定义segue。然后创建一个 UIStoryboardSegue 类。添加以下代码:

- (void)perform {
    UIViewController* source = (UIViewController *)self.sourceViewController;
    UIViewController* destination = (UIViewController *)self.destinationViewController;

    CGRect sourceFrame = source.view.frame;
    sourceFrame.origin.x = -sourceFrame.size.width;

    CGRect destFrame = destination.view.frame;
    destFrame.origin.x = destination.view.frame.size.width;
    destination.view.frame = destFrame;

    destFrame.origin.x = 0;

    [source.view.superview addSubview:destination.view];

    [UIView animateWithDuration:0.5
                     animations:^{
                         source.view.frame = sourceFrame;
                         destination.view.frame = destFrame;
                     }
                     completion:^(BOOL finished) {
                         UIWindow *window = source.view.window;
                         [window setRootViewController:destination];
                     }];
}

Lets assume you want to swipe left to bring up another view from the right.

In the storyboard, drag and drop a swipe gesture recognizer. It will make an icon below the view controller; drag this icon and drop onto the ViewController you want to navigate to. This will add a segue, select custom segue. Then create a UIStoryboardSegue class. Add the following code:

- (void)perform {
    UIViewController* source = (UIViewController *)self.sourceViewController;
    UIViewController* destination = (UIViewController *)self.destinationViewController;

    CGRect sourceFrame = source.view.frame;
    sourceFrame.origin.x = -sourceFrame.size.width;

    CGRect destFrame = destination.view.frame;
    destFrame.origin.x = destination.view.frame.size.width;
    destination.view.frame = destFrame;

    destFrame.origin.x = 0;

    [source.view.superview addSubview:destination.view];

    [UIView animateWithDuration:0.5
                     animations:^{
                         source.view.frame = sourceFrame;
                         destination.view.frame = destFrame;
                     }
                     completion:^(BOOL finished) {
                         UIWindow *window = source.view.window;
                         [window setRootViewController:destination];
                     }];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文