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
- iOS 简单的loading弹出框实现
- iOS Swift 简单实现Loading动画
- IOS简单实现输入框随着键盘的弹出动态上升(很简单的)
- Android仿IOS 弹出框的实现
- IOS内购时的Loading实现-MBProgressHUD
- ios开发之简单实现loading动画效果
- ios开发之简单实现loading动画效果
- SVProgressHUD简单用法,非常简单(实现简单优美的 loading)
- 实现简单的弹出层
- iOS简单的键盘弹出输入框上移
- iOS 简单的按钮弹出效果
- 一个简单的loading
- IOS自定义请求uiwebview的loading框
- 代码录播:jQueryMobile 实现一个简单的弹出框效果
- jquery实现一个简单的弹出框(www.lnctime.com)
- thinkPHP删除前弹出确认框的简单实现方法
- ios实现支持通讯录的手机号码输入弹出框
- IOS-弹出框实现 官方UIAlertView的动画
- 百度面试题
- ubuntu下SVN安装与使用
- springmvc简单实现权限控制
- ARM映像文件
- KAFKA安装和使用
- iOS 简单的loading弹出框实现
- java图片压缩
- SSM框架——详细整合教程(Spring+SpringMVC+MyBatis)
- 网站营销知识:解析CPA、CPC、CPS、CPM广告术语
- 词向量
- 小马哥--高仿红米note m8207主板 2015新版单卡4G 详细拆机主板图与开机识别图 主板摄像头出双排线
- ios-day05-02(ScrollView的常见属性:contentSize、contentInset、contentOffset 以及其他属性)
- git的学习
- 用Markdown写一本自己的开源电子书