123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- //
- // UIView+Category.m
- // 乐销
- //
- // Created by 刘惠萍 on 2017/5/29.
- // Copyright © 2017年 ping. All rights reserved.
- //
- #import "UIView+Category.h"
- @implementation UIView (Category)
- //get
- - (BOOL)isEdited{
- if ([self isKindOfClass:[UITextField class]]||[self isKindOfClass:[UITextView class]]) {
- UITextView * text = (UITextView *)self;
- if (self.userInteractionEnabled && isStr(text.text)) {
- if ([text.text intValue] != 1) {
- return true;
- }
- }
- }
- for (UIView * viewSub in self.subviews) {
- if (viewSub.isEdited) {
- return true;
- }
- }
- return false;
- }
- //fist responder
- - (UIView *)fetchFirstResponder
- {
- if (self.isFirstResponder) {
- return self;
- }
- for (UIView *subView in self.subviews) {
- UIView *firstResponder = [subView fetchFirstResponder];
- if (firstResponder != nil) {
- return firstResponder;
- }
- }
- return nil;
- }
- //获取所在vc
- - (UIViewController *)fetchVC{
- UIView * view = self;
- while (view) {
- UIResponder* nextResponder = [view nextResponder];
- if (nextResponder && [nextResponder isKindOfClass:[UIViewController class]]) {
- return (UIViewController*)nextResponder;
- }
- view = view.superview;
- }
- return nil;
- }
- //移除全部
- - (void)removeAllSubViews{
- while (self.subviews.count) {
- [self.subviews.lastObject removeFromSuperview];
- }
- }
- //增加顶部高度
- - (void)addSubViewsTopHeight:(CGFloat)height{
- for (UIView * view in self.subviews) {
- view.top += height;
- }
- self.height += height;
- }
- // 移除视图
- - (void)removeSubViewWithTag:(NSInteger)tag{
- if (self == nil) return;
- NSArray * aryView = self.subviews;
- for (UIView * viewSub in aryView) {
- if (viewSub.tag == tag) {
- [viewSub removeFromSuperview];
- }
- }
- }
- //获取子视图 根据tag
- - (NSMutableArray *)fetchSubViewsWithTag:(NSInteger)tag{
- NSMutableArray * aryReturn = [NSMutableArray array];
- for (UIView * subView in self.subviews) {
- if (subView.tag == tag) {
- [aryReturn addObject:subView];
- }
- }
- return aryReturn;
- }
- //添加线
- - (CGFloat)addLineFrame:(CGRect)rect{
- return [self addLineFrame:rect color:COLOR_LINE];
- }
- - (CGFloat)addLineFrame:(CGRect)rect tag:(NSInteger)tag{
- return [self addLineFrame:rect color:COLOR_LINE tag:tag];
- }
- - (CGFloat)addLineFrame:(CGRect)rect color:(UIColor *)color{
- return [self addLineFrame:rect color:color tag:TAG_LINE];
- }
- - (CGFloat)addLineFrame:(CGRect)rect color:(UIColor *)color tag:(NSInteger)tag{
- UIView * viewLine = [UIView lineWithFrame:rect color:color];
- viewLine.tag = tag;
- [self addSubview:viewLine];
- return viewLine.bottom;
- }
- - (CGFloat)addLineWithHeight:(CGFloat)height{
- return [self addLineFrame:CGRectMake(0, 0, self.width, height) color:COLOR_LINE];
- }
- //获取线视图
- + (UIView *)lineWithFrame:(CGRect)rect color:(UIColor *)color{
- UIView * viewLine = [[UIView alloc]initWithFrame:rect];
- viewLine.backgroundColor = color;
- viewLine.tag = TAG_LINE;
- return viewLine;
- }
- + (UIView *)lineWithHeight:(CGFloat)height{
- return [self lineWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, height) color:COLOR_LINE];
- }
- + (UIView *)lineWithHeight:(CGFloat)height backGroundColor:(UIColor *)color{
- return [self lineWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, height) color:color];
- }
- /**
- 增加点击事件
-
- @param target 目标
- @param action 点击事件
- */
- - (void)addTarget:(id)target action:(SEL)action{
- if(target){
- self.userInteractionEnabled = YES;
- UITapGestureRecognizer *tapClick=[[UITapGestureRecognizer alloc]initWithTarget:target action:action];
- tapClick.delegate = target;
- [self addGestureRecognizer:tapClick];
- }
- }
- - (void)addKeyboardHideGesture{
- UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGestureHideKeyboardClick)];
- tap.cancelsTouchesInView = NO;
- tap.delegate = self;
- [self addGestureRecognizer:tap];
- }
- - (void)tapGestureHideKeyboardClick{
- [GlobalMethod hideKeyboard];
- }
- #pragma mark gesture delegate
- - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
- if ([touch.view isKindOfClass:[UIControl class]]) {
- return false;
- }if ([touch.view isKindOfClass:[UICollectionView class]]){
- return false;
- }
- return true;
- }
- - (void)addShadowToView:(UIView *)theView withColor:(UIColor *)theColor {
- theView.layer.cornerRadius = 6;
- // 阴影颜色
- theView.layer.shadowColor = theColor.CGColor;
- // 阴影偏移,默认(0, -3)
- theView.layer.shadowOffset = CGSizeMake(0,1);
- // 阴影透明度,默认0
- theView.layer.shadowOpacity = 1;
- // 阴影半径,默认3
- theView.layer.shadowRadius = 10;
-
- }
- @end
|