cdts/xdts-ios 3/TreeHole/CYHResetCode/CYH/QMUIKit/UIKitExtensions/UISwitch+QMUI.m

82 lines
3.9 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.
*/
//
// UISwitch+QMUI.m
// QMUIKit
//
// Created by MoLice on 2019/7/12.
//
#import "UISwitch+QMUI.h"
#import "QMUICore.h"
@implementation UISwitch (QMUI)
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
ExtendImplementationOfNonVoidMethodWithSingleArgument([UISwitch class], @selector(initWithFrame:), CGRect, UISwitch *, ^UISwitch *(UISwitch *selfObject, CGRect firstArgv, UISwitch *originReturnValue) {
if (QMUICMIActivated) {
if (SwitchOffTintColor) {
selfObject.qmui_offTintColor = SwitchOffTintColor;
}
}
return originReturnValue;
});
// qmui_offTintColor UISwitch switchWellView backgroundColor switchWellView switchWellView
OverrideImplementation([UISwitch class], @selector(traitCollectionDidChange:), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP (^originalIMPProvider)(void)) {
return ^(UISwitch *selfObject, UITraitCollection *previousTraitCollection) {
// call super
void (*originSelectorIMP)(id, SEL, UITraitCollection *);
originSelectorIMP = (void (*)(id, SEL, UITraitCollection *))originalIMPProvider();
originSelectorIMP(selfObject, originCMD, previousTraitCollection);
BOOL interfaceStyleChanged = [previousTraitCollection hasDifferentColorAppearanceComparedToTraitCollection:selfObject.traitCollection];
if (interfaceStyleChanged) {
// iOS 13 Dark/Light Mode switchWellView runloop traitCollectionDidChange _traitCollectionDidChangeInternal
dispatch_async(dispatch_get_main_queue(), ^{
[selfObject qmui_applyOffTintColorIfNeeded];
});
}
};
});
});
}
static char kAssociatedObjectKey_offTintColor;
static NSString * const kDefaultOffTintColorKey = @"defaultOffTintColorKey";
- (void)setQmui_offTintColor:(UIColor *)qmui_offTintColor {
UIView *switchWellView = [self valueForKeyPath:@"_visualElement._switchWellView"];
UIColor *defaultOffTintColor = [switchWellView qmui_getBoundObjectForKey:kDefaultOffTintColorKey];
if (!defaultOffTintColor) {
defaultOffTintColor = switchWellView.backgroundColor;
[switchWellView qmui_bindObject:defaultOffTintColor forKey:kDefaultOffTintColorKey];
}
// offTintColor nil setOnTintColor
switchWellView.backgroundColor = qmui_offTintColor ? : defaultOffTintColor;
objc_setAssociatedObject(self, &kAssociatedObjectKey_offTintColor, qmui_offTintColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIColor *)qmui_offTintColor {
return objc_getAssociatedObject(self, &kAssociatedObjectKey_offTintColor);
}
- (void)qmui_applyOffTintColorIfNeeded {
if (self.qmui_offTintColor) {
self.qmui_offTintColor = self.qmui_offTintColor;
}
}
@end