iOS开发中UI控件扩展的工具类(连载……)

整理收集一下常用的UI控件的扩展类!
首先是View的扩展:包含:framelayer获取控制器

#import 

NS_ASSUME_NONNULL_BEGIN

@interface UIView (Utils)

/**
  *  x
  */
@property (nonatomic,assign) CGFloat x;
/**
  *  y
  */
@property (nonatomic,assign) CGFloat y;
/**
  *  centerX   中心点X
  */
@property (nonatomic,assign) CGFloat centerX;
/**
  *  centerY   中心点Y
  */
@property (nonatomic,assign) CGFloat centerY;
/**
  *  width   宽度
  */
@property (nonatomic,assign) CGFloat width;
/**
  *  height   长度
  */
@property (nonatomic,assign) CGFloat height;
/**
  *  size  大小
  */
@property (nonatomic,assign) CGSize size;


/**
  *  添加阴影效果
  *  color   颜色
  *  radius  范围
  *  opacity 透明度
  */
-(void)viewDrawLayerShadowWithColor:(UIColor*)color Radius:(float)radius Opacity:(float) opacity;
/**
  *     添加边框
  */
-(void)viewDrawLayerBorderWithColor:(UIColor*)color BorderWidth:(CGFloat)width ;
/**
  *      设置圆角
  */
-(void)viewDrawLayerCornerWithRadius:(CGFloat)radius;





#warning -(void)viewDidAppear:(BOOL)animated 之后使用

/**
  *  获取当前正在显示的控制器(由于keyWindow的不同可能获取不正确)
  *
  *  @return 正在显示的控制器
  */
+ (UIViewController *)currentViewController;
/**
  *  获取当前正在显示的控制器(无论keyWindow是什么)
  *
  *  @return 正在显示的控制器
  */
+ (UIViewController *)getCurrentViewConrtrollerIgnoreWindowLevel;

/**
  *  获取当前显示的View的控制器的根控制器
  *
  *  @return 根控制器
  */
+ (UIViewController *)getCurrentRootViewController;



@end

NS_ASSUME_NONNULL_END

.m文件


#import "UIView+Utils.h"

@implementation UIView (Utils)

#pragma mark - 获取父级控制器
- (UIViewController *)parentController {
//     获取响应者
    UIResponder *responder = [self nextResponder];
//    递归找到响应者
    while (responder) {
        if ([responder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)responder;
        }
        responder = [responder nextResponder];
    }
    
    return nil;
}


#pragma mark - 获取当前控制器 -  由于keyWindow的不同可能获取不正确
+ (UIViewController *)currentViewController {
    
    //如果是在AlertView上可能获取的keyWindow不是UIWindow(注意)
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;

    //         取到第一层时,取到的是UITransitionView,通过这个view拿不到控制器
    UIView *firstView = [keyWindow.subviews firstObject];
    UIView *secondView = [firstView.subviews firstObject];
    
    UIViewController *vc = secondView.parentController;
    
    if ([vc isKindOfClass:[UITabBarController class]]) {
        
        UITabBarController *tab = (UITabBarController *)vc;
        
        
        if ([tab.selectedViewController isKindOfClass:[UINavigationController class]]) {
            
            UINavigationController *nav = (UINavigationController *)tab.selectedViewController;
            
            return [nav.viewControllers lastObject];
            
        } else {
            
            return tab.selectedViewController;
        }
        
    }else if ([vc isKindOfClass:[UINavigationController class]]) {
        
        UINavigationController *nav = (UINavigationController *)vc;
        
        return [nav.viewControllers lastObject];
        
    } else {
        
        return vc;
    }
    
    
    return nil;
    
}

