NKo*_*kov 3 animation core-animation objective-c uiviewanimation ios
我已经设置了一个简单的项目来试验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
Run Code Online (Sandbox Code Playgroud)
边注.有趣的是,我发现当设置动画选项时UIViewAnimationOptionTransitionCrossDissolve它会做同样的事情,但是!如果将其设置为0,则会在持续时间内为color属性设置动画.非常肯定在这种情况下我们会看到隐式图层动画

在performWithoutAnimation块中包装颜色更改行.
为什么我猜这个工作是转换在转换块之前捕获屏幕截图并且在它运行之后捕获屏幕截图.当你在没有performWithoutAnimation的情况下改变颜色时,后截屏将不会立即改变颜色(导致它动画).使用performWithoutAnimation,后截屏具有最终颜色,可在截屏后正确捕获.
| 归档时间: |
|
| 查看次数: |
706 次 |
| 最近记录: |