3

一度にすべての画像を UIScrollView にロードしますが、それが悪い方法であることはわかっているので、最適化するより良い方法はありますか?

4

4 に答える 4

3
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    int currentPage = (scrollView.contentOffset.x / scrollView.frame.size.width);

    // display the image and maybe +/-1 for a smoother scrolling
    // but be sure to check if the image already exists, you can
    // do this very easily using tags
    if ([scrollView viewWithTag:(currentPage + 1)]) {
        return;
    } else {
        // view is missing, create it and set its tag to currentPage+1
        UIImageView *iv = [[UIImageView alloc] initWithFrame:
            CGRectMake((currentPage + 1) * scrollView.frame.size.width,
                       0,
                       scrollView.frame.size.width,
                       scrollView.frame.size.height)];
        iv.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.jpg",
                                                                  currentPage + 1]];
        iv.tag = currentPage + 1;
        [sv addSubview:iv];
    }

    /**
     * using your paging numbers as tag, you can also clean the UIScrollView
     * from no longer needed views to get your memory back
     * remove all image views except -1 and +1 of the currently drawn page
     */
    for (int i = 0; i < 50; i++) {
        if ((i < (currentPage - 1) || i > (currentPage + 1)) &&
            [scrollView viewWithTag:(i + 1)]) {
            [[scrollView viewWithTag:(i + 1)] removeFromSuperview];
        }
    }
}
于 2012-04-23T03:29:07.637 に答える
1

このチュートリアルを使用して、あなたを助けることができます。私もuser1212112が言ったことをお勧めしますが、見てWWDC 2011 Session 104 - Advanced Scroll View Techniquesください。

于 2012-04-19T06:45:04.460 に答える