IOS custom loadingview

Source: Internet
Author: User

During application development, the page needs to be loaded when loading data. If it is useless, there is a lack of human design. The system comes with the uiactivityindicatorview, but it lacks the text description. The loading view to be added with the text description is only self-encapsulated. The Code is as follows:

Loadingview. h

#import <UIKit/UIKit.h>@interface LoadingView : UIView{}+ (id)loadingViewInView:(UIView *)aSuperview;- (void)removeView;@end

Loadingview. m

#import "LoadingView.h"#import <QuartzCore/QuartzCore.h>//// NewPathWithRoundRect//// Creates a CGPathRect with a round rect of the given radius.//CGPathRef NewPathWithRoundRect(CGRect rect, CGFloat cornerRadius){//// Create the boundary path//CGMutablePathRef path = CGPathCreateMutable();CGPathMoveToPoint(path, NULL,rect.origin.x,rect.origin.y + rect.size.height - cornerRadius);// Top left cornerCGPathAddArcToPoint(path, NULL,rect.origin.x,rect.origin.y,rect.origin.x + rect.size.width,rect.origin.y,cornerRadius);// Top right cornerCGPathAddArcToPoint(path, NULL,rect.origin.x + rect.size.width,rect.origin.y,rect.origin.x + rect.size.width,rect.origin.y + rect.size.height,cornerRadius);// Bottom right cornerCGPathAddArcToPoint(path, NULL,rect.origin.x + rect.size.width,rect.origin.y + rect.size.height,rect.origin.x,rect.origin.y + rect.size.height,cornerRadius);// Bottom left cornerCGPathAddArcToPoint(path, NULL,rect.origin.x,rect.origin.y + rect.size.height,rect.origin.x,rect.origin.y,cornerRadius);// Close the path at the rounded rectCGPathCloseSubpath(path);return path;}@implementation LoadingView//// loadingViewInView://// Constructor for this view. Creates and adds a loading view for covering the// provided aSuperview.//// Parameters://    aSuperview - the superview that will be covered by the loading view//// returns the constructed view, already added as a subview of the aSuperview//(and hence retained by the superview)//+ (id)loadingViewInView:(UIView *)aSuperview{LoadingView *loadingView =[[[LoadingView alloc] initWithFrame:[aSuperview bounds]] autorelease];if (!loadingView){return nil;}loadingView.opaque = NO;loadingView.autoresizingMask =UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;[aSuperview addSubview:loadingView];const CGFloat DEFAULT_LABEL_WIDTH = 280.0;const CGFloat DEFAULT_LABEL_HEIGHT = 50.0;CGRect labelFrame = CGRectMake(0, 0, DEFAULT_LABEL_WIDTH, DEFAULT_LABEL_HEIGHT);UILabel *loadingLabel =[[[UILabel alloc]initWithFrame:labelFrame]autorelease];loadingLabel.text = NSLocalizedString(@"Loading...", nil);loadingLabel.textColor = [UIColor whiteColor];loadingLabel.backgroundColor = [UIColor clearColor];loadingLabel.textAlignment = UITextAlignmentCenter;loadingLabel.font = [UIFont boldSystemFontOfSize:[UIFont labelFontSize]];loadingLabel.autoresizingMask =UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin |UIViewAutoresizingFlexibleTopMargin |UIViewAutoresizingFlexibleBottomMargin;[loadingView addSubview:loadingLabel];UIActivityIndicatorView *activityIndicatorView =[[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]autorelease];[loadingView addSubview:activityIndicatorView];activityIndicatorView.autoresizingMask =UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleRightMargin |UIViewAutoresizingFlexibleTopMargin |UIViewAutoresizingFlexibleBottomMargin;[activityIndicatorView startAnimating];CGFloat totalHeight =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;CGRect activityIndicatorRect = 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;// Set up the fade-in animationCATransition *animation = [CATransition animation];[animation setType:kCATransitionFade];[[aSuperview layer] addAnimation:animation forKey:@"layerAnimation"];return loadingView;}//// removeView//// Animates the view out from the superview. As the view is removed from the// superview, it will be released.//- (void)removeView{UIView *aSuperview = [self superview];[super removeFromSuperview];// Set up the animationCATransition *animation = [CATransition animation];[animation setType:kCATransitionFade];[[aSuperview layer] addAnimation:animation forKey:@"layerAnimation"];}//// drawRect://// Draw the view.//- (void)drawRect:(CGRect)rect{rect.size.height -= 1;rect.size.width -= 1;const CGFloat RECT_PADDING = 8.0;rect = CGRectInset(rect, RECT_PADDING, RECT_PADDING);const CGFloat ROUND_RECT_CORNER_RADIUS = 5.0;CGPathRef roundRectPath = NewPathWithRoundRect(rect, ROUND_RECT_CORNER_RADIUS);CGContextRef context = UIGraphicsGetCurrentContext();const CGFloat BACKGROUND_OPACITY = 0.85;CGContextSetRGBFillColor(context, 0, 0, 0, BACKGROUND_OPACITY);CGContextAddPath(context, roundRectPath);CGContextFillPath(context);const CGFloat STROKE_OPACITY = 0.25;CGContextSetRGBStrokeColor(context, 1, 1, 1, STROKE_OPACITY);CGContextAddPath(context, roundRectPath);CGContextStrokePath(context);CGPathRelease(roundRectPath);}//// dealloc//// Release instance memory.//- (void)dealloc{    [super dealloc];}@end

Remember to add quartzcore. Framework to the project.

The usage is simpler. You only need to call:

LoadingView *loadingView = [LoadingView loadingViewInView:[self.view.window.subviews objectAtIndex:0]];

You only need to call:

[loadingView removeFromSuperview];

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.