2013-08-16 45 views
0

由于UIView动画由CAAnimation支持,所以我猜,当id做UIView animateWithDuration:animations时,CALayer的addAnimation:forKey:将被触发。所以我做了一个方法swizzling并输出一些信息。为什么UIView动画不会触发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 

回答

1

假设,它的被视为在这种情况下,你会调酒actionForKey:代替层上的隐式动画。

0

具有u设定动画的delegate

animation.delegate = self 
+0

为什么要设置代表? – limboy

相关问题