【问题标题】:UIScrollView only works if the child views aren't hitUIScrollView 仅在未命中子视图时才有效
【发布时间】:2009-11-10 19:45:40
【问题描述】:

我有一个不能向右滚动的滚动视图,我已经简化了下面的代码。 它绘制视图和一些水平按钮,我在实际代码中添加了更多内容。 如果拖动按钮之间的空白,视图会滚动。如果您碰巧将手指放在按钮上,它将不会滚动。 在提出相关建议后,我尝试添加 delaysContentTouches = 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; 
}

【问题讨论】:

    标签: cocoa-touch uiscrollview uibutton drag


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      看起来正在发生的一件事是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);
      

      }

      【讨论】:

      • 谢谢格雷格,我会试试的。我同意它的结构很差,我认为是旧代码。你认为这会解决滚动问题吗?罗伯
      • 好的,进行了更改。仍然有按钮不可点击的问题。我应该尝试制作一个问题的迷你应用程序并上传吗?我很茫然。
      猜你喜欢
      • 2019-12-19
      • 2014-04-07
      • 2013-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多