2015-07-03 47 views
3

我已经设置了一个简单的项目来试验UIView transitionWithView:动画片段,并且遇到了相当奇怪的行为。UIView transitionWithView:带背景颜色的动画行为

enter image description here

动漫作品,因为只有与标签的文本(改变它,而观点是半途旋转)预期,但颜色变化发生在动画的最后,呵呵。有没有办法使用这种动画在动画中途改变背景颜色(图像?)?我可以用Core Animation自己创建类似的东西,但我不想重新发明轮子。我觉得我只是没有使用此方法正确

所有示例代码:

#import "ViewController.h" 

@interface ViewController() 
@property (weak, nonatomic) IBOutlet UIView *containerView; 
@property (weak, nonatomic) IBOutlet UIView *innerView; 
@property (weak, nonatomic) IBOutlet UILabel *label; 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.innerView.layer.cornerRadius = 25.0; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (UIColor *)randomColor { 

    CGFloat hue = (arc4random() % 256/256.0); 
    CGFloat saturation = (arc4random() % 128/256.0) + 0.5; 
    CGFloat brightness = (arc4random() % 128/256.0) + 0.5; 
    UIColor *color = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1]; 

    return color; 
} 


- (IBAction)flipAction:(id)sender { 

    static int i = 0; 
    i++; 

    [UIView transitionWithView:self.containerView 
         duration:0.5 
         options:UIViewAnimationOptionTransitionFlipFromTop 
        animations:^{ 

         self.innerView.backgroundColor = [self randomColor]; 
         self.label.text = [NSString stringWithFormat:@"Flip - %d", i]; 

        } completion:nil]; 

} 

@end 

旁注。有趣的是,我发现当动画选项被设置为UIViewAnimationOptionTransitionCrossDissolve时,它会做同样的事情,但是!如果将其设置为0,则会在持续时间内为颜色属性设置动画。在这种情况下,很肯定我们可以看到隐含层动画

enter image description here

+2

你可以用UIView performWithoutAnimation块包装换色线,看看是否有效? – rounak

+0

@rounak whoooa,它的工作! :O如果您将此作为答案发布,我会接受它。你能解释一下吗?为什么**能起作用?会很感激。 – NKorotkov

+0

添加了一个答案,以及为什么我认为这有效。 – rounak

回答

7

裹在performWithoutAnimation块的颜色变线。

为什么我猜这个作品是转换在转换块之前捕获屏幕截图,在转换块之后捕获屏幕截图。当你改变颜色而没有执行无动画时,后面的截图不会立即改变颜色(导致它动画)。随着performWithoutAnimation,后截图具有最终的颜色,它可以在截图后正确捕获。