自动消失的UIAlert

#import <UIKit/UIKit.h>


@interface ActivityIndicatorAlertView : UIAlertView


@property (nonatomic, retain) UIActivityIndicatorView *activityIndicator;

@property (nonatomic, assign) BOOL showActivityIndicator;


- (void) stopActivityIndicator;


+ (void) alertViewAutoDismiss:(float) time title:(NSString *) title msg:(NSString *) message showActivityIndicator:(BOOL) showIndicator;


@end


#import "ActivityIndicatorAlertView.h"


#define  UIAlertViewHeight 10


@implementation ActivityIndicatorAlertView


@synthesize activityIndicator, showActivityIndicator;


- (void) dealloc {

    

    [activityIndicator release];

    

    [super dealloc];

}


- (void)drawRect:(CGRect)rect

{

    if (showActivityIndicator)

    {

        [self setFrame:CGRectMake(self.frame.origin.x, self.frame.origin.y-UIAlertViewHeight/2, self.frame.size.width, self.frame.size.height+UIAlertViewHeight)];

        

        UIView *lowestView;

        

        for (int i = 0; i < [self.subviews count]; i++)

        {

            if (![[self.subviews objectAtIndex:i] isKindOfClass:[UIControl class]])

            {

                lowestView = [self.subviews objectAtIndex:i];

            }

        }

        

        activityIndicator.center = CGPointMake(rect.size.width/2, rect.size.height/2 + 10);

        [activityIndicator startAnimating];

        

        for (UIView *sv in self.subviews)

        {

            if ([sv isKindOfClass:[UIControl class]])

            {

                sv.frame = CGRectMake(sv.frame.origin.x, sv.frame.origin.y + UIAlertViewHeight, sv.frame.size.width, sv.frame.size.height);

            }

        }

    } else

    {

        [super drawRect:rect];

    }

}


- (void) stopActivityIndicator

{

    if (activityIndicator)

    {

        [activityIndicator stopAnimating];

    }

}


- (void) dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated

{

    if (activityIndicator)

    {

        [activityIndicator stopAnimating];

    }


    [super dismissWithClickedButtonIndex:buttonIndex animated:animated];

}


- (void)show

{

    [self prepare];

    [super show];

}


- (void)prepare

{

    if (showActivityIndicator)

    {

        if (!self.activityIndicator)

        {

            self.activityIndicator = [[[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 80, 80)] autorelease];

            [self insertSubview:activityIndicator atIndex:0];

        }

    }

    

[self setNeedsDisplay];

}


+ (void) alertViewAutoDismiss:(float) time title:(NSString *) title msg:(NSString *) message showActivityIndicator:(BOOL) showIndicator

{

    ActivityIndicatorAlertView *alert = [[super alloc] initWithTitle:@""

                                                             message:message

                                                            delegate:nil

                                                   cancelButtonTitle:nil

                                                   otherButtonTitles:nil];

    

    alert.showActivityIndicator = showIndicator;

    

    [alert show];

    

    [NSTimer scheduledTimerWithTimeInterval:time

                                     target:self

                                   selector:@selector(dismissAlert:)

                                   userInfo:[NSDictionary dictionaryWithObjectsAndKeys:alert, @"alert", nil]

                                    repeats:NO];

    

    NSLog(@"release alert");

    [alert release];

}


+(void) dismissAlert:(NSTimer *)timer

{

    UIAlertView *alert = [[timer userInfoobjectForKey:@"alert"];

    [alert dismissWithClickedButtonIndex:0 animated:YES];

}


@end


你可能感兴趣的:(自动消失的UIAlert)