【问题标题】: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

【问题讨论】:

    标签: ios objective-c uiviewanimation caanimation


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      你设置了动画的delegate 吗?

      animation.delegate = self
      

      【讨论】:

      • 为什么要设置委托?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-25
      • 2011-12-11
      • 1970-01-01
      • 2010-12-08
      • 2014-07-01
      • 1970-01-01
      相关资源
      最近更新 更多