简体   繁体   中英

UIButton as subview on complicated UIScrollView

In my app i implement really complicated control, which based on ARScrollViewEnhancer . I need it for displaying multiple pages of UIScrollView . So this is my code for 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);

I use dateScroll.clipsToBounds = NO; for displaying pages out of frame. This is code for 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];

Then i added subviews for UIScrollView . This is UILabels and UIButtons .

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

And method for buttons:

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

So, buttons not worked (( why? Thnx.

UPD.

hitTest methode code. Was:

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

Now:

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

If you also copy the hitTest method in your custom control, it is the one causing the issue because it always return the scroll view instead of the button.

You need to return the correct subviews instead of always returning the subview, something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM