iOS 使用带有 UILabel 的 UITapGestureRecognizer 滑动到下一页 [英] iOS using UITapGestureRecognizer with UILabel to swipe to the next page
问题描述
抱歉,如果我忽略了对我的问题的潜在答案,但是我在将 UITapGestureRecognizer 与子视图 UIViewController 类中的 UILabel 一起使用时遇到了麻烦...
Sorry if I overlooked a potential answer to my question, but I'm having trouble with using the UITapGestureRecognizer with a UILabel inside a subview UIViewController class...
基本上,我创建了一个自定义 UIViewController,它有一个 UILabel 和一些其他不相关的元素.然而,这个自定义 UIViewController 位于一个启用分页的自定义 UIScrollView 中,并且标签的偏移量刚好足以让您看到下一个 UIViewController 的标签.我想要做的是当用户触摸下一个"标签时,scrollRectToVisible:animated: 方法会触发,本质上是在不滚动的情况下切换页面.CustomViewController 的 UILabel 位于顶部.
Basically, I've created a custom UIViewController that has a UILabel and a few other irrelevant elements. This custom UIViewController, however, is inside a custom UIScrollView with paging enabled and the labels are offset just enough so you can see the next UIViewController's label. What I want to do is when the user touches the "next" label, the scrollRectToVisible:animated: method fires, essentially switching pages without scrolling. The CustomViewController's UILabels are situated on the top.
以下是将 UITapGestureRecognizer 添加到 CustomViewController 的 UILabel 时容器 UIScrollView 的示例代码:
Here's an example code for the container UIScrollView when adding the UITapGestureRecognizer to the CustomViewController's UILabel:
[scrollView addSubview:CustomViewController];
- (void) addSubview: (CustomViewController *) view {
// create view's frame here...
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fireScrollRectToVisible:view)]; // this is the current problem like a lot of people out there...
[view.Label addGestureRecognizer:tap];
// [super addSubview:view.view];
}
- (void) fireScrollRectToVisible: (CustomViewController *) cvc {
CGRect frame = CGRectMake(cvc.view.frame.origin.x, cvc.view.frame.origin.y, 320, 480);
[scrollView scrollRectToVisible: frame animated:YES];
}
我最初认为这很容易,但是由于@selector 不允许您放置参数,因此它变得非常困难.我想我需要的是访问 CustomViewController 的框架并将 scrollRectToVisible 设置为该框架,但我不确定了...
I initially thought this would be easy, but since the @selector doesn't allow you to place arguments it made it incredibly difficult. I think what I need is to access the CustomViewController's frame and set the scrollRectToVisible to that, but I'm not sure anymore...
我已经尝试过这篇文章,但我对 Objective-C 很陌生,我不完全理解 hitTest:withEvent:我假设 hitTest:(CGPoint) 与视图的边界有关?
I've tried this post but I'm very new at Objective-C and I don't fully understand the hitTest:withEvent: I'm assuming the hitTest:(CGPoint) has something to do with the view's bounds?
UITapGestureRecognizer initWithTarget:action:获取参数的方法?
如果有人能指出我正确的方向,那就太好了.任何帮助将不胜感激!
If anyone can point me in the right direction that would be great. Any help would be greatly appreciated!
推荐答案
我根据你的代码做了一些改动
I do some change based on your code
- (void) addSubview: (CustomViewController *) view {
// create view's frame here...
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fireScrollRectToVisible:)]; // this is the current problem like a lot of people out there...
[view.Label addGestureRecognizer:tap];
// [super addSubview:view.view];
}
- (void) fireScrollRectToVisible: (UIGestureRecognizer *) gesture {
UIView *view = [gesture.view superview];
CGRect frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, 320, 480);
[scrollView scrollRectToVisible: frame animated:YES];
}
希望能帮到你.
这篇关于iOS 使用带有 UILabel 的 UITapGestureRecognizer 滑动到下一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!