ios - UIButton 作为复杂 UIScrollView 上的 subview

标签 ios uiscrollview uibutton

在我的应用程序中,我实现了非常复杂的控制,该控制基于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 添加了 subview 。这是 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;
}

最佳答案

如果您还在自定义控件中复制 hitTest 方法,则它是导致问题的原因,因为它始终返回 ScrollView 而不是按钮。

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

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

关于ios - UIButton 作为复杂 UIScrollView 上的 subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16881345/

相关文章:

ios - iCloud 和鞋盒应用程序 : How to atomically sync a file package?

ios - 如何通过滑动行来显示在自定义 tableview 单元格内创建的 View ?

iphone - UIView 和 UIControl 在 UIScrollView 内拖动时的区别

ios - 在 UIScrollView 之上使用 UIView 处理滚动,ScrollView 不会滚动

ios - 上一个单选按钮状态未清除 objective-c

swift - 如何以编程方式添加 uibutton 操作?

iphone - UIButton 选择颜色

ios - 如何在 ios 中使用 Swift 将 tableViewCell 的数据共享到下一个 viewController 而不使用 Storyboard

iphone - 点击区域仅位于 UIButton 的标签上

objective-c - 适用于 iOS 的 NSNumericSearch 解决方案