gpt4 book ai didi

ios - UIButton 作为复杂 UIScrollView 上的 subview

转载 作者:行者123 更新时间:2023-11-29 03:52:51 24 4
gpt4 key购买 nike

在我的应用程序中,我实现了非常复杂的控制,该控制基于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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com