2013-06-02 61 views
0

在我的应用程序中,我实现了真正复杂的控制,它基于ARScrollViewEnhancer。我需要它来显示UIScrollView的多个页面。所以这是我的代码UIScrollViewUIButton作为复杂的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); 
} 

所以,按钮没有工作((为什么 日Thnx

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; 
} 
+0

你必须设置你的滚动视图 – Mutawe

+0

@Mutawe我做到了的ContentSize。添加到我的代码中。 dateScroll.contentSize = CGSizeMake(dateLabelMaxSize * [dayArray count],45); –

回答

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; 
} 
+0

hm ..我明白..但它不起作用(( –

+0

@EugeneTrapeznikov你执行hitTest吗?你可以编辑你的问题,包括hitTest方法? –

+0

你可以看到它在upd –

相关问题