views:

91

answers:

2

Would like to execute some code in a loop, but I want to pause it while a uiscrollview is scrolling.

I've created a BOOL named isScrolling that is set to "YES" in the scrollViewDidScroll method and is set to "NO" in the scrollViewDidEndDecelerating method.

I'd like to put the loop in the scrollViewDidEndDecelerating method, but have it paused when isScrolling == YES and then restart it when isScrolling == NO.

I am looping through an array, so I'd like the loop to pick up where it left off in the array.

For example, here's a simple loop that continually counts higher by increments of 1 that I placed in the viewDidAppear method:

for (int i = 1; i > 0; i++) {

    NSLog(@"i = %d", i);

    if (isScrolling == YES) {
        NSLog(@"break");
        break;
    }
}           

But, all scrolling is disabled while this loop is running for some reason so I can't cancel it.

How can I do this?

Thanks!

A: 

I'm not entirely sure what you're aiming for in a use case for this - I'm guessing you have something happening in a loop that's slowing down your UIScrollView scrolling and making it stutter a bit more than you'd like.

I'm going to guess that the answer is make the background task that you're working with asynchronous so that it's not taking up so much CPU resource in general. If you really want to bind into reacting if the scroll view is moving, then you want the UIScrollView Delegate methods to be your primary trigger points. The ones that stand out as potential uses:

– scrollViewWillBeginDragging:
– scrollViewDidEndDragging:willDecelerate:
– scrollViewDidEndScrollingAnimation:

...

Don't invoke the looping in viewDidLoad or you'll block the main UI interaction thread. Instead trigger off a background process using classic threading or better yet NSOperationQueue to do whatever looping/background thing you want done. If you have that queue reference in the view controller, you could also hook into that to pause or cancel those operations when a person decides to scroll using the delegate methods above.

heckj
You are correct in that I am trying to run a method that is causing problems with the performance of my tableView. Unfortunately, I am working with images and uiviews that are not thread-safe so I can't asynchronously load them on a background thread.
Jonah
Unfortunately, the methods you suggest don't seem to work as all scrolling and touches seem to be disabled while the loop is running. See my revised question for some more clarification. Thanks for the help!
Jonah
+2  A: 

You say this loop is within viewDidLoad, which means it's being executed by the main application thread (and thus the "UI thread). If that's the case, there's nothing you can do. Your UI isn't going to be doing anything at all while that loop is running because it's the same thread executing the loop.

Without knowing more about what it is you're actually doing in that loop, it's hard to suggest what you could do to offload that work into a separate thread.

imaginaryboy
I'm adding a group of images into some subviews to they can be viewed quickly later. Unfortunately, uiviews and images aren't thread-safe. I've experimented using Grand Central Dispatch and it generally works, except it causes instability because of the non thread-safe elements.
Jonah
This may be barking up the wrong tree since I've never tried it. I wonder if you can construct these various UIViews/UIImageViews in a separate thread, but then ship off responsibility for adding them to the actively displayed view hierarchy with performSelectorOnMainThread.I know this would NOT be a good idea in general in Windows, but don't know if UIViews have some sort of internal thread affinity in the wonderful world of iOS.
imaginaryboy
@imaginaryboy is right on the money - if your loop is in viewDidLoad, then the main UI thread which is responsible for all the visual animations and user interactions will get completely blocked.
heckj