4

When i tap on ScrollView it call methods scrollViewWillBeginDecelerating and scrollViewDidEndDecelerating even after the scrollView's is set pagingEnabled to YES

I have scrollView with subViews and trying to set contentOffset according to subViews. But it doesn't work.

Here is my code:

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;
}

When I tap on any subView inside the scrollView, it calls the delegate methods:

scrollViewWillBeginDecelerating >> scrollViewDidScroll >> scrollViewDidEndDecelerating

And the contentOffset change to {0,0}

vaibhav
  • 4,038
  • 1
  • 21
  • 51

1 Answers1

0

Please Update Your Code like this:

Please set contentSize after for loop.

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;
}
vegda neel
  • 154
  • 12