【发布时间】:2011-09-13 16:26:09
【问题描述】:
如何使用滑动手势垂直切换视图?
【问题讨论】:
-
你知道如何用 android 做同样的事情吗?
-
这可能就是你要找的东西:stackoverflow.com/a/17540632/4757258
如何使用滑动手势垂直切换视图?
【问题讨论】:
我找到了答案。我发布代码供您参考。谢谢:-)
在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];
}
如果您需要更多说明,请在此处发布。
【讨论】:
实现这个(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
}
【讨论】:
当然!只需将您的 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 选择、跳转导航控制器、以模态方式呈现不同的视图,或者只是在过渡中做一个简单的幻灯片。
【讨论】:
UIViewAnimationTransition 或制作更精美的自定义动画。谷歌搜索一些很好的例子。
在 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) {
}
【讨论】:
供参考 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"];
【讨论】: