iOS自定义LoadingView

本文介绍如何在应用开发中通过封装 LoadingView 类来实现加载数据时的人性化设计,包括添加文字说明的 loadingview 和使用 QuartzCore 动画效果。详细解释了如何在不同场景下调用 LoadingView 类并控制其显示与隐藏。

应用开发的时候,加载数据的时候需要加载页面,如果没用,那么就缺少人性化设计了。系统自带的是UIActivityIndicatorView,但它缺少文字说明,要加上文字说明的loading view只有自子封装。代码如下:

LoadingView.h

Cpp代码
  1. #import<UIKit/UIKit.h>
  2. @interfaceLoadingView:UIView
  3. {
  4. }
  5. +(id)loadingViewInView:(UIView*)aSuperview;
  6. -(void)removeView;
  7. @end

LoadingView.m
Cpp代码
  1. #import"LoadingView.h"
  2. #import<QuartzCore/QuartzCore.h>
  3. //
  4. //NewPathWithRoundRect
  5. //
  6. //CreatesaCGPathRectwitharoundrectofthegivenradius.
  7. //
  8. CGPathRefNewPathWithRoundRect(CGRectrect,CGFloatcornerRadius)
  9. {
  10. //
  11. //Createtheboundarypath
  12. //
  13. CGMutablePathRefpath=CGPathCreateMutable();
  14. CGPathMoveToPoint(path,NULL,
  15. rect.origin.x,
  16. rect.origin.y+rect.size.height-cornerRadius);
  17. //Topleftcorner
  18. CGPathAddArcToPoint(path,NULL,
  19. rect.origin.x,
  20. rect.origin.y,
  21. rect.origin.x+rect.size.width,
  22. rect.origin.y,
  23. cornerRadius);
  24. //Toprightcorner
  25. CGPathAddArcToPoint(path,NULL,
  26. rect.origin.x+rect.size.width,
  27. rect.origin.y,
  28. rect.origin.x+rect.size.width,
  29. rect.origin.y+rect.size.height,
  30. cornerRadius);
  31. //Bottomrightcorner
  32. CGPathAddArcToPoint(path,NULL,
  33. rect.origin.x+rect.size.width,
  34. rect.origin.y+rect.size.height,
  35. rect.origin.x,
  36. rect.origin.y+rect.size.height,
  37. cornerRadius);
  38. //Bottomleftcorner
  39. CGPathAddArcToPoint(path,NULL,
  40. rect.origin.x,
  41. rect.origin.y+rect.size.height,
  42. rect.origin.x,
  43. rect.origin.y,
  44. cornerRadius);
  45. //Closethepathattheroundedrect
  46. CGPathCloseSubpath(path);
  47. returnpath;
  48. }
  49. @implementationLoadingView
  50. //
  51. //loadingViewInView:
  52. //
  53. //Constructorforthisview.Createsandaddsaloadingviewforcoveringthe
  54. //providedaSuperview.
  55. //
  56. //Parameters:
  57. //aSuperview-thesuperviewthatwillbecoveredbytheloadingview
  58. //
  59. //returnstheconstructedview,alreadyaddedasasubviewoftheaSuperview
  60. //(andhenceretainedbythesuperview)
  61. //
  62. +(id)loadingViewInView:(UIView*)aSuperview
  63. {
  64. LoadingView*loadingView=
  65. [[[LoadingViewalloc]initWithFrame:[aSuperviewbounds]]autorelease];
  66. if(!loadingView)
  67. {
  68. returnnil;
  69. }
  70. loadingView.opaque=NO;
  71. loadingView.autoresizingMask=
  72. UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
  73. [aSuperviewaddSubview:loadingView];
  74. constCGFloatDEFAULT_LABEL_WIDTH=280.0;
  75. constCGFloatDEFAULT_LABEL_HEIGHT=50.0;
  76. CGRectlabelFrame=CGRectMake(0,0,DEFAULT_LABEL_WIDTH,DEFAULT_LABEL_HEIGHT);
  77. UILabel*loadingLabel=
  78. [[[UILabelalloc]
  79. initWithFrame:labelFrame]
  80. autorelease];
  81. loadingLabel.text=NSLocalizedString(@"Loading...",nil);
  82. loadingLabel.textColor=[UIColorwhiteColor];
  83. loadingLabel.backgroundColor=[UIColorclearColor];
  84. loadingLabel.textAlignment=UITextAlignmentCenter;
  85. loadingLabel.font=[UIFontboldSystemFontOfSize:[UIFontlabelFontSize]];
  86. loadingLabel.autoresizingMask=
  87. UIViewAutoresizingFlexibleLeftMargin|
  88. UIViewAutoresizingFlexibleRightMargin|
  89. UIViewAutoresizingFlexibleTopMargin|
  90. UIViewAutoresizingFlexibleBottomMargin;
  91. [loadingViewaddSubview:loadingLabel];
  92. UIActivityIndicatorView*activityIndicatorView=
  93. [[[UIActivityIndicatorViewalloc]
  94. initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]
  95. autorelease];
  96. [loadingViewaddSubview:activityIndicatorView];
  97. activityIndicatorView.autoresizingMask=
  98. UIViewAutoresizingFlexibleLeftMargin|
  99. UIViewAutoresizingFlexibleRightMargin|
  100. UIViewAutoresizingFlexibleTopMargin|
  101. UIViewAutoresizingFlexibleBottomMargin;
  102. [activityIndicatorViewstartAnimating];
  103. CGFloattotalHeight=
  104. loadingLabel.frame.size.height+
  105. activityIndicatorView.frame.size.height;
  106. labelFrame.origin.x=floor(0.5*(loadingView.frame.size.width-DEFAULT_LABEL_WIDTH));
  107. labelFrame.origin.y=floor(0.5*(loadingView.frame.size.height-totalHeight));
  108. loadingLabel.frame=labelFrame;
  109. CGRectactivityIndicatorRect=activityIndicatorView.frame;
  110. activityIndicatorRect.origin.x=
  111. 0.5*(loadingView.frame.size.width-activityIndicatorRect.size.width);
  112. activityIndicatorRect.origin.y=
  113. loadingLabel.frame.origin.y+loadingLabel.frame.size.height;
  114. activityIndicatorView.frame=activityIndicatorRect;
  115. //Setupthefade-inanimation
  116. CATransition*animation=[CATransitionanimation];
  117. [animationsetType:kCATransitionFade];
  118. [[aSuperviewlayer]addAnimation:animationforKey:@"layerAnimation"];
  119. returnloadingView;
  120. }
  121. //
  122. //removeView
  123. //
  124. //Animatestheviewoutfromthesuperview.Astheviewisremovedfromthe
  125. //superview,itwillbereleased.
  126. //
  127. -(void)removeView
  128. {
  129. UIView*aSuperview=[selfsuperview];
  130. [superremoveFromSuperview];
  131. //Setuptheanimation
  132. CATransition*animation=[CATransitionanimation];
  133. [animationsetType:kCATransitionFade];
  134. [[aSuperviewlayer]addAnimation:animationforKey:@"layerAnimation"];
  135. }
  136. //
  137. //drawRect:
  138. //
  139. //Drawtheview.
  140. //
  141. -(void)drawRect:(CGRect)rect
  142. {
  143. rect.size.height-=1;
  144. rect.size.width-=1;
  145. constCGFloatRECT_PADDING=8.0;
  146. rect=CGRectInset(rect,RECT_PADDING,RECT_PADDING);
  147. constCGFloatROUND_RECT_CORNER_RADIUS=5.0;
  148. CGPathRefroundRectPath=NewPathWithRoundRect(rect,ROUND_RECT_CORNER_RADIUS);
  149. CGContextRefcontext=UIGraphicsGetCurrentContext();
  150. constCGFloatBACKGROUND_OPACITY=0.85;
  151. CGContextSetRGBFillColor(context,0,0,0,BACKGROUND_OPACITY);
  152. CGContextAddPath(context,roundRectPath);
  153. CGContextFillPath(context);
  154. constCGFloatSTROKE_OPACITY=0.25;
  155. CGContextSetRGBStrokeColor(context,1,1,1,STROKE_OPACITY);
  156. CGContextAddPath(context,roundRectPath);
  157. CGContextStrokePath(context);
  158. CGPathRelease(roundRectPath);
  159. }
  160. //
  161. //dealloc
  162. //
  163. //Releaseinstancememory.
  164. //
  165. -(void)dealloc
  166. {
  167. [superdealloc];
  168. }
  169. @end

记得要加入QuartzCore.framework 到工程。

用法就更简单了,加入的时候只需调用:

Cpp代码
  1. LoadingView*loadingView=[LoadingViewloadingViewInView:[self.view.window.subviewsobjectAtIndex:0]];

删除的时候只需调用:
Cpp代码
  1. [loadingViewremoveFromSuperview];

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值