苹果无敌风火轮如果不是那么酷的

来源:互联网 发布:mac爱奇艺视频转换器 编辑:程序博客网 时间:2024/06/09 17:24

引用:

      苹果无敌风火轮如果不是那么酷的话,我们就不需要定制它了。可惜的是,UIActivityIndicator只有一个初始化方法 initWithActivityIndicatorStyle。

      一,不能任意改变它的大小——有时候我们需要一个比 UIActivityIndicatorViewStyleWhiteLarge还要更大的无敌风火轮;

      二,它不够友好——我们需要在风火轮的下面显示一些友好的提示信息。


为此,我们不惜代价,自己用一个UIActivityIndicator控件和用Quartz绘图的手段,定制了一个自己的无敌风火轮。


实现自己的无敌风火轮主要思想如下:

1. 首先建一个MyProgressView类继承UIView,因为我们打算新建一个UIView,在上面添加Indicator和文字并且绘制背景,然后把这个View放到当前界面中

2. 然后就是对这个MyprogressView进行操作了,重写initWithFrame,根据传进来的坐标和大小绘制,固定Indicator和Lable的位置和大小,并且添加到我们定义的外部框架viewHud 中

3. 重写drawRect,判断自定义的view是否可见,可见就绘制圆角矩形,主要根据CGRect绘制路线并根据CGContextFillPath将路线进行颜色填充,这个函数是在自定义view显示之后执行的,所以我们可以在里面进行view移位操作,通过CGRectOffset函数将CGRect进行移位,也可以调用alignToCenter将其居中

4. show,hide,setMessage主要是用来对我们定义的view进行进一步的操作

5. alignToCenter主要是将当前的自定义view进行移位操作


请看源码:

MyProgressView.h文件

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  MyProgressView.h  
  3. //  FourthIOSProject  
  4. //  
  5. //  Created by Mac on 14-4-16.  
  6. //  Copyright (c) 2014年 Mac. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10. #import <UIKit/UIKit.h>  
  11.   
  12. @interface MyProgressView : UIView{  
  13.       
  14.     UIActivityIndicatorView* indicator;  
  15.       
  16.     UILabel* label;  
  17.       
  18.     BOOL visible,blocked;  
  19.       
  20.     UIView* maskView;  
  21.       
  22.     CGRect rectHud,rectSuper,rectOrigin;//外壳区域、父视图区域  
  23.       
  24.     UIView* viewHud;//外壳  
  25.       
  26. }  
  27.   
  28. @property (assign) BOOL visible;  
  29.   
  30. -(id)initWithFrame:(CGRect)frame superView:(UIView*)superView;  
  31.   
  32. -(void)show:(BOOL)block;// block:是否阻塞父视图  
  33.   
  34. -(void)hide;  
  35.   
  36. -(void)setMessage:(NSString*)newMsg;  
  37.   
  38. -(void)alignToCenter;  
  39.   
  40. @end  


