UIScrollView仅在未命中子视图时才有效

时间:2009-11-10 19:45:41

标签: cocoa-touch uiscrollview uibutton drag

我有一个不向右滚动的滚动视图,我简化了下面的代码。 它绘制了视图和一些水平按钮,我在实际代码中添加了更多东西。 如果拖动按钮之间的空白,则视图会滚动。如果您碰巧将手指放在按钮上,它将无法滚动。 在一个相关的建议之后,我试图添加delayedContentTouches = YES行,但它似乎没有什么区别。 UIScrollview with UIButtons - how to recreate springboard?

我做错了什么? TIA, 罗布

更新了代码

- (void)viewDidLoad {
    l = [self landscapeView];
    [self.view addSubview:l];
    [l release];    
}

- (UIScrollView *) landscapeView {
    // LANDSCAPE VIEW
    UIScrollView *landscapeView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 325)];
    landscapeView.backgroundColor = [UIColor whiteColor];
    landscapeView.delaysContentTouches = YES;
    NSInteger iMargin, runningY, n;
    iMargin = 3;
    runningY = iMargin;

    for (n = 1; n <= 38; n++) {
        //add day labels
        UIButton *templabel = [[UIButton alloc] initWithFrame:CGRectMake(iMargin,runningY,320 - ( 2 * iMargin),20)];
        templabel.backgroundColor = [UIColor grayColor];
        [landscapeView addSubview:templabel];
        [templabel release];
        runningY = runningY + 30;
    }
    landscapeView.contentSize = CGSizeMake( 320, runningY);

    return landscapeView; 
}

2 个答案:

答案 0 :(得分:0)

我不能复制你的结果;当我创建一个scrollView并添加按钮时,即使在按钮上进行触摸操作,滚动仍然有效。您是否在设置简化代码中未显示的任何其他UIScrollView属性或子类化任何方法?

答案 1 :(得分:0)

看起来有一件事情是,landScapeView的getter是一个复杂的构造函数......所以每次你做[self landscapeView]或self.landscapeView时,你都会创建一个全新的UIScrollView并对其进行操作

我会在你的.h文件中尝试这样的事情:

@interface MyClass: MyParent {
  UIScrollView *landscapeView;
}

@property (retain) UIScrollView *landscapeView;

...并在.m文件中:

@implementation MyClass

@synthesize landscapeView;

(void)viewDidLoad 
{ l = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 325)] autorelease];
  [self.view addSubview:self.landscapeView]; [l release]; 
  landscapeView.backgroundColor = [UIColor whiteColor];
  landscapeView.delaysContentTouches = YES;
   NSInteger iMargin, runningY, n;
  iMargin = 3;
  runningY = iMargin;
  for (n = 1; n <= 38; n++) { //add day labels 
       UIButton *templabel = [[UIButton alloc] initWithFrame:CGRectMake(iMargin,runningY,320 - ( 2 * iMargin),20)];
       templabel.backgroundColor = [UIColor grayColor]; 
       [landscapeView addSubview:templabel]; 
       [templabel release]; runningY = runningY + 30; 
  } 
  landscapeView.contentSize = CGSizeMake( 320, runningY);

}