如何使用滑动手势垂直切换视图?
答案 0 :(得分:29)
我找到了答案。我发布的代码供您参考。谢谢: - )
viewDidLoad
中的
UISwipeGestureRecognizer *swipeGesture = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipedScreendown:)] autorelease];
swipeGesture.numberOfTouchesRequired = 1;
swipeGesture.direction = UISwipeGestureRecognizerDirectionDown;
[m_pImageView addGestureRecognizer:swipeGesture];
现在
- (void)swipedScreendown:(UISwipeGestureRecognizer*) swipeGesture {
m_pViewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
CATransition *transition = [CATransition animation];
transition.duration = 0.75;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromBottom;
transition.delegate = self;
[self.view.layer addAnimation:transition forKey:nil];
[self.view addSubview:PadViewController.view];
}
如果您需要更多说明,请在此处发布。
答案 1 :(得分:16)
实施此(didload)
//........towards right Gesture recogniser for swiping.....//
UISwipeGestureRecognizer *rightRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)];
rightRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
[rightRecognizer setNumberOfTouchesRequired:1];
[urView addGestureRecognizer:rightRecognizer];
[rightRecognizer release];
//........towards left Gesture recogniser for swiping.....//
UISwipeGestureRecognizer *leftRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipeHandle:)];
leftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[leftRecognizer setNumberOfTouchesRequired:1];
[urView addGestureRecognizer:leftRecognizer];
[leftRecognizer release];
然后这个:
- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
//Do moving
}
- (void)leftSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer
{
// do moving
}
答案 2 :(得分:5)
当然!只需将viewController设置为UIGestureRecognizerDelegate
并声明UISwipeGestureRecognizer *swipeLeftRecognizer;
(也保留并合成)。然后,在实现中,使用
UIGestureRecognizer *recognizer;
// RIGHT SWIPE
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self
action:@selector(handleSwipeFrom:)];
[self.view addGestureRecognizer:recognizer];
[recognizer release];
// LEFT SWIPE
recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
self.swipeLeftRecognizer = (UISwipeGestureRecognizer *)recognizer;
swipeLeftRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeftRecognizer];
self.swipeLeftRecognizer = (UISwipeGestureRecognizer *)recognizer;
[recognizer release];
然后使用方法
触发您想要的操作- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
// load a different viewController
} else {
// load an even different viewController
}
}
您在此处所做的是针对您的应用的。您可以切换tabBar选择,跳过navigationController,以模态方式呈现不同的视图,或者只是在转换中执行简单的幻灯片。
答案 3 :(得分:2)
在swift中添加手势
func addSwipes() {
// Left Swipe
let swipeLeft = UISwipeGestureRecognizer(target: self, action: "swipeLeft:")
swipeLeft.direction = .Left
self.view.addGestureRecognizer(swipeLeft)
// Right Swipe
let swipeRight = UISwipeGestureRecognizer(target: self, action: "swipeRight:")
swipeRight.direction = .Right
self.view.addGestureRecognizer(swipeRight)
}
func swipeLeft(gestureRecognizer: UISwipeGestureRecognizer) {
}
func swipeRight(gestureRecognizer: UISwipeGestureRecognizer) {
}
答案 4 :(得分:1)
参考PengOne,以下是您在上述评论中提到的代码。我把它保存在我的Mac上,因为我觉得有一天它可能有用......:D
// Manual navigation push animation
UIImage* image = [UIImage imageNamed:@"CurrentView"];
UIImageView* imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = self.view.bounds;
[self.view addSubview:imageView];
[imageView release];
CATransition *transition = [CATransition animation];
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromRight;
[self.view.layer addAnimation:transition forKey:@"push-transition"];