iRSSの日記

はてなダイアリーiRSSの日記の続き

UIScrollViewの横方法無限スクロール

#pragma mark - Layout
- (void)recenterIfNecessary {
    CGPoint currentOffset = [self contentOffset];
    CGFloat contentWidth = [self contentSize].width;
    CGFloat centerOffsetX = (contentWidth - [self bounds].size.width) / 2.0;
    CGFloat distanceFromCenterX = fabs(currentOffset.x - centerOffsetX);
    if (distanceFromCenter > (contentWidth /4.0)){
        self.contentOffset = CGPointMake(centerOffsetX,currentOffset.y);
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self recenterIfNecessary];
    
    self.backView.backgroundColor = [UIColor redColor];
}