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 回答
0

VSScrollview 看看 VSScrollview。它重用您传递给它的视图。它的使用就像使用 UITableview 一样简单。实现以下数据源方法

-(VSScrollViewCell *)vsscrollView:(VSScrollView *)scrollView viewAtPosition:(int)position;
// implement this to tell VSScrollview the view at position "position" . This view is VSScrollviewCell or subclass of VSScrollviewCell.

-(NSUInteger)numberOfViewInvsscrollview:(VSScrollView *)scrollview;

// implement this to tell VSScrollview about number of views you want in VSSCrollview.

还有其他可选的数据源和委托方法,您可以通过它们自定义 VSScrollview 的行为您可以为每个视图设置不同的宽度、高度、间距甚至内容大小。

于 2013-06-27T07:33:11.180 回答
0

我知道这是旧线程,但任何寻找解决方案的人都可以看看这个:https ://github.com/sumofighter666/ReusableScrollView

于 2018-06-30T21:23:13.080 回答