MyProgressView.m 文件

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  MyProgressView.m  
  3. //  FourthIOSProject  
  4. //  
  5. //  Created by Mac on 14-4-16.  
  6. //  Copyright (c) 2014年 Mac. All rights reserved.  
  7. //  
  8.   
  9. #import "MyProgressView.h"  
  10.   
  11. @implementation MyProgressView  
  12.   
  13. @synthesize visible;  
  14.   
  15. -(id)initWithFrame:(CGRect)frame superView:(UIView*)superView{  
  16.       
  17.     rectOrigin=frame;  
  18.       
  19.     rectSuper=[superView bounds];  
  20.       
  21.     //保持正方形比例  
  22.       
  23.     rectHud=CGRectMake(frame.origin.x,frame.origin.y, frame.size.width, frame.size.width);  
  24.       
  25.     self = [super initWithFrame:rectHud];  
  26.       
  27.     if (self) {  
  28.           
  29.         self.backgroundColor =[UIColor clearColor];  
  30.           
  31.         self.opaque = NO;  
  32.           
  33.         viewHud=[[UIView alloc]initWithFrame:rectHud];  
  34.           
  35.         [self addSubview:viewHud];  
  36.           
  37.         indicator=[[UIActivityIndicatorView alloc]  
  38.                      
  39.                    initWithActivityIndicatorStyle:  
  40.                      
  41.                    UIActivityIndicatorViewStyleWhiteLarge];  
  42.           
  43.         double gridUnit=round(rectHud.size.width/12);  
  44.           
  45.         float ind_width=6*gridUnit;  
  46.           
  47.         indicator.frame=CGRectMake(  
  48.                                      
  49.                                    3*gridUnit,  
  50.                                      
  51.                                    2*gridUnit,  
  52.                                      
  53.                                    ind_width,  
  54.                                      
  55.                                    ind_width);  
  56.           
  57.         [viewHud addSubview:indicator];  
  58.           
  59.         CGRect rectLabel=CGRectMake(1*gridUnit,  
  60.                                       
  61.                                     9*gridUnit,  
  62.                                       
  63.                                     10*gridUnit, 2*gridUnit);  
  64.           
  65.         label=[[UILabel alloc]initWithFrame:rectLabel];  
  66.           
  67.         label.backgroundColor=[UIColor clearColor];  
  68.           
  69.         label.font=[UIFont fontWithName:@"Arial" size:14];  
  70.           
  71.         label.textAlignment=UITextAlignmentCenter;  
  72.           
  73.         label.textColor=[UIColor whiteColor];  
  74.           
  75.         label.text=@"请等待...";  
  76.           
  77.         label.adjustsFontSizeToFitWidth=YES;  
  78.           
  79.         [viewHud addSubview:label];  
  80.           
  81.         visible=NO;  
  82.           
  83.         [self setHidden:YES];  
  84.           
  85.         [superView addSubview:self];  
  86.           
  87.     }  
  88.       
  89.     return self;  
  90.       
  91. }  
  92.   
  93. #pragma mark Drawing  
  94.   
  95. - (void)drawRect:(CGRect)rect {  
  96.       
  97.     if(visible){  
  98.   
  99.         CGContextRef context = UIGraphicsGetCurrentContext();  
  100.           
  101.         CGRect boxRect = rectHud;  
  102.           
  103.         // 绘制圆角矩形  
  104.           
  105.         float radius = 10.0f;  
  106.           
  107.         // 路径开始  
  108.           
  109.         CGContextBeginPath(context);  
  110.           
  111.         // 填充色:灰度0.0,透明度:0.1  
  112.           
  113.         CGContextSetGrayFillColor(context,0.0f0.25);  
  114.           
  115.         // 画笔移动到左上角的圆弧处  
  116.           
  117.         CGContextMoveToPoint(context,CGRectGetMinX(boxRect) + radius, CGRectGetMinY(boxRect));  
  118.           
  119.         // 开始绘制右上角圆弧:圆心x坐标,圆心y坐标,起始角,终止角,方向为顺时针  
  120.           
  121.         CGContextAddArc(context,CGRectGetMaxX(boxRect) - radius, CGRectGetMinY(boxRect) + radius, radius, 33 * (float)M_PI / 200);  
  122.           
  123.         // 开始绘制右下角圆弧  
  124.           
  125.         CGContextAddArc(context,CGRectGetMaxX(boxRect) - radius, CGRectGetMaxY(boxRect) - radius, radius, 0, (float)M_PI / 20);  
  126.           
  127.         // 开始绘制左下角圆弧  
  128.           
  129.         CGContextAddArc(context,CGRectGetMinX(boxRect) + radius, CGRectGetMaxY(boxRect) - radius, radius, (float)M_PI / 2, (float)M_PI, 0);  
  130.           
  131.         // 开始绘制左上角圆弧  
  132.           
  133.         CGContextAddArc(context,CGRectGetMinX(boxRect) + radius, CGRectGetMinY(boxRect) + radius, radius, (float)M_PI, 33 * (float)M_PI / 20);  
  134.           
  135. //        NSLog(@"MinX is:%f",CGRectGetMinX(boxRect));  
  136. //        NSLog(@"MaxX is:%f",CGRectGetMaxX(boxRect));  
  137. //        NSLog(@"MinY is:%f",CGRectGetMinY(boxRect));  
  138. //        NSLog(@"MaxY is:%f",CGRectGetMaxY(boxRect));  
  139.           
  140.           
  141.         //  CGContextClosePath(context);// 关闭路径  
  142.           
  143.         CGContextFillPath(context);// 填充路径,该函数自动关闭路径  
  144.           
  145.         //将画面按照这个方向平移  
  146.         //[self setFrame:CGRectOffset(self.frame, 100, 160)];  
  147.         //这行代码是最新添加的,表示绘制结束后,讲画面放到屏幕最中间  
  148.         [self alignToCenter];  
  149.     }  
  150.       
  151. }  
  152.   
  153. #pragma mark Action  
  154.   
  155. -(void)show:(BOOL)block{  
  156.       
  157.     if (block && blocked==NO) {  
  158.           
  159.         CGPoint offset=self.frame.origin;  
  160.           
  161.         // 改变视图大小为父视图大小  
  162.           
  163.         self.frame=rectSuper;  
  164.           
  165.         viewHud.frame=CGRectOffset(viewHud.frame, offset.x, offset.y);  
  166.           
  167.         if (maskView==nil) {  
  168.               
  169.             maskView=[[UIView alloc]initWithFrame:rectSuper];  
  170.               
  171.         }else{  
  172.               
  173.             maskView.frame=rectSuper;  
  174.               
  175.         }  
  176.           
  177.         maskView.backgroundColor=[UIColor clearColor];  
  178.           
  179.         [self addSubview:maskView];  
  180.           
  181.         [self bringSubviewToFront:maskView];  
  182.           
  183.         blocked=YES;  
  184.           
  185.     }  
  186.       
  187.     [indicator startAnimating];  
  188.       
  189.     [self setHidden:NO];  
  190.       
  191.     [self setNeedsLayout];  
  192.       
  193.     visible=YES;  
  194.       
  195. }  
  196.   
  197. #pragma mark 将这个东东隐藏  
  198. -(void)hide{  
  199.       
  200.     visible=NO;  
  201.       
  202.     [indicator stopAnimating];  
  203.       
  204.     [self setHidden:YES];  
  205.       
  206. }  
  207.   
  208. #pragma mark 改变里面的文字  
  209. -(void)setMessage:(NSString*)newMsg{  
  210.       
  211.     label.text=newMsg;  
  212.       
  213. }  
  214.   
  215. #pragma mark 将这个东东居中  
  216. -(void)alignToCenter{  
  217.       
  218.     CGPoint centerSuper={rectSuper.size.width/2,rectSuper.size.height/2};  
  219.       
  220.     CGPoint centerSelf={self.frame.origin.x+self.frame.size.width/2,  
  221.           
  222.         self.frame.origin.y+self.frame.size.height/2};  
  223.       
  224.     CGPoint offset={centerSuper.x-centerSelf.x,centerSuper.y-centerSelf.y};  
  225.       
  226.     CGRect newRect=CGRectOffset(self.frame, offset.x, offset.y);  
  227.       
  228.     [self setFrame:newRect];  
  229.       
  230.     rectHud=newRect;  
  231.       
  232.     // NSLog(@"newRect:%f,%f",newRect.origin.x,newRect.origin.y);  
  233.       
  234. }  
  235. @end  

那么如何引用上面的代码产生效果呢?

ViewController.m 里面的引用代码如下:

[objc] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view.  
  5.       
  6.     //在页面中添加一个Activity Indicator View  
  7.     //这玩意不用了,太丑,现在换大的了  
  8. //    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];  
  9. //    [indicator setCenter:CGPointMake(100, 100)];  
  10. //    [self.view addSubview:indicator];   
  11. //    [indicator startAnimating];  
  12.       
  13.     MyProgressView *indicator = [[MyProgressView alloc]initWithFrame:CGRectMake(00120120) superView:self.view];  
  14.     [indicator setMessage:@"正在接受心电数据请稍候..."];  
  15.       
  16.     if (indicator.visible==NO) {  
  17.   
  18.         [indicator show:NO];  
  19.           
  20.     }else {  
  21.           
  22.         [indicator hide];  
  23.           
  24.     }  
  25.       
  26. }  
0 0
原创粉丝点击