// ActivityView.h
//
// Copyright 2010. All rights reserved.
//
#import <UIKit/UIKit.h>
#define kAnimationDurationStart 2
#define kAnimationDurationEnd 1
@interface ActivityView : NSObject {
IBOutlet UILabel *messageLabel;
IBOutlet UIView *view;
BOOL isShow;
}
@property (nonatomic, readonly) UILabel *messageLabel;
@property (nonatomic, readonly) UIView *view;
@property (nonatomic) BOOL isShow;
+ (ActivityView *)sharedActivityView;
- (void)showWithMessage:(NSString *)message animate:(BOOL)animate;
- (void)hide:(BOOL)animate;
@end
//
// ActivityView.m
//
// Copyright 2010 All rights reserved.
//
#import "ActivityView.h"
@implementation ActivityView
@synthesize messageLabel,view, isShow;
//单例模式
static ActivityView *activityView;
- (id) init {
self = [super init];
if (self != nil) {
[[NSBundle mainBundle] loadNibNamed:@"ActivityView" owner:self options:nil];
[[[UIApplication sharedApplication] keyWindow] addSubview:view];
[view setFrame:CGRectMake(0, [UIApplication sharedApplication].statusBarFrame.size.height-view.frame.size.height, view.frame.size.width, view.frame.size.height)];
isShow = NO;
}
return self;
}
+ (ActivityView *)sharedActivityView {
if (!activityView) {
activityView = [[ActivityView alloc]init];
}
return activityView;
}
- (void)showWithMessage:(NSString *)message animate:(BOOL)animate {
isShow = YES;
messageLabel.text = message;
[view.superview bringSubviewToFront:view];
if ( animate )
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:kAnimationDurationStart];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[view setFrame:CGRectMake(0, [UIApplication sharedApplication].statusBarFrame.size.height, view.frame.size.width, view.frame.size.height)];
[UIView commitAnimations];
}
else
{
[view setFrame:CGRectMake(0, [UIApplication sharedApplication].statusBarFrame.size.height, view.frame.size.width, view.frame.size.height)];
}
}
- (void)hide:(BOOL)animate{
messageLabel.text = @"";
if (animate)
{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:kAnimationDurationEnd];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[view setFrame:CGRectMake(0, [UIApplication sharedApplication].statusBarFrame.size.height-view.frame.size.height, view.frame.size.width, view.frame.size.height)];
[UIView commitAnimations];
}
else
{
[view setFrame:CGRectMake(0, [UIApplication sharedApplication].statusBarFrame.size.height-view.frame.size.height, view.frame.size.width, view.frame.size.height)];
}
[[UIApplication sharedApplication] keyWindow].userInteractionEnabled = YES;
isShow = NO;
}
@end
调用方法
[[ActivityView sharedActivityView] showWithMessage:@"正在下载数据" animate: NO];
[[ActivityView sharedActivityView] hide: YES];
如何更改ActivityView位置,
修改 setFrame方法
coco china里有人提供了另外一个方法:
#define activityViewTag 0x98751234
@interface UIView (UIViewUtils)
- (void)showActivityViewAtCenter;
- (void)hideActivityViewAtCenter;
- (UIActivityIndicatorView*)createActivityViewAtCenter:(UIActivityIndicatorViewStyle)style;
- (UIActivityIndicatorView*)getActivityViewAtCenter;
@end
#import "UIViewUtils.h"
@implementation UIView (UIViewUtils)
- (UIActivityIndicatorView*)createActivityViewAtCenter:(UIActivityIndicatorViewStyle)style
{
static int size = 30;
UIActivityIndicatorView* activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:style];
activityView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2 - size/2, [UIScreen mainScreen].bounds.size.height/2 - size*2, size, size);
activityView.tag = activityViewTag;
[self addSubview:activityView];
[activityView release];
return activityView;
}
- (UIActivityIndicatorView*)getActivityViewAtCenter
{
UIView* view = [self viewWithTag:activityViewTag];
if (view != nil && [view isKindOfClass:[UIActivityIndicatorView class]]){
return (UIActivityIndicatorView*)view;
}
else {
return nil;
}
}
- (void)showActivityViewAtCenter
{
UIActivityIndicatorView* activityView = [self getActivityViewAtCenter];
if (activityView == nil){
activityView = [self createActivityViewAtCenter:UIActivityIndicatorViewStyleWhite];
}
[activityView startAnimating];
}
- (void)hideActivityViewAtCenter
{
UIActivityIndicatorView* activityView = [self getActivityViewAtCenter];
if (activityView != nil){
[activityView stopAnimating];
}
}
@end