0

我有 2 个视图与 NavigationController 连接。第二个视图是带有页面控件的滚动视图。我的主视图有 1 个 UIButton。当按下它时获得 3-4 秒的延迟。scrollView 中有 21 个视图。而且我在scrollView中的视图之间滚动时会突然出现。如何减少延迟并消除突发?

我的viewDidLoad方法:

-(void)viewDidLoad {
self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0.0f, 280.0f, 320.0f, 20.0f)];
[self.view addSubview:self.pageControl];

self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 30.0f, 480.0f, 250.0f)];
[self.view addSubview:self.scrollView];

pageControlBeingUsed = NO;

NSInteger firstNumber = 1;
for (NSUInteger i = 0; i < 20; i++) {
    BIDExercisePrototype *view = [[BIDExercisePrototype alloc] initWithNumber:firstNumber];
    [self.viewsArray addObject:view];
    firstNumber += 50;
}


for (NSUInteger i = 0; i < 20; i++) {
    UIViewController *someController = [[UIViewController alloc] init];
    someController = [self.viewsArray objectAtIndex:i];
    someController.view.frame = CGRectMake(480.0f*i, 0.0f, 480.0f, 280.0f);
    [self.scrollView addSubview:someController.view];
}
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * self.viewsArray.count, self.scrollView.frame.size.height);
self.pageControl.backgroundColor = [UIColor clearColor];
self.pageControl.currentPage = 0;
self.pageControl.numberOfPages = self.viewsArray.count;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.pagingEnabled = YES;
self.scrollView.delegate = self;
[self.pageControl addTarget:self action:@selector(changePage) forControlEvents:UIControlEventValueChanged];
}

- (void)scrollViewDidScroll:(UIScrollView *)sender {
if (!pageControlBeingUsed) {
    CGFloat pageWidth = self.scrollView.frame.size.width;
    int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    self.pageControl.currentPage = page;
}
    }

    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
    }

    - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
    }

    - (void)changePage {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];

pageControlBeingUsed = YES;
    }
4

2 回答 2

1

我可以建议您忘记重新编码轮子(可以这么说)并使用已经解决问题的现有项目

于 2012-06-19T06:45:37.797 回答
1

我认为您应该能够遵循相同的做法来提高 UITableView 的滚动性能。

手动绘制,每个单元格中只有一个视图。

查看以下 SO 帖子,其中讨论了通过以特定方式呈现单元格来提高滚动性能的多种方法。

提高 iPhone UITableView 滚动性能的技巧?

于 2012-06-18T22:40:41.707 回答