iOS收藏首页投稿(暂停使用,暂停投稿)iOS开发资料收集区

iOS小工具-给UIView加菊花

2016-04-13  本文已影响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
上一篇下一篇

猜你喜欢

热点阅读