【发布时间】:2014-02-14 18:55:40
【问题描述】:
我在更改滚动视图的框架时遇到了一些问题...有时这种更改会导致滚动,有时不会。
情况是这样的。我有一个自定义 UIScrollView 作为我的根视图控制器的子视图。 在父视图控制器中,我有一个按钮来更改滚动视图的框架。它在屏幕的 2/3 和全屏之间切换。此帧更改不会导致滚动视图的 contentOffset 发生更改。
在滚动视图的内容中,我有一些按钮。这个按钮也改变了滚动视图的框架。 但是,有时,此更改会使滚动视图更改其 contentOffset...
我不明白为什么滚动视图中的按钮会导致这个...
我已经尝试在触摸按钮时调用委托(并在父视图控制器中更改滚动视图的框架)。 并试图改变滚动视图内的框架(当按钮被触摸时)。但问题发生的方式相同。
知道发生了什么或解决这种情况的方法吗?
谢谢!
我试图隔离问题,在这里放一些简单的代码示例。
在我的 xib 中只有一个带有框架 ((20,44),(728,300)) 的滚动视图和一个调用事件“btnOut”的按钮。 该滚动视图启用了分页并禁用了自动调整子视图(其余为默认值)。
我将使用布尔值来判断滚动视图是否大(bigScroll)。
这是我的 viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
// the initial frame of scrollView is the big frame
_bigScroll = YES;
// here I put a big content to create some pages in scrollView
[_scrollView setContentSize:CGSizeMake(2184, 300)];
// I put one button in each column
// When scrollView is with big frame it shows two columns and when it is with small frame it shows only one column.
UIButton *btn01 = [UIButton buttonWithType:UIButtonTypeSystem];
[btn01 setFrame:CGRectMake(159, 30, 46, 30)];
[btn01 setTitle:@"01" forState:UIControlStateNormal];
[btn01 addTarget:self action:@selector(btnIn:) forControlEvents:UIControlEventTouchDown];
[_scrollView addSubview:btn01];
UIButton *btn02 = [UIButton buttonWithType:UIButtonTypeSystem];
[btn02 setFrame:CGRectMake(523, 30, 46, 30)];
[btn02 setTitle:@"02" forState:UIControlStateNormal];
[btn02 addTarget:self action:@selector(btnIn:) forControlEvents:UIControlEventTouchDown];
[_scrollView addSubview:btn02];
UIButton *btn03 = [UIButton buttonWithType:UIButtonTypeSystem];
[btn03 setFrame:CGRectMake(887, 30, 46, 30)];
[btn03 setTitle:@"03" forState:UIControlStateNormal];
[btn03 addTarget:self action:@selector(btnIn:) forControlEvents:UIControlEventTouchDown];
[_scrollView addSubview:btn03];
UIButton *btn04 = [UIButton buttonWithType:UIButtonTypeSystem];
[btn04 setFrame:CGRectMake(1251, 30, 46, 30)];
[btn04 setTitle:@"04" forState:UIControlStateNormal];
[btn04 addTarget:self action:@selector(btnIn:) forControlEvents:UIControlEventTouchDown];
[_scrollView addSubview:btn04];
UIButton *btn05 = [UIButton buttonWithType:UIButtonTypeSystem];
[btn05 setFrame:CGRectMake(1615, 30, 46, 30)];
[btn05 setTitle:@"05" forState:UIControlStateNormal];
[btn05 addTarget:self action:@selector(btnIn:) forControlEvents:UIControlEventTouchDown];
[_scrollView addSubview:btn05];
UIButton *btn06 = [UIButton buttonWithType:UIButtonTypeSystem];
[btn06 setFrame:CGRectMake(1979, 30, 46, 30)];
[btn06 setTitle:@"06" forState:UIControlStateNormal];
[btn06 addTarget:self action:@selector(btnIn:) forControlEvents:UIControlEventTouchDown];
[_scrollView addSubview:btn06];
}
“btnIn”和“btnOut”两个动作调用相同的函数 (changeFrameScrollView)。
- (void)changeFrameScrollView {
_bigScroll = !_bigScroll;
if (_bigScroll) {
[_scrollView setFrame:CGRectMake(20, 44, 728, 300)];
} else {
[_scrollView setFrame:CGRectMake(20, 44, 364, 300)];
}
}
我的预期: 使用滚动视图之外的按钮,框架更改按我的预期工作。
- 如果更改为小框架,则会从右侧隐藏该列并继续从左侧显示该列。
- 如果它变成大框架,它会从右侧显示列并且不要移动 contentOffset。
** 独立于我正在查看的页面。
问题: 有时,使用滚动视图内的按钮,框架更改会导致 contentOffset 发生更改。
嗯...我不能解释得这么好,所以我举了一个小例子。
【问题讨论】:
-
也许一些代码会有所帮助。另外,描述内容偏移量的变化。
标签: ios uiscrollview