【问题标题】:Why new UIImageView can not Animating?为什么新的 UIImageView 不能动画?
【发布时间】:2011-02-07 00:27:44
【问题描述】:

我的 iPhone 应用中有一些这样的代码:

//fromView is a UIImageView. 
//self is a UIView.
        UIGraphicsBeginImageContext(fromView.bounds.size); 
        [fromView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
        UIImage *dummyFromImage = UIGraphicsGetImageFromCurrentImageContext(); 
        UIGraphicsEndImageContext();
        UIImageView* dummyImageView = [[UIImageView alloc] initWithImage:dummyFromImage]; 
        [self addSubview:dummyImageView];
        [UIView beginAnimations:nil context:nil]; 
        [UIView setAnimationDuration:0.5]; 
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView: dummyImageView cache:YES]; //line:9 
        [UIView commitAnimations];

dummyImageView只显示不翻转,如果将line9的dummyImageView改为fromView,fromView会翻转,请告诉我为什么?

【问题讨论】:

    标签: iphone ipad uiimageview


    【解决方案1】:

    我向 Apple 开发者技术支持提出了这个问题,他们说,

    “基本问题是,由于动画的时间安排,Core Animation 没有“先前”状态来制作动画,因为视图刚刚添加到视图层次结构中,因此当尝试转换时,所有它可以做的是显示“最终”状态。

    如果您改为在下一次 runloop 迭代中执行翻转,那么 Core Animation 将有时间为视图层创建初始状态,因此翻转将正确发生。您可以通过将翻转方法一分为二并使用 -performSelector:withObject:afterDelay: 来做到这一点,如下所示: "

    -(IBAction)flip {
    
       UIImageView* dummyImageView = [[UIImageView alloc] initWithImage:fromView.image];
       dummyImageView.frame = fromView.frame;
       [window addSubview:dummyImageView];
    
       [self performSelector:@selector(animate:) withObject:dummyImageView afterDelay:0.0];
    }
    
    -(void)animate:(UIView*)view {
    
       [UIView beginAnimations:nil context:nil];
       [UIView setAnimationDuration:0.5];
       [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView: view cache:YES];
       [UIView commitAnimations];
    }
    

    但是,既然您提到“fromView”是一个 UIImageView,我想知道您为什么要使用 -renderInContext: – 简单地使用“fromView”正在使用的相同图像并将其分配为您的图像更有效新的 UIImageView,因为这可以节省 CPU 时间和内存,特别是因为我在您的示例中注意到图像比您正在使用的视图小。

    【讨论】:

      【解决方案2】:
          [UIView setAnimationTransition:… forView: dummyImageView cache:YES]; //line:9 
      

      -setAnimationTransition:… 方法中的视图应分配给包含更改的视图。在你的情况下,self

      dummyImageView本身并没有改变(改变superview等外部变化是无关紧要的),所以动画什么也做不了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多