iOS 简单的loading弹出框实现

来源:互联网 发布:淘宝图片如何加护盾 编辑:程序博客网 时间:2024/05/14 15:52

————-LoadingAlerter.h———–

////  LoadingAlerter.h//  SdkModle////  Created by Sean on 15/2/10.//  Copyright (c) 2015年 Feiyu. All rights reserved.//#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>@interface LoadingAlerter : NSObject- (id) initWithParentView:(UIView*)parentView;- (void) startAlert;- (void) stopAlert;@end

——LoadingAlerter.m——

////  LoadingAlerter.m//  SdkModle////  Created by Sean on 15/2/10.//  Copyright (c) 2015年 Feiyu. All rights reserved.//#import "LoadingAlerter.h"@interface LoadingAlerter()@property (nonatomic) UIActivityIndicatorView* indicatorWaiting;@property (nonatomic) UIView* viewAlert;@property (nonatomic) UIView* viewParent;@property (nonatomic) UILabel* labWaiting;@property (copy, nonatomic) NSString* strWaiting;@property (nonatomic) BOOL isStart;@end@implementation LoadingAlerter//初始化//parentView 父view- (id) initWithParentView:(UIView*)parentView {    if(self = [super init]) {        self.viewParent = parentView;        self.isStart = NO;    }    return self;}//弹出框弹出- (void) startAlert {    if (self.isStart == YES) {        return;    }    //-----设置弹出框-----    self.viewAlert = [[UIView alloc] init];    //设置颜色    [self.viewAlert setBackgroundColor:[UIColor blackColor]];    //设置透明度    [self.viewAlert setAlpha:0.6];    //设置圆角    self.viewAlert.layer.cornerRadius = 10;    //填充图片时,如果加圆角,图片会超出圆角框,即还是直角,得加一句masksToBounds    self.viewAlert.layer.masksToBounds = YES;    //设置边框的宽度,当然可以不要    self.viewAlert.layer.borderWidth = 0;    //设置边框的颜色    self.viewAlert.layer.borderColor = [[UIColor grayColor] CGColor];    //防止了控件布局与自动布局冲突    [self.viewAlert setTranslatesAutoresizingMaskIntoConstraints:NO];    //将弹出框加入到父view    [self.viewParent addSubview:self.viewAlert];    //给弹出框添加约束,涉及到自动布局的知识;    //给控件添加约束之前一定先要将其添加到父控件上,不然会报异常    [self.viewAlert addConstraint:[NSLayoutConstraint constraintWithItem:self.viewAlert        attribute:NSLayoutAttributeHeight        relatedBy:NSLayoutRelationEqual        toItem:nil        attribute:NSLayoutAttributeHeight        multiplier:1        constant:80]];    [self.viewAlert addConstraint:[NSLayoutConstraint constraintWithItem:self.viewAlert        attribute:NSLayoutAttributeWidth        relatedBy:NSLayoutRelationEqual        toItem:nil        attribute:NSLayoutAttributeWidth        multiplier:1        constant:200]];    [self.viewParent addConstraint:[NSLayoutConstraint constraintWithItem:self.viewAlert        attribute:NSLayoutAttributeCenterY           relatedBy:NSLayoutRelationLessThanOrEqual        toItem:self.viewParent        attribute:NSLayoutAttributeCenterY        multiplier:1        constant:0]];    [self.viewParent addConstraint:[NSLayoutConstraint constraintWithItem:self.viewAlert        attribute:NSLayoutAttributeCenterX        relatedBy:NSLayoutRelationLessThanOrEqual        toItem:self.viewParent        attribute:NSLayoutAttributeCenterX        multiplier:1        constant:0]];    //-----设置指示器-----    self.indicatorWaiting =  [[UIActivityIndicatorView alloc] init];    [self.indicatorWaiting setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];    [self.indicatorWaiting setTranslatesAutoresizingMaskIntoConstraints:NO];    [self.viewAlert addSubview:self.indicatorWaiting];    [self.viewAlert addConstraint:[NSLayoutConstraint constraintWithItem:self.indicatorWaiting        attribute:NSLayoutAttributeTop        relatedBy:NSLayoutRelationLessThanOrEqual        toItem:self.viewAlert        attribute:NSLayoutAttributeTop        multiplier:1        constant:40]];    [self.viewAlert addConstraint:[NSLayoutConstraint constraintWithItem:self.indicatorWaiting        attribute:NSLayoutAttributeCenterX        relatedBy:NSLayoutRelationLessThanOrEqual        toItem:self.viewAlert        attribute:NSLayoutAttributeCenterX        multiplier:1        constant:0]];    //-----设置文本框-----    self.strWaiting = @"正在努力加载中...";    self.labWaiting = [[UILabel alloc] init];    self.labWaiting.text = self.strWaiting;    self.labWaiting.textAlignment = NSTextAlignmentCenter;    self.labWaiting.font = [UIFont fontWithName:@"Helvetica" size:14];    [self.labWaiting setTextColor:[UIColor whiteColor]];    [self.labWaiting setTranslatesAutoresizingMaskIntoConstraints:NO];    [self.viewAlert addSubview:self.labWaiting];    [self.viewAlert addConstraint:[NSLayoutConstraint constraintWithItem:self.labWaiting        attribute:NSLayoutAttributeTop        relatedBy:NSLayoutRelationLessThanOrEqual        toItem:self.viewAlert        attribute:NSLayoutAttributeTop        multiplier:1        constant:20]];    [self.viewAlert addConstraint:[NSLayoutConstraint constraintWithItem:self.labWaiting        attribute:NSLayoutAttributeCenterX        relatedBy:NSLayoutRelationLessThanOrEqual        toItem:self.viewAlert        attribute:NSLayoutAttributeCenterX        multiplier:1        constant:0]];    //开始动画    [self.indicatorWaiting startAnimating];    self.isStart = YES;}//弹出框收起- (void) stopAlert {    //停止动画    [self.indicatorWaiting stopAnimating];    //将弹出框从父view删除    [self.viewAlert removeFromSuperview];    self.isStart = NO;}@end

使用方式
1.初始化的时候

self.loadingAlert = [[LoadingAlerter alloc] initWithParentView:self.view];

2.开始加载的时候

[self.loadingAlert startAlert];

3.结束加载的时候

[self.loadingAlert stopAlert];

附:自动布局的用法
1.要给控件加约束,要先找到它的父控件,将约束添加到齐父控件上

[self.viewAlert addConstraint:[NSLayoutConstraint                 constraintWithItem:self.indicatorWaiting                                                               attribute:NSLayoutAttributeTop                                                               relatedBy:NSLayoutRelationLessThanOrEqual                                                                  toItem:self.viewAlert                                                               attribute:NSLayoutAttributeTop                                                              multiplier:1                                                                constant:40]];

上面代码给指示器indicatorWaiting添加约束。

2.NSLayoutConstraint的constraintWithItem方法说明

+ (instancetype)constraintWithItem:(id)view1                         attribute:(NSLayoutAttribute)attr1                         relatedBy:(NSLayoutRelation)relation                            toItem:(id)view2                         attribute:(NSLayoutAttribute)attr2                        multiplier:(CGFloat)multiplier                          constant:(CGFloat)c

官方说明:
Create constraints explicitly. Constraints are of the form “view1.attr1 = view2.attr2 * multiplier + constant”
If your equation does not have a second view and attribute, use nil and NSLayoutAttributeNotAnAttribute.

意思是说view1的某某属性的值 = view2的某某属性*倍数 + 长度

0 0
原创粉丝点击