【问题标题】:UIButton as subview on complicated UIScrollViewUIButton 作为复杂 UIScrollView 上的子视图
【发布时间】:2013-06-02 09:09:50
【问题描述】:

在我的应用程序中,我实现了基于ARScrollViewEnhancer 的非常复杂的控制。我需要它来显示UIScrollView 的多个页面。所以这是我的UIScrollView 代码:

dateScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(28, 0, dateLabelMaxSize, 45)];
dateScroll.pagingEnabled = YES;
dateScroll.clipsToBounds = NO;
dateScroll.showsHorizontalScrollIndicator = NO;
dateScroll.backgroundColor = [UIColor clearColor];
dateScroll.contentSize = CGSizeMake(dateLabelMaxSize*[dayArray count], 45);

我使用dateScroll.clipsToBounds = NO; 来显示超出框架的页面。这是ARScrollViewEnhancer的代码:

ARScrollViewEnhancer *backForDate = [[ARScrollViewEnhancer alloc] initWithFrame:CGRectMake(0, 80, self.view.frame.size.width, 45)];
[backForDate addSubview:dateScroll];
backForDate._scrollView = dateScroll;
[self.view addSubview:backForDate];

然后我为UIScrollView 添加了子视图。这是UILabelsUIButtons

for (int i=0;i<[dayArray count];i++){

    UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(3+dateLabelMaxSize*i, 5, dateLabelMaxSize, 40)];
    dateLabel.backgroundColor = [UIColor clearColor];
    dateLabel.textAlignment = UITextAlignmentLeft;
    dateLabel.text = @"some text...";
    [dateScroll addSubview:dateLabel];


    UIButton *dateButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //dateButton.backgroundColor = [UIColor redColor];
    dateButton.frame = CGRectMake(3+dateLabelMaxSize*i, 5, dateLabelMaxSize-10, 40);
    dateButton.tag = i;
    [dateButton addTarget:self action:@selector(dateTapped:) forControlEvents:UIControlEventTouchUpInside];
    [dateScroll addSubview:dateButton];
} 

以及按钮的方法:

-(void)dateTapped:(UIButton*)button{
    NSLog(@"%i",button.tag);
}

所以,按钮不起作用((为什么? 谢谢。

UPD。

hitTest 方法代码。是:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    if ([self pointInside:point withEvent:event]) {
        return _scrollView;
    }
    return nil;
}

现在:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *scrv = _scrollView;
    UIView *superView = [super hitTest:point withEvent:event];
    if (superView == self)
    {
        CGPoint scrollViewpoint = [_scrollView convertPoint:point fromView:self];

        for(UIView *view in _scrollView.subviews) {
            if(CGRectContainsPoint(view.frame, scrollViewpoint)) {
                return view;
            }
        }

        return scrv;
    }

    return superView;
}

【问题讨论】:

  • 您必须设置滚动视图的 ContentSize
  • @Mutawe 我做到了。添加到我有问题的代码中。 dateScroll.contentSize = CGSizeMake(dateLabelMaxSize*[dayArray count], 45);

标签: ios uiscrollview uibutton


【解决方案1】:

如果您还在自定义控件中复制了 hitTest 方法,则会导致问题,因为它总是返回滚动视图而不是按钮。

您需要返回正确的子视图,而不是总是返回子视图,如下所示:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {    
    UIView *scrv = self.scrollView;
    UIView *superView = [super hitTest:point withEvent:event];
    if (superView == self)
    {
        CGPoint scrollViewpoint = [self.scrollView convertPoint:point fromView:self];
        for(UIView *view in self.scrollView.subviews) {
            if(CGRectContainsPoint(view.frame, scrollViewpoint)) {
                return view;
            }
        }
        return scrv;
    }
    return superView;
}

【讨论】:

  • @EugeneTrapeznikov 你实现了hitTest吗?您可以编辑您的问题以包含 hitTest 方法吗?
  • @EugeneTrapeznikov 你能详细说明什么不起作用吗?尝试在hitTest中也设置断点,看看是否从hitTest返回了UIButton。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-21
  • 1970-01-01
  • 2022-01-09
  • 2013-07-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多