gpt4 book ai didi

ios - UIScrollView 在更改框架时更改 contentOffset

转载 作者:可可西里 更新时间:2023-11-01 06:15:18 25 4
gpt4 key购买 nike

我在更改 ScrollView 的框架时遇到了一些问题...有时这种更改会导致滚动,有时不会。

情况是这样的。我有一个自定义的 UIScrollView 作为我的 Root View Controller 的 subview 。在父 View Controller 中,我有一个按钮来更改 ScrollView 的框架。它在屏幕的 2/3 和全屏之间变化。此帧更改不会导致 ScrollView 的 contentOffset 发生更改。

在 ScrollView 的内容中我有一些按钮。这个按钮也改变了 ScrollView 的框架。但是,有时,这种变化会使 ScrollView 改变其 contentOffset ...

我不明白为什么 ScrollView 中的按钮会导致这个...

我已经尝试在触摸按钮时调用委托(delegate)(并在父 View Controller 中更改 ScrollView 的框架)。并尝试更改 ScrollView 内的框架(触摸按钮时)。但同样会出现问题。

知道发生了什么或有一些解决方法可以解决这种情况吗?

谢谢!


我试图找出问题所在,在这里放一些简单的代码示例。

在我的 xib 中只有一个带有框架 ((20,44),(728,300)) 的 ScrollView 和一个调用事件“btnOut”的按钮。该 ScrollView 启用了分页并禁用了自动调整 subview 大小(其余为默认设置)。

我将使用 bool 值来判断 ScrollView 是否大 (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”这两个 Action 调用相同的函数 (changeFrameScrollView)。

- (void)changeFrameScrollView {
_bigScroll = !_bigScroll;

if (_bigScroll) {
[_scrollView setFrame:CGRectMake(20, 44, 728, 300)];
} else {
[_scrollView setFrame:CGRectMake(20, 44, 364, 300)];
}
}

我的预期:使用 ScrollView 外的按钮,框架更改按我预期的方式工作。

  • 如果它变为小框架,它会隐藏右侧的列并继续显示左侧的列。
  • 如果它变为大框架,它会从右侧显示列并且不移动 contentOffset。

** 独立于我正在查看的页面。

问题:使用 ScrollView 内的按钮,有时,框架变化会导致 contentOffset 发生变化。

  • 如果 scrollview 在小框架中并且它显示 02 列,我按下内部按钮它改变框架并显示 03 和 04 列(与按钮相同的 Action 在外部显示 02 和 03 列)。

好吧...我无法解释得那么好,所以我会在发生这种情况时举一个小例子。

最佳答案

我发现这是一个私有(private)方法导致的 => _adjustContentOffsetIfNecessary

它在某些情况下会改变偏移量,而在其他情况下不会改变。就像我在示例中解释的那样。

如果禁用分页,则不会调用该私有(private)方法,因此为了解决我的问题,我创建了 UIScrollView 的子类并覆盖方法 touchesShouldBegin(仅当按下 scrollView 内的按钮时调用) 并禁用分页:

- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view {
[self setPagingEnabled:NO];
return YES;
}

要再次启用分页,我使用委托(delegate) scrollViewWillBeginDragging

这样做,contentOffset 不会在我更改框架时更改,并且 scrollView 会继续使用分页。

关于ios - UIScrollView 在更改框架时更改 contentOffset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21787365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com