【发布时间】:2014-06-30 22:58:21
【问题描述】:
我是 Objective-C 和 Xcode 的新手,我很难更改视图。我知道您可以使用UIScrollView 在图像视图之间滑动,但我想简单地向右滑动以更改视图,然后向左滑动以返回。我有 AppDelegate.h 和 AppDelegate.m 没有更改。 ViewController.h 和 ViewController.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.h 和ViewController2.m,我已将它们链接到情节提要中的第二个视图以及我已链接到上述handleSwipeGesture 方法的手势。
【问题讨论】:
-
你想用两根手指滑动吗?还是只用一根手指?
标签: ios objective-c xcode uigesturerecognizer viewcontroller