菊花怪

//
//  MyProgressView.h
//  test2
//
//  Created by MSMW on 15/3/18.
//  Copyright (c) 2015年 msmw. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface MyProgressView : UIView
{
    
    UIActivityIndicatorView* indicator;
    
    UILabel* label;
    
    BOOL visible,blocked;
    
    UIView* maskView;
    
    CGRect rectHud,rectSuper,rectOrigin;//外殼區域、父視圖區域
    
    UIView* viewHud;//外殼
    

}

@property (assign) BOOL visible;

-(id)initWithFrame:(CGRect)frame superView:(UIView*)superView;

-(void)show:(BOOL)block;// block:是否阻塞父視圖

-(void)hide;

-(void)setMessage:(NSString*)newMsg;

-(void)alignToCenter;



@end
//
//  MyProgressView.m
//  test2
//
//  Created by MSMW on 15/3/18.
//  Copyright (c) 2015年 msmw. All rights reserved.
//

#import "MyProgressView.h"

@implementation MyProgressView

@synthesize visible;

-(id)initWithFrame:(CGRect)frame superView:(UIView*)superView{
    
    rectOrigin=frame;
    
    rectSuper=[superView bounds];
    
    //保持正方形比例
    
    rectHud=CGRectMake(frame.origin.x,frame.origin.y, frame.size.width, frame.size.width);
    self = [super initWithFrame:rectHud];
    
    if (self) {
        
        self.backgroundColor =[UIColor clearColor];
        
        self.opaque = NO;
        
        viewHud=[[UIView alloc]initWithFrame:rectHud];
        
        [self addSubview:viewHud];
        
        indicator=[[UIActivityIndicatorView alloc]
                   
                   initWithActivityIndicatorStyle:
                   
                   UIActivityIndicatorViewStyleWhiteLarge];
        
        double gridUnit=round(rectHud.size.width/12);
        
        float ind_width=6*gridUnit;
        
        indicator.frame=CGRectMake(
                                   
                                   3*gridUnit,
                                   
                                   2*gridUnit,
                                   
                                   ind_width,
                                   
                                   ind_width);
        
        [viewHud addSubview:indicator];
        
        CGRect rectLabel=CGRectMake(1*gridUnit,
                                    
                                    9*gridUnit,
                                    
                                    10*gridUnit, 2*gridUnit);
        
        label=[[UILabel alloc]initWithFrame:rectLabel];
        
        label.backgroundColor=[UIColor clearColor];
        
        label.font=[UIFont fontWithName:@"Arial" size:14];
        
        label.textAlignment=NSTextAlignmentCenter;
        
        label.textColor=[UIColor whiteColor];
        
        label.text=@"請等待...";
        
        label.adjustsFontSizeToFitWidth=YES;
        
        [viewHud addSubview:label];
        
        visible=NO;
        
        [self setHidden:YES];
        
        [superView addSubview:self];
        
    }
    
    return self;
    
}

#pragma mark Drawing

