How to make ui responsive all the time and do background updating?

前端 未结 3 561
囚心锁ツ
囚心锁ツ 2020-12-01 13:28

I am creating a application which displays 8 thumbnails per page and it can have n pages. Each of these thumbnails are UIViews and are added to UIScrollView. However i have

相关标签:
3条回答
  • 2020-12-01 14:08

    Whether you are using a subview or a separate ViewController for each "page" or element of the Scrollview, the jerkiness or poor performance can be helped by changing the location of your code.

    Specifically the apple sample code for a scrollview with pagecontrol has something like this:

    [self loadScrollViewWithPage:page - 1];
    [self loadScrollViewWithPage:page];
    [self loadScrollViewWithPage:page + 1];
    

    However, that code appears in their sample in the method "scrollViewDidScroll". It's trying to do multiple heavy lifting by both scrolling and loading at the same time. Even if your images are local this is nasty.

    If you move this and related code including a reference to the current page to "scrollViewDidEndDecelerating" the jerkiness of the interface is resolved because the loading happens while the scrollview is no longer moving.

    0 讨论(0)
  • 2020-12-01 14:09

    Grand Central Dispatch is easy to use for background loading. But GCD is only for after iOS4. If you have to support iOS3, performSelectorInBackground/performSelectorOnMainThread or NSOperationQueue are helpful.

    And, be careful almost UIKit classes are not thread-safe except drawing to a graphics context. For example, UIScrollView is not thread-safe, UIImage imageNamed: is not thread-safe, but UIImage imageWithContentsOfFile: is thread-safe.

    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    dispatch_queue_t concurrentQueue =
        dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_async(concurrentQueue, ^{
    
        dispatch_apply([thumbnails count], concurrentQueue, ^(size_t index) {
    
            Thumbnail *thumbnail = [thumbnails objectAtIndex:index];
            thumbnail.image = [UIImage imageWithContentsOfFile:thumbnail.url];
    
            dispatch_sync(mainQueue, ^{
    
                /* update UIScrollView using thumbnail. It is safe because this block is on main thread. */
    
            });
        }
    
        /* dispatch_apply waits until all blocks are done */
    
        dispatch_async(mainQueue, ^{
            /* do for all done. */
        });
    }
    
    0 讨论(0)
  • 2020-12-01 14:09

    I was having a similar problem.
    What i did was at an instance i kept only 3 pages in the memory and cleared remaining all.
    If suppose there are 3 screens s1, s2, s3. And the user is viewing s2. Whenever he scrolls to s3 i will remove s1 and load a new page s4.
    So that the users will have a better experience. And less memory will be occupied.

    0 讨论(0)
提交回复
热议问题