0

我有一个不能向右滚动的滚动视图,我已经简化了下面的代码。它绘制视图和一些水平按钮,我在实际代码中添加了更多内容。如果拖动按钮之间的空白,视图会滚动。如果您碰巧将手指放在按钮上,它将不会滚动。在提出相关建议后,我尝试添加 delaysContentTouches = YES 行,但似乎没有什么不同。 带有 UIButtons 的 UIScrollview - 如何重新创建跳板?

我究竟做错了什么?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; 
}
4

2 回答 2

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);

}

于 2009-11-20T20:23:25.197 回答
0

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

于 2009-11-10T20:27:25.423 回答