// // 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 { QMUIAlertController *alertController = [QMUIAlertController alertControllerWithTitle:title message:message preferredStyle:QMUIAlertControllerStyleAlert]; /// 左边按钮 if(cancelTitle.length>0) { // UIAlertAction *cancel= [UIAlertAction actionWithTitle:cancelTitle?cancelTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { !cancelAction?:cancelAction(); }]; QMUIAlertAction *cancel = [QMUIAlertAction actionWithTitle:RQStringIsNotEmpty(cancelTitle)? cancelTitle : @"取消" style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) { !cancelAction?:cancelAction(); }]; [alertController addAction:cancel]; } if (confirmTitle.length>0) { // UIAlertAction *confirm = [UIAlertAction actionWithTitle:confirmTitle?confirmTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { !confirmAction?:confirmAction();}]; QMUIAlertAction *confirm = [QMUIAlertAction actionWithTitle:RQStringIsNotEmpty(confirmTitle)? confirmTitle : @"确定" style:QMUIAlertActionStyleDefault handler:^(__kindof QMUIAlertController * _Nonnull aAlertController, QMUIAlertAction * _Nonnull action) { !confirmAction?:confirmAction(); }]; [alertController addAction:confirm]; } dispatch_async(dispatch_get_main_queue(), ^{ // [[RQControllerHelper currentViewController] presentViewController:alertController animated:YES completion:NULL]; [alertController showWithAnimated:YES]; }); } #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