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

时间:2013-08-16 09:36:06

标签: ios objective-c uiviewanimation caanimation

由于UIView动画由CAAnimation支持,所以我猜,当id为UIView animateWithDuration:animations时,将触发CALayer的addAnimation:forKey:。所以我做了一个方法调整并输出一些信息。

#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

2 个答案:

答案 0 :(得分:1)

假设它被视为图层上的隐式动画,在这种情况下,你会混淆actionForKey:

答案 1 :(得分:0)

你设置动画的delegate吗?

animation.delegate = self