cdts/xdts-ios 3/TreeHole/CYHResetCode/CYH/QMUIKit/QMUIComponents/CALayer+QMUIViewAnimation.m

99 lines
4.4 KiB
Mathematica
Raw Permalink Normal View History

2023-07-27 09:20:00 +08:00
/**
* Tencent is pleased to support the open source community by making QMUI_iOS available.
* Copyright (C) 2016-2021 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
//
// CALayer+QMUIViewAnimation.m
// QMUIKit
//
// Created by ziezheng on 2020/4/4.
//
#import "CALayer+QMUIViewAnimation.h"
#import "CALayer+QMUI.h"
#import "QMUICore.h"
#import "QMUIMultipleDelegates.h"
@interface _QMUICALayerDelegator : NSObject <CALayerDelegate>
@end
@implementation _QMUICALayerDelegator
+ (instancetype)sharedDelegator {
static dispatch_once_t onceToken;
static _QMUICALayerDelegator *instance = nil;
dispatch_once(&onceToken,^{
instance = [[super allocWithZone:NULL] init];
});
return instance;
}
+ (id)allocWithZone:(struct _NSZone *)zone {
return [self sharedDelegator];
}
- (id<CAAction>)actionForLayer:(CALayer *)layer forKey:(NSString *)event {
static UIView *standardView = nil;
if (!standardView) standardView = UIView.new;
// +[UIView animateWithDuration:animations:] UIView actionForLayer:forKey: CAAction
id<CAAction> action = [standardView actionForLayer:standardView.layer forKey:event];
if (action == [NSNull null]) {
// -[CALayer actionForKey:] NSNull self.actions CALayer nil -[CALayer actionForKey:]
return nil;
} else {
return action;
}
}
@end
@implementation CALayer (QMUIViewAnimation)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
OverrideImplementation([CALayer class], @selector(addAnimation:forKey:), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP (^originalIMPProvider)(void)) {
return ^(CALayer *selfObject, CAAnimation *animation, NSString *key) {
if (selfObject.qmui_viewAnimationEnabled) {
BOOL isViewAnimtion = [animation isKindOfClass:CABasicAnimation.class] && [animation.delegate isKindOfClass:NSClassFromString(@"UIViewAnimationState")];
if (isViewAnimtion) {
// fromValue toValue CAMediaTimingCopyRenderTiming animtion CATransaction Layer
((CABasicAnimation *)animation).fromValue = nil;
((CABasicAnimation *)animation).toValue = nil;
// toValue additive NO
((CABasicAnimation *)animation).additive = NO;
}
}
void (*originSelectorIMP)(id, SEL, CAAnimation *, NSString *);
originSelectorIMP = (void (*)(id, SEL, CAAnimation *, NSString *))originalIMPProvider();
originSelectorIMP(selfObject, originCMD, animation, key);
};
});
});
}
static char kAssociatedObjectKey_qmuiviewAnimationEnabled;
- (void)setQmui_viewAnimationEnabled:(BOOL)qmui_viewAnimationEnabled {
QMUIAssert(!self.qmui_isRootLayerOfView, @"CALayer (QMUIViewAnimation)", @"UIView 本身的 Layer 无须开启 %s", __func__);
objc_setAssociatedObject(self, &kAssociatedObjectKey_qmuiviewAnimationEnabled, @(qmui_viewAnimationEnabled), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
if (qmui_viewAnimationEnabled) {
self.qmui_multipleDelegatesEnabled = YES;
self.delegate = [_QMUICALayerDelegator sharedDelegator];
} else {
[self qmui_removeDelegate:[_QMUICALayerDelegator sharedDelegator]];
}
}
- (BOOL)qmui_viewAnimationEnabled {
return [((NSNumber *)objc_getAssociatedObject(self, &kAssociatedObjectKey_qmuiviewAnimationEnabled)) boolValue];
}
@end