- (void)drawRect:(CGRect)rect {
    
    if(visible){
        
        CGContextRef context = UIGraphicsGetCurrentContext();
        
        CGRect boxRect = rectHud;
        
        // 繪製圓角矩形
        
        float radius = 10.0f;
        
        // 路徑開始
        
        CGContextBeginPath(context);
        
        // 填充色:灰度0.0,透明度:0.1
        
        CGContextSetGrayFillColor(context,0.0f, 0.25);
        
        // 畫筆移動到左上角的圓弧處
        
        CGContextMoveToPoint(context,CGRectGetMinX(boxRect) + radius, CGRectGetMinY(boxRect));
        
        // 開始繪製右上角圓弧:圓心x座標,圓心y座標,起始角,終止角,方向爲順時針
        
        CGContextAddArc(context,CGRectGetMaxX(boxRect) - radius, CGRectGetMinY(boxRect) + radius, radius,3*(float)M_PI/2 ,
                        0,               0);
        
        // 開始繪製右下角圓弧
        
        CGContextAddArc(context,CGRectGetMaxX(boxRect) - radius, CGRectGetMaxY(boxRect) - radius, radius,       0     ,      (float)M_PI / 2, 0);
        
        // 開始繪製左下角圓弧
        
        CGContextAddArc(context,CGRectGetMinX(boxRect) + radius, CGRectGetMaxY(boxRect) - radius, radius, (float)M_PI / 2,
            (float)M_PI,     0);
        
        // 開始繪製左上角圓弧
        
        CGContextAddArc(context,CGRectGetMinX(boxRect) + radius, CGRectGetMinY(boxRect) + radius, radius, (float)M_PI,           (float)M_PI / 2, 0);
        
        NSLog (@"M_PI:%f",(float)M_PI);
//                NSLog(@"MinX is:%f",CGRectGetMinX(boxRect));
//                NSLog(@"MaxX is:%f",CGRectGetMaxX(boxRect));
//                NSLog(@"MinY is:%f",CGRectGetMinY(boxRect));
//                NSLog(@"MaxY is:%f",CGRectGetMaxY(boxRect));
        
        
//          CGContextClosePath(context);// 關閉路徑
        
        CGContextFillPath(context);// 填充路徑,該函數自動關閉路徑
        
        //將畫面按照這個方向平移
        //[self setFrame:CGRectOffset(self.frame, 100, 160)];
        //這行代碼是最新添加的,表示繪製結束後,講畫面放到屏幕最中間
        [self alignToCenter];
    }
    
}

#pragma mark Action

-(void)show:(BOOL)block{
    
    if (block && blocked==NO) {
        
        CGPoint offset=self.frame.origin;
        
        // 改變視圖大小爲父視圖大小
        
        self.frame=rectSuper;
        
        viewHud.frame=CGRectOffset(viewHud.frame, offset.x, offset.y);
        
        if (maskView==nil) {
            
            maskView=[[UIView alloc]initWithFrame:rectSuper];
            
        }else{
            
            maskView.frame=rectSuper;
            
        }
        
        maskView.backgroundColor=[UIColor clearColor];
        
        [self addSubview:maskView];
        
        [self bringSubviewToFront:maskView];
        
        blocked=YES;
        
    }
    
    [indicator startAnimating];
    
    [self setHidden:NO];
    
    [self setNeedsLayout];
    
    visible=YES;
    
}

#pragma mark 將這個東東隱藏
-(void)hide{
    
    visible=NO;
    
    [indicator stopAnimating];
    
    [self setHidden:YES];
    
}

#pragma mark 改變裏面的文字
-(void)setMessage:(NSString*)newMsg{
    
    label.text=newMsg;
    
}

#pragma mark 將這個東東居中
-(void)alignToCenter{
    
    CGPoint centerSuper={rectSuper.size.width/2,rectSuper.size.height/2};
    
    CGPoint centerSelf={self.frame.origin.x+self.frame.size.width/2,
        
        self.frame.origin.y+self.frame.size.height/2};
    
    CGPoint offset={centerSuper.x-centerSelf.x,centerSuper.y-centerSelf.y};
    
    CGRect newRect=CGRectOffset(self.frame, offset.x, offset.y);
    
    [self setFrame:newRect];
    
    rectHud=newRect;
    
    // NSLog(@"newRect:%f,%f",newRect.origin.x,newRect.origin.y);
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

本來在別的地方看見的。但是代碼有點問題  經過修改可以用了 。這個菊花怪比較簡單  用的時候只需要
//    無敵風火輪
   indicator = [[MyProgressView alloc]initWithFrame:CGRectMake(0, 0, 120, 120) superView:self.view];
    [indicator setMessage:@"正在努力加載,請稍候..."];
    
    if (indicator.visible==NO) {
        [indicator show:NO];
    }else {
        [indicator hide];
    }
就可以了。什麼時候想讓菊花怪停下來就在那個地方讓indicator hide就好了 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章