xed hacker
xed hacker

Reputation: 44

UIScrollView with many subviews takes time to process

I have an UIScrollView with subviews. It contains a result list of products/items. UIViewwith UIImageView and UILabel. It may contain many items. My problem is when I'm pressing the button(Grid Button) to change the UI result into grid view, it takes time before it finishes the processing. Is there a way to make this faster? Like remove images which are not visible in screen/UIScrollView?

Here's my code for 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);
}

Upvotes: 0

Views: 133

Answers (3)

Payal Maniyar
Payal Maniyar

Reputation: 4402

I think you should implement UITableView and you can load image with "dispatch_async" so it will not take time. I hope you may get solution this way.

Upvotes: 0

Yucel Bayram
Yucel Bayram

Reputation: 1663

It can take a time because of images. If you are loading a lots of images in main thread then it is normal to take a time. Try to use lazy image loading with UITableView. I use this in my project and easy to implement and use. http://www.theappguruz.com/sample-code/ios-lazy-loading-images/ there is some codes and helps. I did not check this but it looks also fine https://developer.apple.com/library/ios/samplecode/LazyTableImages/Introduction/Intro.html

Upvotes: 1

l0gg3r
l0gg3r

Reputation: 8954

Just use

scrollView.delaysContentTouches = NO;  

A Boolean value that determines whether the scroll view delays the handling of touch-down gestures.
If the value of this property is YES, the scroll view delays handling the touch-down gesture until it can determine if scrolling is the intent. If the value is NO , the scroll view immediately calls touchesShouldBegin:withEvent:inContentView:. The default value is YES.

This means, scrollView will delay touches by default, you can disable that functionality by above code,
Hope it will solve your problem.

Upvotes: 0

Related Questions