我建立了一个简单的项目来尝试使用UIView transitionWithView:
动画片段,并遇到了奇怪的行为。
动画只在标签文本(在视图旋转到一半时更改)方面按预期工作,但颜色更改发生在动画结束时。有没有办法使用这种动画在动画进行到一半的时候改变背景颜色(图片)?我可以使用核心动画构建类似的东西,但我不想重复造轮子。我觉得我可能没有正确地使用这个方法。
完整的示例代码:
#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,它将在整个持续时间内通过动画来改变颜色属性。在这种情况下,我们几乎可以确定看到隐式层动画。