//
// 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就好了