iOS小工具-给UIView加菊花

作者: 邓龙 | 来源:发表于2016-04-13 13:34 被阅读1578次

    ps: 方便使用,木有高深技术

    1.效果

    菊花残.PNG

    2.使用方式

    在layoutsubview方法中

    - (void)layoutSubviews{
        [super layoutSubviews];
        [self layoutIfNeeded];
        if (!self.appModel) {
            [self.tableView appendActivityView:[UIColor lightGrayColor]];
        }else{
            [self.tableView removeActivityView];
        }
    }
    

    3.具体代码

    #import <UIKit/UIKit.h>
    @interface UIView (activityView)
    - (void)appendActivityView:(UIColor *)color;
    - (void)removeActivityView;
    @property (nonatomic,strong) UIActivityIndicatorView *appendActivity;/**< 附加菊花图 */
    @end
    
    #import "UIView+activityView.h"
    #import <objc/runtime.h>
    static char activityViewKey;
    
    @implementation UIView (activityView)
    - (void)appendActivityView:(UIColor *)color{
        //1.添加菊花
        if (!self.appendActivity) {
            UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
            activityIndicator.frame = self.bounds;
            activityIndicator.color = color;
            [activityIndicator startAnimating];
            [activityIndicator setHidesWhenStopped:YES];
            
            self.appendActivity = activityIndicator;
            
            //隐藏其它子视图
            for (UIView *view in self.subviews) {
                view.hidden = YES;
            }
            [self addSubview:activityIndicator];
        }
    
        [self bringSubviewToFront:self.appendActivity];
        
        //2.如果是scrollView,则在显示菊花时禁止滑动
        if ([self isKindOfClass:[UIScrollView class]]) {
            UIScrollView *scrollView = (UIScrollView *)self;
            scrollView.scrollEnabled = NO;
        }
    }
    
    - (void)removeActivityView{
        //1.恢复滑动
        if ([self isKindOfClass:[UIScrollView class]]) {
            UIScrollView *scrollView = (UIScrollView *)self;
            scrollView.scrollEnabled = YES;
        }
        
        for (UIView *view in self.subviews) {
            view.hidden = NO;
        }
        
        //2.去掉菊花
        if (self.appendActivity) {
            self.appendActivity.hidden = YES;
            [self.appendActivity stopAnimating]; // 结束旋转
            [self.appendActivity removeFromSuperview];
        }
    }
    
    
    #pragma mark - 运行时添加属性
    - (UIActivityIndicatorView *)appendActivity{
        return objc_getAssociatedObject(self, &activityViewKey);
    }
    
    - (void)setAppendActivity:(UIActivityIndicatorView *)appendActivity{
        objc_setAssociatedObject(self, &activityViewKey, appendActivity, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    @end
    

    相关文章

      网友评论

      • 宋鸿康iOS:[self bringSubviewToFront:self.appendActivity];
        这句代码失效了把。。 博主可以测试测试
        宋鸿康iOS:可以监听addView这个方法,最有一次addView ,然后执行[self bringSubviewToFront:self.appendActivity];
      • Salad可乐:activityViewKey 是什么?
      • 1b2ae550dc99:运行时不是很懂, 运行时添加属性的目的是什么?
        3be1d8f070d6:@行进的NSLog 分类里用的吧,分类里不能直接加属性,就用对象关键

      本文标题:iOS小工具-给UIView加菊花

      本文链接:https://www.haomeiwen.com/subject/lbgwlttx.html