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