123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- //
- // NSObject+RQExtension.m
- // RQCommon
- //
- // Created by 张嵘 on 2018/11/21.
- // Copyright © 2018 张嵘. All rights reserved.
- //
- #import "NSObject+RQExtension.h"
- #import "RQControllerHelper.h"
- @implementation NSObject (RQExtension)
- #pragma mark - Other
- + (NSInteger) rq_randomNumberWithFrom:(NSInteger)from to:(NSInteger)to{
- return (NSInteger)(from + (arc4random() % (to - from + 1)));
- }
- - (void)rq_convertNotification:(NSNotification *)notification completion:(void (^ _Nullable)(CGFloat, UIViewAnimationOptions, CGFloat))completion
- {
- // 按钮
- NSDictionary *userInfo = notification.userInfo;
- // 最终尺寸
- CGRect endFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
- // 开始尺寸
- CGRect beginFrame = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
- // 动画时间
- CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
- /// options
- UIViewAnimationOptions options = ([userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue] << 16 ) | UIViewAnimationOptionBeginFromCurrentState;
-
- /// keyboard height
- CGFloat keyboardH = 0;
- if (beginFrame.origin.y == [[UIScreen mainScreen] bounds].size.height){
- // up
- keyboardH = endFrame.size.height;
- }else if (endFrame.origin.y == [[UIScreen mainScreen] bounds].size.height) {
- // down
- keyboardH = 0;
- }else{
- // up
- keyboardH = endFrame.size.height;
- }
- /// 回调
- !completion?:completion(duration,options,keyboardH);
- }
- #pragma mark - Get..
- /// Get class
- - (BOOL)rq_isStringClass { return [self isKindOfClass:[NSString class]]; }
- - (BOOL)rq_isNumberClass { return [self isKindOfClass:[NSNumber class]]; }
- - (BOOL)rq_isArrayClass { return [self isKindOfClass:[NSArray class]]; }
- - (BOOL)rq_isDictionaryClass { return [self isKindOfClass:[NSDictionary class]]; }
- - (BOOL)rq_isStringOrNumberClass { return [self rq_isStringClass] || [self rq_isNumberClass]; }
- - (BOOL)rq_isNullOrNil { return !self || [self isKindOfClass:[NSNull class]]; }
- - (BOOL)rq_isExist {
- if (self.rq_isNullOrNil) return NO;
- if (self.rq_isStringClass) return (self.rq_stringValueExtension.length>0);
- return YES;
- }
- /// Get value
- - (NSString *)rq_stringValueExtension{
- if ([self rq_isStringClass]) return [(NSString *)self length]? (NSString *)self: @"";
- if ([self rq_isNumberClass]) return [NSString stringWithFormat:@"%@", self];
- return @"";
- }
- #pragma mark - Alert
- + (void)rq_showAlertViewWithTitle:(NSString *)title message:(NSString *)message confirmTitle:(NSString *)confirmTitle {
-
- [self rq_showAlertViewWithTitle:title message:message confirmTitle:confirmTitle confirmAction:NULL];
- }
- + (void)rq_showAlertViewWithTitle:(NSString *)title message:(NSString *)message confirmTitle:(NSString *)confirmTitle confirmAction:(void(^)(void))confirmAction {
-
- [self rq_showAlertViewWithTitle:title message:message confirmTitle:confirmTitle cancelTitle:nil confirmAction:confirmAction cancelAction:NULL];
- }
- + (void)rq_showAlertViewWithTitle:(NSString *)title message:(NSString *)message confirmTitle:(NSString *)confirmTitle cancelTitle:(NSString *)cancelTitle confirmAction:(void(^)(void))confirmAction cancelAction:(void(^)(void))cancelAction {
-
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
- /// 配置alertController
- alertController.titleColor = RQ_MAIN_TEXT_COLOR_1;
- alertController.messageColor = RQ_MAIN_TEXT_COLOR_3;
-
- /// 左边按钮
- if(cancelTitle.length>0){
- UIAlertAction *cancel= [UIAlertAction actionWithTitle:cancelTitle?cancelTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { !cancelAction?:cancelAction(); }];
- cancel.textColor = RQ_MAIN_TEXT_COLOR_3;
- [alertController addAction:cancel];
- }
-
-
- if (confirmTitle.length>0) {
- UIAlertAction *confirm = [UIAlertAction actionWithTitle:confirmTitle?confirmTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { !confirmAction?:confirmAction();}];
- confirm.textColor = RQ_MAIN_TINTCOLOR;
- [alertController addAction:confirm];
- }
-
- dispatch_async(dispatch_get_main_queue(), ^{
- [[RQControllerHelper currentViewController] presentViewController:alertController animated:YES completion:NULL];
- });
- }
- #pragma mark - Safe
- + (void)methodSwizzlingWithOriginalSelector:(SEL)originalSelector bySwizzledSelector:(SEL)swizzledSelector{
- Class class = [self class];
- //原有方法
- Method originalMethod = class_getInstanceMethod(class, originalSelector);
- //替换原有方法的新方法
- Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
- //先尝试給源SEL添加IMP,这里是为了避免源SEL没有实现IMP的情况
- BOOL didAddMethod = class_addMethod(class,originalSelector,
- method_getImplementation(swizzledMethod),
- method_getTypeEncoding(swizzledMethod));
- if (didAddMethod) {//添加成功:说明源SEL没有实现IMP,将源SEL的IMP替换到交换SEL的IMP
- class_replaceMethod(class,swizzledSelector,
- method_getImplementation(originalMethod),
- method_getTypeEncoding(originalMethod));
- } else {//添加失败:说明源SEL已经有IMP,直接将两个SEL的IMP交换即可
- method_exchangeImplementations(originalMethod, swizzledMethod);
- }
- }
- @end
|