【问题标题】:UIScrollView with many subviews takes time to process具有许多子视图的 UIScrollView 需要时间来处理
【发布时间】:2014-11-06 06:39:39
【问题描述】:

我有一个带有子视图的UIScrollView。它包含产品/项目的结果列表。 UIViewUIImageViewUILabel。它可能包含许多项目。我的问题是当我按下按钮(网格按钮)将 UI 结果更改为网格视图时,完成处理需要时间。有没有办法让它更快?比如移除 screen/UIScrollView 中不可见的图片?

这是我的 listView 代码

-(void)actionList {

NSLog(@"List!!");
isGrid = NO;
for(UIView*iView in sv_results.subviews){
    for(UILabel *iView2 in iView.subviews){
        if([iView2 isMemberOfClass:[UILabel class]]){
            if(iView2.tag == 0)
            {
                iView2.frame = CGRectMake(80, 0, 150, 50);
                iView2.font = [UIFont boldSystemFontOfSize:12.0f];
            }
            else if(iView2.tag == 1)
            {
                pricewidth = [iView2.text sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13.0f]}];
                iView2.frame = CGRectMake(80, 20, pricewidth.width+1, 50);
                iView2.font = [UIFont systemFontOfSize:13.0f];
                for(UIView *discountline in iView2.subviews)
                {
                    discountline.frame = CGRectMake(0, iView2.frame.size.height/2, iView2.frame.size.width, 1);
                }
            }
            else if(iView2.tag == 2)
            {
                iView2.frame = CGRectMake(screenRect.size.width-60, 30, 50, 15);
                iView2.font = [UIFont systemFontOfSize:10.0f];
            }
            else if(iView2.tag == 3)
            {
                iView2.frame = CGRectMake(140, 20, 150, 50);
                iView2.font = [UIFont systemFontOfSize:13.0f];
            }
            else {
                iView2.frame = CGRectMake(80, 0, 150, 50);
                iView2.font = [UIFont boldSystemFontOfSize:12.0f];
            }
        }
        if([iView2 isMemberOfClass:[AsyncImageView class]]){
            iView2.frame = CGRectMake(15,12,50,50);
            iView2.layer.cornerRadius = 5;
        }
    }


    for(int i = 0;i<=allAvailableData;i++)
    {
        if(iView.tag == i)
        {
            iView.frame = CGRectMake(0,((i-1)*75),320,75);
        }
        //sv_results.contentSize = CGSizeMake(320,yPosition);//optional resize height scrollview from gridview
    }


    iView.layer.borderWidth = .3f;
}
sv_results.contentSize = CGSizeMake(320,(allAvailableData)*75);
}

【问题讨论】:

  • 您本质上是在询问 UITableView 是否存在,答案是肯定的。当 UITableViewCell 当前在屏幕上不可见时,UI 元素不会加载到内存中。它重用现有的单元格。
  • 我正在使用UIScrollView。我正在考虑将其更改为UITableView for List 和UICollectionView for Grid。你怎么看?
  • 我建议进行切换。 UiTableView 和 UICollectionView 就是为此目的而设计的。 UIScrollView 并不是真的要处理任意数量的动态渲染。一开始会有点复杂,但你会发现 UITableView/UICollectionView 为你做了很多工作。
  • 还有一个问题,UITableViewUICollectionView可以同时使用,隐藏和显示吗?这会是内存的问题吗?两者都包含相同的产品/项目。

标签: ios objective-c xcode uiscrollview


【解决方案1】:

由于图像,它可能需要一些时间。如果您在主线程中加载大量图像,则需要一些时间是正常的。尝试对 UITableView 使用延迟图像加载。我在我的项目中使用它并且易于实现和使用。 http://www.theappguruz.com/sample-code/ios-lazy-loading-images/ 有一些代码和帮助。我没有检查这个,但它看起来也很好https://developer.apple.com/library/ios/samplecode/LazyTableImages/Introduction/Intro.html

【讨论】:

    【解决方案2】:

    随便用

    scrollView.delaysContentTouches = NO;  
    

    一个布尔值,用于确定滚动视图是否延迟对触控手势的处理。
    如果此属性的值为 YES,滚动视图延迟处理触摸向下手势,直到它可以确定滚动是否是意图。如果值为 NO ,则滚动视图立即调用 touchesShouldBegin:withEvent:inContentView:。默认值为是。

    这意味着,scrollView 默认会延迟触摸,您可以通过上面的代码禁用该功能,
    希望它能解决你的问题。

    【讨论】:

      【解决方案3】:

      我认为您应该实现 UITableView 并且可以使用“dispatch_async”加载图像,这样就不会花费时间。我希望您可以通过这种方式得到解决方案。

      【讨论】:

        猜你喜欢
        • 2016-09-07
        • 1970-01-01
        • 2021-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-05
        相关资源
        最近更新 更多