开发者

Repeated update of UIImage isn't shown on screen

开发者 https://www.devze.com 2023-03-29 12:19 出处:网络
I\'m working with an iOS app that has an animation of a spinning body. A series of UIImages are stored in an NSArray and a variable keeps track on the index to the current image.

I'm working with an iOS app that has an animation of a spinning body. A series of UIImages are stored in an NSArray and a variable keeps track on the index to the current image.

The following method updates the UIImageView

- (void)renewImage{
    [rotatingBody.image release];
    rotatingBody.image = [[UIImage alloc] initWithContentsOfFile:[currentPerson.imageArray objectAtIndex:pictureIndex]];
}

Up until now i have called the method as a response to touch events. From the touch event I calculate a new pictureIndex and then call [self renewImage]. It works perfectly.

However, now I want to add momentum to the animation. Instead of calculating the nex开发者_JS百科t image right away I calculate a velocity. I'm planning to have a loop in a separate thread to update pictureIndex by using the velocity variable, decrease the velocity and eventually call [self renewImage].

ENTER THE PROBLEM

Whenever I call [self renewImage] more then once in a row in the same function (e.g. in a loop or just twice separated by some sort of sleep) the screen won't update until after the last call. It is as if the device (or compilator) rationalizes away the first updates even though I'm certain that the renewImage method has been visited (thanks to tracing).

Here is some test code that doesn't work:

while (velocity > 0) {
        pictureIndex++;
        pictureIndex = pictureIndex % [self getNumberOfImages];
        if (pictureIndex < 0)
        {
            pictureIndex += [self getNumberOfImages];
        }
        velocity -= 20;
        NSLog(@"velocity: %f", velocity);
        [self renewImage];
        [NSThread sleepForTimeInterval:0.5];
    }

Neither does this:

pictureIndex += 20;
[self renewImage];
sleep(1);
pictureIndex += 20;
[self renewImage];

Any thoughts?


Use an NSTimer callback to change the image, this way everything happens in the run loop rather than on a separate thread.

If an NSTimer isn't sufficient for your needs, you'll need to call a method on your main thread to change the image as UI change on other threads do not update immediately.

0

精彩评论

暂无评论...
验证码 换一张
取 消