来自 电脑系统 2019-09-11 14:01 的文章
当前位置: 金沙澳门官网网址 > 电脑系统 > 正文

iOS小工具-给UIView加菊花

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

图片 1菊花残.PNG

在layoutsubview方法中

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

#import <UIKit/UIKit.h>@interface UIView (activityView)- appendActivityView:(UIColor *)color;- removeActivityView;@property (nonatomic,strong) UIActivityIndicatorView *appendActivity;/**< 附加菊花图 */@end

#import "UIView+activityView.h"#import <objc/runtime.h>static char activityViewKey;@implementation UIView (activityView)- 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; }}- 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);}- setAppendActivity:(UIActivityIndicatorView *)appendActivity{ objc_setAssociatedObject(self, &activityViewKey, appendActivity, OBJC_ASSOCIATION_RETAIN_NONATOMIC);}@end

本文由金沙澳门官网网址发布于电脑系统,转载请注明出处:iOS小工具-给UIView加菊花

关键词: