zoukankan      html  css  js  c++  java
  • iphone 动态显示正在刷新。

    //
    //  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  

  • 相关阅读:
    知识点复习
    【程序人生】一个IT人的立功,立言,立德三不朽
    【朝花夕拾】Android多线程之(三)runOnUiThread篇——程序猿们的贴心小棉袄
    【朝花夕拾】Android多线程之(二)ThreadLocal篇
    【朝花夕拾】Android多线程之(一)View.post()篇
    Camera2笔记
    HangFire多集群切换及DashBoard登录验证
    Linq 动态多条件group by
    Api接口签名验证
    postgre ||连接字段
  • 原文地址:https://www.cnblogs.com/likwo/p/1823396.html
Copyright © 2011-2022 走看看