#pragma mark - 获取当前显示的控制器
+(UIViewController *)getCurrentViewConrtrollerIgnoreWindowLevel {

//    获取到最顶层window
    UIWindow *topWindow = [[UIApplication sharedApplication] keyWindow];
    
    
    if (topWindow.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(topWindow in windows){
            
            if (topWindow.windowLevel == UIWindowLevelNormal)
                break;
            
        }
    }
    
    //         取到第一层时,取到的是UITransitionView,通过这个view拿不到控制器
    UIView *firstView = [topWindow.subviews firstObject];
    UIView *secondView = [firstView.subviews firstObject];
    
//     获取其父级控制器
    UIViewController *vc = secondView.parentController;
    if ([vc isKindOfClass:[UITabBarController class]])
    {
        UITabBarController *tab = (UITabBarController *)vc;
        if ([tab.selectedViewController isKindOfClass:[UINavigationController class]])
        {
            UINavigationController *nav = (UINavigationController *)tab.selectedViewController;
            return [nav.viewControllers lastObject];
            
        } else {
            return tab.selectedViewController;
            
        }
        
    }else if ([vc isKindOfClass:[UINavigationController class]]) {
        
        UINavigationController *nav = (UINavigationController *)vc;
        return [nav.viewControllers lastObject];
        
    }else {
        return vc;
    }
    
    return nil;
    
}


#pragma mark -  获取当前显示的View的控制器的根控制器
+ (UIViewController *)getCurrentRootViewController {
    
    UIViewController *result;
    
    //         Try to find the root view controller programmically
    //
    //         Find the top window (that is not an alert view or other window)
    
    UIWindow *topWindow = [[UIApplication sharedApplication] keyWindow];
    
    if (topWindow.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        
        for(topWindow in windows) {
            if (topWindow.windowLevel == UIWindowLevelNormal)
                break;
        }
    }
    
    UIView *rootView = [[topWindow subviews] objectAtIndex:0];
    id nextResponder = [rootView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]]) {
        
        result = nextResponder;
        
    } else if ([topWindow respondsToSelector:@selector(rootViewController)] && topWindow.rootViewController !=nil) {
        
        result = topWindow.rootViewController;
        
    }else {
        NSAssert(NO,@"ShareKit: Could not find a root view controller. You can assign one manually by calling [[SHK currentHelper] setRootViewController:YOURROOTVIEWCONTROLLER].");
        
        
        NSLog(@"未能找到相应的控制器!");
        
    }
    
    return result;
}


#pragma mark - UIView +Layer
/****------> UIView +Layer <------ ****/
-(void)viewDrawLayerShadowWithColor:(UIColor*)color Radius:(float)radius Opacity:(float) opacity
{
    self.layer.shadowOpacity = opacity;//阴影透明度
    
    self.layer.shadowColor = color.CGColor;//阴影的颜色
    
    self.layer.shadowRadius = radius;//阴影扩散的范围控制
    // 这里可扩展 : 设置 阴影的方向和范围
    self.layer.shadowOffset = CGSizeMake(1,1);//阴影的范围
}

-(void)viewDrawLayerBorderWithColor:(UIColor *)color BorderWidth:(CGFloat)width
{
    self.layer.borderColor = color.CGColor;//边框颜色
    
    self.layer.borderWidth = width;//边框宽度
}

-(void)viewDrawLayerCornerWithRadius:(CGFloat)radius
{
    self.layer.masksToBounds =YES;
    self.layer.cornerRadius = radius;

}


#pragma mark - UIView +Frame
/****------> UIView +Frame <------ ****/
- (void)setX:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (CGFloat)x
{
    return self.frame.origin.x;
}

- (void)setY:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)y
{
    return self.frame.origin.y;
}

- (void)setCenterX:(CGFloat)centerX
{
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}

- (CGFloat)centerX
{
    return self.center.x;
}

- (void)setCenterY:(CGFloat)centerY
{
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
    
}

- (CGFloat)centerY
{
    return self.center.y;
}

- (void)setWidth:(CGFloat)width
{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (CGFloat)width
{
    return self.frame.size.width;
}

- (void)setHeight:(CGFloat)height
{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)height
{
    return self.frame.size.height;
}

- (void)setSize:(CGSize)size
{
    //    self.width = size.width;
    //    self.height = size.height;
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (CGSize)size
{
    return self.frame.size;
}


@end

你可能感兴趣的:(iOS开发中UI控件扩展的工具类(连载……))