为啥 UIView Animation 不会触发 CALayer 的 addAnimation:forKey:?

Posted

技术标签:

【中文标题】为啥 UIView Animation 不会触发 CALayer 的 addAnimation:forKey:?【英文标题】:Why UIView Animation doesn't trigger CALayer's addAnimation:forKey:?为什么 UIView Animation 不会触发 CALayer 的 addAnimation:forKey:? 【发布时间】:2013-08-16 09:36:06 【问题描述】:

由于 UIView Animation 是由 CAAnimation 支持的,所以我猜,当 id do UIView animateWithDuration:animations 时,会触发 CALayer 的 addAnimation:forKey:。所以我做了一个方法 swizzling 并在其中输出一些信息。

#import "CALayer+Swizzling.h"
#import <objc/runtime.h>

@implementation CALayer (Swizzling)

+ (void)load

    method_exchangeImplementations(class_getInstanceMethod([CALayer class], @selector(addAnimation:forKey:)), class_getInstanceMethod([CALayer class], @selector(hackedAddAnimation:forKey:)));


- (void)hackedAddAnimation:(CAAnimation *)animation forKey:(NSString *)key

    [self hackedAddAnimation:animation forKey:key];
    if ([animation isKindOfClass:[CABasicAnimation class]]) 
        CABasicAnimation *basicAnimation = (CABasicAnimation *)animation;
        if ([basicAnimation.keyPath isEqualToString:@"transform"]) 
            CATransform3D transform = [basicAnimation.fromValue CATransform3DValue];
            if (basicAnimation.fromValue) 
                NSLog(@"fromValue:%@", NSStringFromCGAffineTransform(CATransform3DGetAffineTransform(transform)));
            
            if (basicAnimation.toValue) 
                NSLog(@"toValue:%@", NSStringFromCGAffineTransform(CATransform3DGetAffineTransform(transform)));
            
            if (basicAnimation.byValue) 
                NSLog(@"byValue:%@", NSStringFromCGAffineTransform(CATransform3DGetAffineTransform(transform)));
            
            NSLog(@"timingFunction:%@", basicAnimation.timingFunction);
            NSLog(@"duration:%f", basicAnimation.duration);
        
    

@end

什么也没发生。但如果我显示UIAlertView,它就会被触发。

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"title" message:@"" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"", nil];

[alertView show];

这是结果:

2013-08-16 17:28:39.693 Test[81840:c07] fromValue:[0.01, 0, 0, 0.01, 0, 0]
2013-08-16 17:28:39.694 Test[81840:c07] timingFunction:easeInEaseOut
2013-08-16 17:28:39.695 Test[81840:c07] duration:0.200000
2013-08-16 17:28:39.897 Test[81840:c07] fromValue:[1.1, 0, 0, 1.1, 0, 0]
2013-08-16 17:28:39.897 Test[81840:c07] timingFunction:easeInEaseOut
2013-08-16 17:28:39.899 Test[81840:c07] duration:0.100000
2013-08-16 17:28:40.000 Test[81840:c07] fromValue:[0.9, 0, 0, 0.9, 0, 0]
2013-08-16 17:28:40.000 Test[81840:c07] timingFunction:easeInEaseOut
2013-08-16 17:28:40.001 Test[81840:c07] duration:0.100000

【问题讨论】:

【参考方案1】:

假设它被视为图层上的隐式动画,在这种情况下,您将改用 actionForKey:

【讨论】:

【参考方案2】:

你设置了动画的delegate 吗?

animation.delegate = self

【讨论】:

为什么要设置委托?

以上是关于为啥 UIView Animation 不会触发 CALayer 的 addAnimation:forKey:?的主要内容,如果未能解决你的问题,请参考以下文章

dispatch_after 和 [UIView animation: duration] 立即发生(但不应该)

Swift Animation Enclosure 不会设置动画

UIView重新渲染布局时,不会触发draw函数

为啥移动 UIView 不会在同一个视图上移动 UIImageView?

UIView Animation 中的 Alpha 变化没有影响,在 UIView Animation 之外很好

为啥 swift uiview 不会连接到视图控制器?