————-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的某某屬性*倍數 + 長度