UIView animateWithDuration: animation: completion: apply transformation, not animation

tries to call this message from a KVO observation. After downloading the image, this message is sent. The message in the completion block also contains an animation that works correctly (animates correctly). This animation applies the transformation without the appearance of the animation (it expects the length of the animation, and then simply enters 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]; } } 
+4
source share
1 answer

What is the purpose of sleep?

If you do sleep in the main thread, it is not going to update animations in the meantime.

And if you do not call this in the main thread, then it will not work either, because the UIKit animation is not thread safe and can only be reliably used from the main thread.

+1
source

Source: https://habr.com/ru/post/1402301/


All Articles