4

attempting to call this message from KVO observing. Once an image has downloaded this message gets sent. The message in the completion block also contains an animation which works correctly (animates correctly). This animation applies the transform without the animation occurring (waits the length of the animation, then just jumps to the final state).

/**
 *  Discover the subview with the supplied tag, attach the fullsize image to the view
 *  scale to fullsize and begin retract.
 *  @param viewTag int - #FUTURE USE# - The tag of the view to be animated.
 *  @param image UIImage - #FUTURE USE# - The image to be applied to the view.
 *  @return void
 */
- (void)animateViewWithTag:(int)viewTag andImage:(UIImage *)image {

    Panel *activePanel = [self.panels objectAtIndex:currentIndex];
    UIView *activePanelView = [self.view viewWithTag:activePanel.panelId];

    // Display the transition to the fullsize version of the panel image.
    // Determine the scale that needs to be applied to the view to show
    // the image in the appropriate scaling. If scaled image is greater than
    // the size of the screen, find the best fit.

    float scale = image.size.width / activePanelView.frame.size.width;

    if (image.size.width > self.view.window.frame.size.width || image.size.height > self.view.window.frame.size.height) {
        // The image will scale beyond the bounds of the window, scale must be adjusted.
        scale = self.view.window.frame.size.width / activePanelView.frame.size.width;
    }

    CGAffineTransform transform = CGAffineTransformMakeScale(scale, scale);

    [UIView animateWithDuration:1.0
                     animations:^{
                         // Get the fullsize image and display it in the growing panel.
                         [activePanelView setTransform:transform];
                         [NSThread sleepForTimeInterval:3.0];
                     } 
                     completion:^(BOOL finished) {
                         [self retractImage:activePanelView];
                     }];
}


#pragma mark - KVO

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {       
    int tmpInt = (int)context;        
    UIImage *tmpImage = [change objectForKey:NSKeyValueChangeNewKey];

    if ( keyPath == @"imgOriginal" ) {
        [self animateViewWithTag:[(Panel *)object panelId] andImage:tmpImage];             
    }   

}
5
  • did you try call this method on main thread?
    – NeverBe
    Mar 19, 2012 at 19:55
  • I wrapped the [self animateViewWithTag:] like so dispatch_async(dispatch_get_main_queue(), ^{ [animate .... had the same effect.
    – Kyle
    Mar 19, 2012 at 20:13
  • I wonder, why are you passing required viewTag and not using it :) Hope it helps.
    – A-Live
    Mar 19, 2012 at 22:36
  • Sorry, that is some future use stuff (or maybe not at all). Not relevant right now.
    – Kyle
    Mar 20, 2012 at 2:28
  • Turns out that if I comment out the call to [self retractImage:... the animation works correctly, except I need the reverse animation to occur following this one.
    – Kyle
    Mar 20, 2012 at 15:02

1 Answer 1

1

What's the purpose of the thread sleep?

If you make the main thread sleep then it's not going to update the animations in the meantime.

And if you're not calling this on the main thread then it's also not going to work because UIKit animation isn't thread safe and can only be reliably used from the main thread.

4
  • the sleep is simply to delay the animation at the final state. From your post though it seems like it may be sleeping through the whole animation.
    – Kyle
    Mar 20, 2012 at 11:35
  • Yes. The animation code doesn't fire at the end of the animation, it fires at the beginning, but because it's within a CATransaction, instead of setting the values directly, it then interpolates between them over time. I'm not really sure what you're trying to achieve by freezing the interface for 3 seconds after the animation (no user interaction will be possible if you sleep the main thread) but you'd be better off sticking it in the completion block instead of the animation block (I don't know for sure that will work either). Mar 20, 2012 at 13:57
  • If you just want to delay something from happening until 3 seconds after the animation finishes, I suggest using performSelector:withObject:afterDelay: or dispatch_after(...) if you prefer to use blocks Mar 20, 2012 at 14:00
  • I'm not interested in blocking the UI, so I'll remove the sleep from the code. The problem though is that the animation isn't happening at all, even if I place it on the main thread. If I change the property that I am animating (say, alpha) then it animates fine.
    – Kyle
    Mar 20, 2012 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.