【问题标题】:UIView animateWithDuration: animations: completion: applies transform, not animatingUIView animateWithDuration:动画:完成:应用变换,不动画
【发布时间】:2012-03-19 19:44:07
【问题描述】:

试图从 KVO 观察中调用此消息。下载图像后,将发送此消息。完成块中的消息还包含一个正常工作的动画(动画正确)。此动画应用变换而不发生动画(等待动画的长度,然后跳转到最终状态)。

/**
 *  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];             
    }   

}

【问题讨论】:

  • 你试过在主线程上调用这个方法吗?
  • 我把 [self animateViewWithTag:] 包装成这样 dispatch_async(dispatch_get_main_queue(), ^{ [animate .... 有同样的效果。
  • 我想知道,你为什么要传递 required viewTag 而不使用它:) 希望它有所帮助。
  • 对不起,这是一些未来使用的东西(或者可能根本没有)。目前不相关。
  • 事实证明,如果我注释掉对 [self rectractImage:...

标签: objective-c ios uiview objective-c-blocks uiviewanimation


【解决方案1】:

线程休眠的目的是什么?

如果你让主线程休眠,那么它不会同时更新动画。

如果你不在主线程上调用它,那么它也不会工作,因为 UIKit 动画不是线程安全的,只能在主线程中可靠地使用。

【讨论】:

  • 睡眠只是将动画延迟到最终状态。从你的帖子来看,它似乎可能在整个动画中都在睡觉。
  • 是的。动画代码不会在动画结束时触发,而是在开始时触发,但因为它在 CATransaction 中,而不是直接设置值,它会随着时间的推移在它们之间进行插值。我不太确定通过在动画后将界面冻结 3 秒来实现什么(如果您使主线程休眠,则无法进行用户交互),但最好将其粘贴在完成块中而不是动画块(我不确定它是否会起作用)。
  • 如果你只是想延迟某些事情的发生直到动画结束后 3 秒,我建议使用 performSelector:withObject:afterDelay: 或 dispatch_after(...) 如果你更喜欢使用块
  • 我对阻塞 UI 不感兴趣,所以我将从代码中删除睡眠。但问题是动画根本没有发生,即使我把它放在主线程上。如果我更改了我正在制作动画的属性(比如 alpha),那么它的动画效果很好。
猜你喜欢
  • 1970-01-01
  • 2011-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多