Код | @interface InfiniteScrollView : UIScrollView @end;
@implementation InfiniteScrollView { bool _recentered; }
- (void)layoutSubviews { [super layoutSubviews]; [self recenterIfNecessary]; }
- (void)recenterIfNecessary { CGPoint currentOffset = [self contentOffset]; CGFloat contentWidth = [self contentSize].width; CGFloat centerOffsetX = (contentWidth - [self bounds].size.width) / 2.0; CGFloat distanceFromCenter = fabs(currentOffset.x - centerOffsetX); if (distanceFromCenter < contentWidth / 4.0) { if (_recentered) _recentered = NO; return; } if (!_recentered) { _recentered = YES; // ! NSLog(@"recenter"); sleep(1); } } @end;
@interface ViewController : UIViewController @end;
@implementation ViewController
- (void)viewDidLoad { [super viewDidLoad]; InfiniteScrollView * scrollView = [[InfiniteScrollView alloc] initWithFrame:[self viewBounds]]; scrollView.contentSize = CGSizeMake([self viewBounds].size.width * 3, [self viewBounds].size.height); scrollView.contentOffset = CGPointMake([self viewBounds].size.width, 0); scrollView.pagingEnabled = YES; [self.view addSubview:scrollView]; UIView *view1 = [[UIView alloc] initWithFrame:[self viewBounds]]; view1.backgroundColor = [UIColor redColor]; [scrollView addSubview:view1]; UIView *view2 = [[UIView alloc] initWithFrame:CGRectOffset([self viewBounds], [self viewBounds].size.width, 0)]; view2.backgroundColor = [UIColor greenColor]; [scrollView addSubview:view2]; UIView *view3 = [[UIView alloc] initWithFrame:CGRectOffset([self viewBounds], [self viewBounds].size.width * 2, 0)]; view3.backgroundColor = [UIColor blueColor]; [scrollView addSubview:view3]; }
- (CGRect) viewBounds { CGRect bounds = [[UIScreen mainScreen] bounds]; // portrait bounds if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) bounds.size = CGSizeMake(bounds.size.height, bounds.size.width); return bounds; }
@end
|
Посмотрите на такую конструкцию. Есть три View, которые находятся на ScrollView. На некотором scrollView.contentOffset нужно подготовить новую View, что занимает немного времени.
Код | // ! NSLog(@"recenter"); sleep(1);
|
Т.е. вместо sleep(1); я сознаю экземпляр класса, в котором много элементов UI. Выполняя это в главном потоке, ScrollView приостанавливается. А нужно, чтоб он продолжал скролиться и одновременно создавался объект с UI. Что можно сделать? |