当我点击ScrollView时,即使scrollViewWillBeginDecelerating
设置为scrollViewDidEndDecelerating
到scrollView's
pagingEnabled
和YES
我scrollView
subViews
并尝试根据contentOffset
设置subViews
。但它不起作用。
这是我的代码:
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;
}
当我点击subView
内的任何scrollView
时,它会调用委托方法:
scrollViewWillBeginDecelerating
&gt;&gt; scrollViewDidScroll
&gt;&gt; scrollViewDidEndDecelerating
contentOffset
更改为{0,0}
答案 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;
}