UIView transitionWithView:带背景色的动画行为
UIView transitionWithView: animation behaviour with background color
我建立了一个简单的项目来试验 UIView transitionWithView:
动画片段和 运行 相当奇怪的行为。
动画仅对标签的文本按预期工作(在视图旋转一半时更改它),但颜色变化发生在动画结束时,嗯。有没有办法使用这种动画在动画中途更改背景颜色(图像?)?我可以用 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,它将在整个持续时间内为颜色 属性 设置动画。很确定在那种情况下我们看到隐式层动画
将颜色变化线包裹在 performWithoutAnimation 块中。
为什么我猜这行得通是因为转换捕获了转换块之前和之后的屏幕截图 运行。当您在不使用 performWithoutAnimation 的情况下更改颜色时,之后的屏幕截图将不会立即更改颜色(因为它具有动画效果)。使用 performWithoutAnimation,后截图具有最终颜色,在后截图中被正确捕获。
我建立了一个简单的项目来试验 UIView transitionWithView:
动画片段和 运行 相当奇怪的行为。
动画仅对标签的文本按预期工作(在视图旋转一半时更改它),但颜色变化发生在动画结束时,嗯。有没有办法使用这种动画在动画中途更改背景颜色(图像?)?我可以用 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,它将在整个持续时间内为颜色 属性 设置动画。很确定在那种情况下我们看到隐式层动画
将颜色变化线包裹在 performWithoutAnimation 块中。
为什么我猜这行得通是因为转换捕获了转换块之前和之后的屏幕截图 运行。当您在不使用 performWithoutAnimation 的情况下更改颜色时,之后的屏幕截图将不会立即更改颜色(因为它具有动画效果)。使用 performWithoutAnimation,后截图具有最终颜色,在后截图中被正确捕获。