工作中的小工具

给按钮添加点击事件

*给UIcontrol加个类别
.h文件中

#import 

typedef void(^GCControlEventActionBlock)(UIControl* control, NSSet* touches);

/**
 *  This Extension is used for add UIControlEvents to the UIControls.
 */
@interface UIControl (GCEvent)

/**
 *  add actionBlock for paticular event. you can call this method mutiple times for a paticular event.
 *
 *  @param event    the particular event.
 *  @param action   action will be invoked when the particular event happen. |action| can't be nil.
 */
- (void)addControlEvents:(UIControlEvents)event action:(GCControlEventActionBlock)action;

/**
 *  remove all the actionBlocks for paticular event.
 *
 *  @param event    the particular event.
 */
- (void)removeAllControlEventsAction:(UIControlEvents)event;

#import "UIControl+GCEvent.h"

#import 


@interface UIControlEventBlockWrapper : NSObject

@property (nonatomic, weak) UIControl* control;
@property (nonatomic, assign) UIControlEvents events;
@property (nonatomic, copy) GCControlEventActionBlock eventActionBlock;

- (instancetype)initWithControl:(UIControl *)control
                  controlEvents:(UIControlEvents)event
               eventActionBlock:(GCControlEventActionBlock)eventActionBlock;

@end

@implementation UIControlEventBlockWrapper

- (instancetype)initWithControl:(UIControl *)control
                  controlEvents:(UIControlEvents)event
               eventActionBlock:(GCControlEventActionBlock)eventActionBlock {
    if (self = [self init]) {
        [control addTarget:self action:@selector(_executeActionBlockByControl:touches:) forControlEvents:event];
        _control = control;
        _events = event;
        _eventActionBlock = eventActionBlock;
    }
    return self;
}

- (void)_executeActionBlockByControl:(UIControl *)control touches:(id/*UITouchesEvent*/)touches {
    id allTouches = nil;
    if ([touches respondsToSelector:@selector(allTouches)]) {
        allTouches = [touches allTouches];
    }
    _eventActionBlock(control, allTouches);
}

- (void)dealloc {
    [_control removeTarget:self
                    action:@selector(_executeActionBlockByControl:touches:)
          forControlEvents:_events];
}

@end






@implementation UIControl (GCEvent)

- (void)addControlEvents:(UIControlEvents)event action:(GCControlEventActionBlock)action {
    NSParameterAssert(action);
    
    /**
     *  |wrapper| created by using class method will release delay at the end of the
     *  method which invoke the |removeAllControlEventsAction:|. why??
     */
    UIControlEventBlockWrapper* wrapper = [[UIControlEventBlockWrapper alloc]
                                           initWithControl:self
                                           controlEvents:event
                                           eventActionBlock:action];
    
    [[self _controlEventsBlockWrappersForControlEvents:event] addObject:wrapper];
}

- (void)removeAllControlEventsAction:(UIControlEvents)event {
    [[self _controlEventsBlockWrappersForControlEvents:event] removeAllObjects];
}


#pragma mark - instance private method
- (NSMutableArray *)_controlEventsBlockWrappersForControlEvents:(UIControlEvents)event {
    static char const wrappersDicKey;
    NSMutableDictionary* wrapperDic = objc_getAssociatedObject(self, &wrappersDicKey);
    if (!wrapperDic) {
        wrapperDic = [NSMutableDictionary dictionary];
        objc_setAssociatedObject(self, &wrappersDicKey, wrapperDic, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    NSMutableArray* wrapperArr = wrapperDic[@(event)];
    if (!wrapperArr) {
        wrapperArr = [NSMutableArray array];
        wrapperDic[@(event)] = wrapperArr;
    }
    
    return wrapperArr;
}


@end

获取应用当前正显示的ViewController

*给UIApplication添加类别
.h中

#import 

@interface UIApplication (Ext)

/**
 *  获取应用当前正显示的ViewController
 *
 *  @return ViewController
 */
- (UIViewController *)currentViewController;

@end

.m中

#import "UIApplication+Ext.h"

@implementation UIApplication (Ext)

+ (UIViewController*)_findBestViewController:(UIViewController*)vc {
    
    if (vc.presentedViewController) {
        
        // Return presented view controller
        return [self _findBestViewController:vc.presentedViewController];
        
    } else if ([vc isKindOfClass:[UISplitViewController class]]) {
        
        // Return right hand side
        UISplitViewController* svc = (UISplitViewController*) vc;
        if (svc.viewControllers.count > 0)
            return [self _findBestViewController:svc.viewControllers.lastObject];
        else
            return vc;
        
    } else if ([vc isKindOfClass:[UINavigationController class]]) {
        
        // Return top view
        UINavigationController* svc = (UINavigationController*) vc;
        if (svc.viewControllers.count > 0)
            return [self _findBestViewController:svc.topViewController];
        else
            return vc;
        
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        
        // Return visible view
        UITabBarController* svc = (UITabBarController*) vc;
        if (svc.viewControllers.count > 0)
            return [self _findBestViewController:svc.selectedViewController];
        else
            return vc;
        
    } else {
        
        // Unknown view controller type, return last child view controller
        return vc;
        
    }
    
}

- (UIViewController *)currentViewController {
    UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    return [UIApplication _findBestViewController:viewController];
}

@end

返回NavigationBar上的FixedSpace,用于缩短按钮间距


#import 

@interface UIBarButtonItem (FixedSpace)

/**
 *  返回NavigationBar上的FixedSpace,用于缩短按钮间距
 *
 *  @return
 */
+ (instancetype)createNavigationFixedItem;

@end

.m

#import "UIBarButtonItem+FixedSpace.h"

@implementation UIBarButtonItem (FixedSpace)

+ (instancetype)createNavigationFixedItem {
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    item.width = -10;
    return item;
}
@end

对图片的处理

.h

#import 

@interface UIImage (AdjustmentSize)

/**
 *  调整图片尺寸到新尺寸,不保持比例
 *
 *  @param newSize 新尺寸
 *
 *  @return 调整后的图片
 */
- (UIImage *)adjustmentWithNewSize:(CGSize)newSize;

/**
 *  调整图片尺寸到指定大小范围内,保存比例(大了会缩小,小的不变)
 *
 *  @param maxSize 宽或高最大尺寸
 *
 *  @return 调整后的图片
 */
- (UIImage *)adjustedToMaxSize:(NSInteger)maxSize;

/**
 *  调整图片尺寸到当前工程标准大小
 *
 *  @return 调整后的图片
 */
- (UIImage *)adjustedToStandardSize;

@end

.m

#import "UIImage+AdjustmentSize.h"

@implementation UIImage (AdjustmentSize)

- (UIImage *)adjustmentWithNewSize:(CGSize)newSize {
    UIGraphicsBeginImageContext(newSize);
    [self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

- (UIImage *)adjustedToMaxSize:(NSInteger)maxSize {
    if (self.size.width >= self.size.height && self.size.width > maxSize) {
        CGSize newSize;
        newSize.width = maxSize;
        newSize.height = self.size.height * (maxSize / self.size.width);
        return [self adjustmentWithNewSize:newSize];
    }
    else if (self.size.height > maxSize) {
        CGSize newSize;
        newSize.height = maxSize;
        newSize.width = self.size.width * (maxSize / self.size.height);
        return [self adjustmentWithNewSize:newSize];
    }
    else {
        return self;
    }
}

- (UIImage *)adjustedToStandardSize {
    return [self adjustedToMaxSize:1920];
}

@end

你可能感兴趣的:(工作中的小工具)