应用开发的时候,加载数据的时候需要加载页面,如果没用,那么就缺少人性化设计了。系统自带的是UIActivityIndicatorView,但它缺少文字说明,要加上文字说明的loading view只有自子封装。代码如下:
LoadingView.h
- #import<UIKit/UIKit.h>
- @interfaceLoadingView:UIView
- {
- }
- +(id)loadingViewInView:(UIView*)aSuperview;
- -(void)removeView;
- @end
LoadingView.m
- #import"LoadingView.h"
- #import<QuartzCore/QuartzCore.h>
- //
- //NewPathWithRoundRect
- //
- //CreatesaCGPathRectwitharoundrectofthegivenradius.
- //
- CGPathRefNewPathWithRoundRect(CGRectrect,CGFloatcornerRadius)
- {
- //
- //Createtheboundarypath
- //
- CGMutablePathRefpath=CGPathCreateMutable();
- CGPathMoveToPoint(path,NULL,
- rect.origin.x,
- rect.origin.y+rect.size.height-cornerRadius);
- //Topleftcorner
- CGPathAddArcToPoint(path,NULL,
- rect.origin.x,
- rect.origin.y,
- rect.origin.x+rect.size.width,
- rect.origin.y,
- cornerRadius);
- //Toprightcorner
- CGPathAddArcToPoint(path,NULL,
- rect.origin.x+rect.size.width,
- rect.origin.y,
- rect.origin.x+rect.size.width,
- rect.origin.y+rect.size.height,
- cornerRadius);
- //Bottomrightcorner
- CGPathAddArcToPoint(path,NULL,
- rect.origin.x+rect.size.width,
- rect.origin.y+rect.size.height,
- rect.origin.x,
- rect.origin.y+rect.size.height,
- cornerRadius);
- //Bottomleftcorner
- CGPathAddArcToPoint(path,NULL,
- rect.origin.x,
- rect.origin.y+rect.size.height,
- rect.origin.x,
- rect.origin.y,
- cornerRadius);
- //Closethepathattheroundedrect
- CGPathCloseSubpath(path);
- returnpath;
- }
- @implementationLoadingView
- //
- //loadingViewInView:
- //
- //Constructorforthisview.Createsandaddsaloadingviewforcoveringthe
- //providedaSuperview.
- //
- //Parameters:
- //aSuperview-thesuperviewthatwillbecoveredbytheloadingview
- //
- //returnstheconstructedview,alreadyaddedasasubviewoftheaSuperview
- //(andhenceretainedbythesuperview)
- //
- +(id)loadingViewInView:(UIView*)aSuperview
- {
- LoadingView*loadingView=
- [[[LoadingViewalloc]initWithFrame:[aSuperviewbounds]]autorelease];
- if(!loadingView)
- {
- returnnil;
- }
- loadingView.opaque=NO;
- loadingView.autoresizingMask=
- UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
- [aSuperviewaddSubview:loadingView];
- constCGFloatDEFAULT_LABEL_WIDTH=280.0;
- constCGFloatDEFAULT_LABEL_HEIGHT=50.0;
- CGRectlabelFrame=CGRectMake(0,0,DEFAULT_LABEL_WIDTH,DEFAULT_LABEL_HEIGHT);
- UILabel*loadingLabel=
- [[[UILabelalloc]
- initWithFrame:labelFrame]
- autorelease];
- loadingLabel.text=NSLocalizedString(@"Loading...",nil);
- loadingLabel.textColor=[UIColorwhiteColor];
- loadingLabel.backgroundColor=[UIColorclearColor];
- loadingLabel.textAlignment=UITextAlignmentCenter;
- loadingLabel.font=[UIFontboldSystemFontOfSize:[UIFontlabelFontSize]];
- loadingLabel.autoresizingMask=
- UIViewAutoresizingFlexibleLeftMargin|
- UIViewAutoresizingFlexibleRightMargin|
- UIViewAutoresizingFlexibleTopMargin|
- UIViewAutoresizingFlexibleBottomMargin;
- [loadingViewaddSubview:loadingLabel];
- UIActivityIndicatorView*activityIndicatorView=
- [[[UIActivityIndicatorViewalloc]
- initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]
- autorelease];
- [loadingViewaddSubview:activityIndicatorView];
- activityIndicatorView.autoresizingMask=
- UIViewAutoresizingFlexibleLeftMargin|
- UIViewAutoresizingFlexibleRightMargin|
- UIViewAutoresizingFlexibleTopMargin|
- UIViewAutoresizingFlexibleBottomMargin;
- [activityIndicatorViewstartAnimating];
- CGFloattotalHeight=
- loadingLabel.frame.size.height+
- activityIndicatorView.frame.size.height;
- labelFrame.origin.x=floor(0.5*(loadingView.frame.size.width-DEFAULT_LABEL_WIDTH));
- labelFrame.origin.y=floor(0.5*(loadingView.frame.size.height-totalHeight));
- loadingLabel.frame=labelFrame;
- CGRectactivityIndicatorRect=activityIndicatorView.frame;
- activityIndicatorRect.origin.x=
- 0.5*(loadingView.frame.size.width-activityIndicatorRect.size.width);
- activityIndicatorRect.origin.y=
- loadingLabel.frame.origin.y+loadingLabel.frame.size.height;
- activityIndicatorView.frame=activityIndicatorRect;
- //Setupthefade-inanimation
- CATransition*animation=[CATransitionanimation];
- [animationsetType:kCATransitionFade];
- [[aSuperviewlayer]addAnimation:animationforKey:@"layerAnimation"];
- returnloadingView;
- }
- //
- //removeView
- //
- //Animatestheviewoutfromthesuperview.Astheviewisremovedfromthe
- //superview,itwillbereleased.
- //
- -(void)removeView
- {
- UIView*aSuperview=[selfsuperview];
- [superremoveFromSuperview];
- //Setuptheanimation
- CATransition*animation=[CATransitionanimation];
- [animationsetType:kCATransitionFade];
- [[aSuperviewlayer]addAnimation:animationforKey:@"layerAnimation"];
- }
- //
- //drawRect:
- //
- //Drawtheview.
- //
- -(void)drawRect:(CGRect)rect
- {
- rect.size.height-=1;
- rect.size.width-=1;
- constCGFloatRECT_PADDING=8.0;
- rect=CGRectInset(rect,RECT_PADDING,RECT_PADDING);
- constCGFloatROUND_RECT_CORNER_RADIUS=5.0;
- CGPathRefroundRectPath=NewPathWithRoundRect(rect,ROUND_RECT_CORNER_RADIUS);
- CGContextRefcontext=UIGraphicsGetCurrentContext();
- constCGFloatBACKGROUND_OPACITY=0.85;
- CGContextSetRGBFillColor(context,0,0,0,BACKGROUND_OPACITY);
- CGContextAddPath(context,roundRectPath);
- CGContextFillPath(context);
- constCGFloatSTROKE_OPACITY=0.25;
- CGContextSetRGBStrokeColor(context,1,1,1,STROKE_OPACITY);
- CGContextAddPath(context,roundRectPath);
- CGContextStrokePath(context);
- CGPathRelease(roundRectPath);
- }
- //
- //dealloc
- //
- //Releaseinstancememory.
- //
- -(void)dealloc
- {
- [superdealloc];
- }
- @end
记得要加入QuartzCore.framework 到工程。
用法就更简单了,加入的时候只需调用:
- LoadingView*loadingView=[LoadingViewloadingViewInView:[self.view.window.subviewsobjectAtIndex:0]];
删除的时候只需调用:
- [loadingViewremoveFromSuperview];
本文介绍如何在应用开发中通过封装 LoadingView 类来实现加载数据时的人性化设计,包括添加文字说明的 loadingview 和使用 QuartzCore 动画效果。详细解释了如何在不同场景下调用 LoadingView 类并控制其显示与隐藏。
1537

被折叠的 条评论
为什么被折叠?



