gpt4 book ai didi

ios - 当 scrollView 设置 pagingEnabled 为 YES 时,点击 ScrollView 调用 `scrollViewWillBeginDecelerating`

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:32:05 26 4
gpt4 key购买 nike

当我点击 ScrollView 时,它会调用方法 scrollViewWillBeginDeceleratingscrollViewDidEndDecelerating 甚至在 scrollView's 设置为 pagingEnabled 之后

我有 scrollViewsubViews 并尝试根据 subViews 设置 contentOffset。但它不起作用。

这是我的代码:

self.myScrollView.frame = CGRectMake(0, 0, pageWidth, pageHeight);
self.myScrollView.contentSize = CGSizeMake(pageWidth * self.contentViewControllers.count, pageHeight);

for (NSInteger i = 0; i < self.contentViewControllers.count; i++) {
UILabel *label = [UILabel new];
label.textColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentCenter;
label.text = [NSString stringWithFormat:@"Page %ld",i];
label.backgroundColor = [UIColor randomColor];
label.frame = CGRectMake(i * pageWidth, 0, pageWidth, pageHeight);
[self.myScrollView addSubview:label];
}

self.myScrollView.contentInset = UIEdgeInsetsMake(120, 0, 0, 0);
self.myScrollView.contentOffset = CGPointMake(0, - 120);
self.myScrollView.scrollIndicatorInsets = UIEdgeInsetsMake(120, 0, 0, 0);

- (UIScrollView *)myScrollView{
if (!_myScrollView) {
_myScrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
_myScrollView.backgroundColor = [UIColor blackColor];
_myScrollView.pagingEnabled = YES;
_myScrollView.directionalLockEnabled = YES;
_myScrollView.delegate = self;
//_myScrollView.delaysContentTouches = NO;
//_myScrollView.scrollsToTop = NO;
}

return _myScrollView;
}

当我点击 scrollView 中的任何 subView 时,它会调用委托(delegate)方法:

scrollViewWillBeginDecelerating >> scrollViewDidScroll >> scrollViewDidEndDecelerating

contentOffset 更改为 {0,0}

最佳答案

请像这样更新您的代码:

请在for循环后设置contentSize。

self.myScrollView.frame = CGRectMake(0, 0, pageWidth, pageHeight);

for (NSInteger i = 0; i < self.contentViewControllers.count; i++) {
UILabel *label = [UILabel new];
label.textColor = [UIColor whiteColor];
label.textAlignment = NSTextAlignmentCenter;
label.text = [NSString stringWithFormat:@"Page %ld",i];
label.backgroundColor = [UIColor randomColor];
label.frame = CGRectMake(i * pageWidth, 0, pageWidth, pageHeight);
[self.myScrollView addSubview:label];
}

self.myScrollView.contentInset = UIEdgeInsetsMake(120, 0, 0, 0);
self.myScrollView.contentOffset = CGPointMake(0, - 120);
self.myScrollView.scrollIndicatorInsets = UIEdgeInsetsMake(120, 0, 0, 0);

self.myScrollView.contentSize = CGSizeMake(pageWidth * self.contentViewControllers.count, pageHeight);

- (UIScrollView *)myScrollView{
if (!_myScrollView) {
_myScrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
_myScrollView.backgroundColor = [UIColor blackColor];
_myScrollView.pagingEnabled = YES;
_myScrollView.directionalLockEnabled = YES;
_myScrollView.delegate = self;
//_myScrollView.delaysContentTouches = NO;
//_myScrollView.scrollsToTop = NO;
}

return _myScrollView;
}

关于ios - 当 scrollView 设置 pagingEnabled 为 YES 时,点击 ScrollView 调用 `scrollViewWillBeginDecelerating`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40715012/

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