iOS 使用 UITapGestureRecognizer 和 UILabel 滑动到下一页



抱歉,如果我忽略了我的问题的潜在答案,但是我在子视图UIViewController类中使用UITapGestureRecognizer和UILabel时遇到了问题...

基本上,我创建了一个自定义的UIViewController,它有一个UILabel和其他一些不相关的元素。但是,此自定义UIViewController位于启用了分页的自定义UIScrollView中,并且标签的偏移量刚好,因此您可以看到下一个UIViewController的标签。我想做的是当用户触摸"下一个"标签时,scrollRectToVisible:animated:方法触发,基本上在不滚动的情况下切换页面。CustomViewController 的 UILabels 位于顶部。

下面是将 UITapGestureRecognizer 添加到 CustomViewController 的 UILabel 时容器 UIScrollView 的示例代码:

[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不允许你提出论点,所以它变得非常困难。我认为我需要的是访问自定义视图控制器的框架并将滚动矩形设置为可见,但我不再确定......

我已经尝试过这篇文章,但我对Objective-C很陌生,我不完全理解hitTest:withEvent:我假设命中测试:(CGPoint)与视图的边界有关?

UITapGestureRecognizer initWithTarget:action: 获取参数的方法?

如果有人能指出我正确的方向,那就太好了。任何帮助将不胜感激!

我根据您的代码进行了一些更改

- (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];
}

希望这能帮助你。

如果您需要包含包含手势识别器的标签的视图框架,您不能只做:

gestureRecognizer.view.superview.frame;

得到框架?

手势识别器的选择器应采用以下形式

- (void)gestureRecognizerDidFire:(UIGestureRecognizer *)recognizer;

如果将选择器设置为 @selector(gestureRecognizerDidFire:)并且可以访问其中的 View 属性,则识别器将自动传递。

最新更新