你可以用我的解決方案:
UIViewsIndicatorsPull.h
#import <Foundation/Foundation.h>
@interface UIViewsIndicatorContainer : NSObject
@property (nonatomic, weak) UIView *view;
@property (nonatomic, strong)UIActivityIndicatorView *indicator;
@end
@interface UIViewsIndicatorsPull : NSObject
+ (instancetype)instance;
- (UIActivityIndicatorView*)indicatorForViewObject:(UIView*)object;
- (UIActivityIndicatorView*)indicatorForViewObject:(UIView*)object style:(UIActivityIndicatorViewStyle)style;
- (void)removeIndicatorForViewObject:(UIView*)object;
@end
UIViewsIndicatorsPull.m
#import "UIViewsIndicatorsPull.h"
@implementation UIViewsIndicatorsPull
{
NSMutableArray *indicators;
}
+ (instancetype)instance
{
static UIViewsIndicatorsPull *sharedInstance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [UIViewsIndicatorsPull new];
});
return sharedInstance;
}
- (UIActivityIndicatorView*)indicatorForViewObject:(UIView*)object
{
return [self indicatorForViewObject:object style:UIActivityIndicatorViewStyleGray];
}
- (UIActivityIndicatorView*)indicatorForViewObject:(UIView*)object style:(UIActivityIndicatorViewStyle)style
{
if (indicators == nil) {
indicators = [NSMutableArray array];
}
UIActivityIndicatorView *result;
for (UIViewsIndicatorContainer *entity in indicators){
if (entity.view == object) {
result = (UIActivityIndicatorView*)entity.indicator;
break;
}
}
if (result == nil) {
result = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:style];
[object addSubview:result];
UIViewsIndicatorContainer *entity = [[UIViewsIndicatorContainer alloc] init];
entity.view = object;
entity.indicator = (UIActivityIndicatorView*)result;
[indicators addObject:entity];
}
return result;
}
- (void)removeIndicatorForViewObject:(UIView*)object
{
for (UIViewsIndicatorContainer *entity in indicators){
if (entity.view == object) {
UIActivityIndicatorView *indicator = entity.indicator;
[indicator stopAnimating];
[indicator removeFromSuperview];
[indicators removeObject:entity];
break;
}
}
}
@end
@implementation UIViewsIndicatorContainer
@end
的UIView + Additions.h
#import <UIKit/UIKit.h>
#import "UIViewsIndicatorsPull.h"
#define UIViewAutoresizingAll UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth
@interface UIView (Additions)
//process indicator
- (void)showProcessIndicator;
- (void)showProcessIndicatorWithStyle:(UIActivityIndicatorViewStyle)style;
- (void)showProcessIndicatorWithAlignment:(UIViewContentMode)mode;
- (void)showProcessIndicatorWithAlignment:(UIViewContentMode)mode style:(UIActivityIndicatorViewStyle)style;
- (void)hideProcessIndicator;
- (BOOL)isProcessIndicatorPresented;
@end
的UIView + Additions.m
#import "UIView+Additions.h"
@implementation UIView (UIView_Additions)
- (void)showProcessIndicatorWithStyle:(UIActivityIndicatorViewStyle)style
{
[self showProcessIndicatorWithAlignment:UIViewContentModeCenter style:style];
}
- (void)showProcessIndicator
{
[self showProcessIndicatorWithAlignment:UIViewContentModeCenter style:UIActivityIndicatorViewStyleGray];
}
- (void)showProcessIndicatorWithAlignment:(UIViewContentMode)mode style:(UIActivityIndicatorViewStyle)style
{
UIActivityIndicatorView *indicator = (UIActivityIndicatorView*)[[UIViewsIndicatorsPull instance] indicatorForViewObject:self style:style];
[self positionIndicator:indicator andAlignment:mode];
[indicator startAnimating];
[self.superview addSubview:indicator];
self.alpha = 0;
}
- (void)showProcessIndicatorWithAlignment:(UIViewContentMode)mode
{
[self showProcessIndicatorWithAlignment:mode style:UIActivityIndicatorViewStyleGray];
}
- (void)positionIndicator:(UIActivityIndicatorView*)indicator andAlignment:(UIViewContentMode)mode
{
CGRect frame = indicator.frame;
switch (mode) {
case UIViewContentModeRight:
frame.origin.x = self.frame.origin.x + self.frame.size.width - frame.size.width;
frame.origin.y = self.frame.origin.y + self.frame.size.height/2 - frame.size.height/2;
indicator.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
break;
case UIViewContentModeLeft:
frame.origin.x = self.frame.origin.x;
frame.origin.y = self.frame.origin.y + self.frame.size.height/2 - frame.size.height/2;
indicator.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
break;
case UIViewContentModeTop:
frame.origin.x = self.frame.origin.x + self.frame.size.width/2 - frame.size.width/2;
frame.origin.y = self.frame.origin.y;
indicator.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
break;
case UIViewContentModeBottom:
frame.origin.x = self.frame.origin.x + self.frame.size.width/2 - frame.size.width/2;
frame.origin.y = self.frame.origin.y + self.frame.size.height - frame.size.height;
indicator.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
break;
case UIViewContentModeTopLeft:
frame.origin.x = self.frame.origin.x;
frame.origin.y = self.frame.origin.y;
indicator.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin;
break;
case UIViewContentModeTopRight:
frame.origin.x = self.frame.origin.x + self.frame.size.width - frame.size.width;
frame.origin.y = self.frame.origin.y;
indicator.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin;
break;
case UIViewContentModeBottomLeft:
frame.origin.x = self.frame.origin.x;
frame.origin.y = self.frame.origin.y + self.frame.size.height - frame.size.height;
indicator.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin;
break;
case UIViewContentModeBottomRight:
frame.origin.x = self.frame.origin.x + self.frame.size.width - frame.size.width;
frame.origin.y = self.frame.origin.y + self.frame.size.height - frame.size.height;
indicator.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;
break;
case UIViewContentModeCenter:
frame.origin.x = self.frame.origin.x + self.frame.size.width/2 - frame.size.width/2;
frame.origin.y = self.frame.origin.y + self.frame.size.height/2 - frame.size.height/2;
indicator.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;
break;
default:
frame.origin.x = self.frame.origin.x + self.frame.size.width/2 - frame.size.width/2;
frame.origin.y = self.frame.origin.y + self.frame.size.height/2 - frame.size.height/2;
indicator.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin;
break;
}
indicator.frame = frame;
}
- (void)hideProcessIndicator
{
UIActivityIndicatorView *indicator = [[UIViewsIndicatorsPull instance] indicatorForViewObject:self];
[indicator stopAnimating];
[indicator removeFromSuperview];
self.alpha = 1.0;
}
- (BOOL)isProcessIndicatorPresented
{
return self.alpha == 0;
}
@end
添加此文件到項目中,然後調用細胞:
[cell.contentView showProcessIndicator];
[cell.contentView showProcessIndicatorWithAlignment:UIViewContentModeCenter];
[cell.contentView showProcessIndicatorWithAlignment:UIViewContentModeCenter style:UIActivityIndicatorViewStyleGray];
[cell.contentView hideProcessIndicator];
你說的是在呈現出裝載圖屏幕中心? – gabbler 2014-10-08 17:45:21
你有沒有考慮過使用'UIRefreshControl'?如果你使用'UITableViewController',你可以免費獲得這個功能 – 2014-10-08 17:45:38