iOS自定义一些提示控件
Posted isHakan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS自定义一些提示控件相关的知识,希望对你有一定的参考价值。
自己自定义了一些最常用的控件,效果如下。这些控件是非常简单的,但是在一般开发过程中,很多人都比较喜欢直接拉一个第三方来实现。(如在做加载控件时都喜欢使用MBProgressHUD库),不曾想就为了这一个小小的控件把人家的整份代码都copy过来。对于追求能自己写就尽量自己写的我来说,起码在使用加载控件时,我是不能忍受的。所以简简单单封装了一套:
代码如下:
.h中的代码:
// // HKUIToolsView.h // HKUIToolsDemo // // Created by isHakan on 2017/7/28. // Copyright © 2017年 liuhuakun. All rights reserved. // #import <UIKit/UIKit.h> @interface HKUIToolsView : UIView /*移除加载类型的view */ - (void)removeLoadViewFromSuperView; /* *@msg 输入的需要提示文字 *@typeNum 选择控件类型 /// 0代表中间方形文字提示框 1等待加载view 2底部条形文字提示框 **/ - (instancetype)initWithFrame:(CGRect)frame withMsg:(NSString *)msg andType:(NSInteger)typeNum; @end
.m中的代码:
// // HKUIToolsView.m // HKUIToolsDemo // // Created by isHakan on 2017/7/28. // Copyright © 2017年 liuhuakun. All rights reserved. // #import "HKUIToolsView.h" @interface HKUIToolsView () { NSTimer *_loadViewTimeoutTimer; } @end @implementation HKUIToolsView - (instancetype)initWithFrame:(CGRect)frame withMsg:(NSString *)msg andType:(NSInteger)typeNum { self = [super initWithFrame:frame]; if (self) { UIView *mainScreenBgView = [[UIView alloc] initWithFrame:frame]; mainScreenBgView.alpha = 0.3; mainScreenBgView.backgroundColor = [UIColor blackColor]; [self addSubview:mainScreenBgView]; [self createView:frame andMsg:msg andType:typeNum]; } return self; } //加载中和中间提示msg - (void)createView:(CGRect)frame andMsg:(NSString *)msg andType:(NSInteger)typeNum { CGFloat w,h,x,y; if (typeNum==0 || typeNum==1) { w = frame.size.width/3.0; h = w; x = (frame.size.width-w)/2.0; y = (frame.size.height-h)/2.0; } else { w = frame.size.width*0.6; h = 60.0; x = (frame.size.width-w)/2.0; y = frame.size.height-h-32.0; } UIView *centerMsgView = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)]; centerMsgView.backgroundColor = [UIColor blackColor]; centerMsgView.layer.masksToBounds = YES; centerMsgView.layer.cornerRadius = 8.0; [self addSubview:centerMsgView]; if (typeNum==0 || typeNum==2) { CGFloat label_x,label_y,label_w,label_h; label_x = label_y = 8; label_w = w-16; label_h = h-16; UILabel *msgLabel = [[UILabel alloc] initWithFrame:CGRectMake(label_x, label_y, label_w, label_h)]; msgLabel.backgroundColor = centerMsgView.backgroundColor; msgLabel.textColor = [UIColor whiteColor]; msgLabel.font = [UIFont systemFontOfSize:14]; msgLabel.text = msg; msgLabel.numberOfLines = 0; msgLabel.textAlignment = NSTextAlignmentCenter; [centerMsgView addSubview:msgLabel]; [UIView animateWithDuration:3.0 animations:^{ self.alpha=0; } completion:^(BOOL finished) { [self removeFromSuperview]; }]; } else//加载view { CGFloat actIndView_w = w-32; CGFloat actIndView_h = h-32; CGFloat actIndView_x = 16; CGFloat actIndView_y = 0; UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; activityIndicatorView.frame = CGRectMake(actIndView_x, actIndView_y, actIndView_w, actIndView_h); activityIndicatorView.backgroundColor = [UIColor blackColor]; activityIndicatorView.tag = 111; [activityIndicatorView startAnimating]; [centerMsgView addSubview:activityIndicatorView]; CGFloat acIndLabel_w,acIndLabel_h,acIndLabel_x,acIndLabel_y; acIndLabel_x = actIndView_x; acIndLabel_y = actIndView_h*2/3.0; acIndLabel_w = actIndView_w; acIndLabel_h = actIndView_h/3.0; UILabel *msgLabel = [[UILabel alloc] initWithFrame:CGRectMake(acIndLabel_x, acIndLabel_y, acIndLabel_w, acIndLabel_h)]; msgLabel.backgroundColor = [UIColor blackColor]; msgLabel.textColor = [UIColor whiteColor]; msgLabel.textAlignment = NSTextAlignmentCenter; msgLabel.text = msg; msgLabel.font = [UIFont systemFontOfSize:14.0]; [centerMsgView addSubview:msgLabel]; _loadViewTimeoutTimer = [NSTimer scheduledTimerWithTimeInterval:45.0 target:self selector:@selector(removeLoadViewFromSuperView) userInfo:nil repeats:NO]; } } //加载超时隐藏 - (void)removeLoadViewFromSuperView { if ([_loadViewTimeoutTimer isValid]) { [_loadViewTimeoutTimer invalidate]; } [UIView animateWithDuration:2.5 animations:^{ self.alpha=0; } completion:^(BOOL finished) { UIActivityIndicatorView *indicatorView = [self viewWithTag:111]; [indicatorView stopAnimating]; [self removeFromSuperview]; }]; } @end
在需要调用对应控件的界面处,
// // ViewController.m // HKUIToolsDemo // // Created by isHakan on 2017/7/28. // Copyright © 2017年 liuhuakun. All rights reserved. // #import "ViewController.h" #import "HKUIToolsView.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (IBAction)centerPointViewShowBtnClick:(UIButton *)sender { [self showToolMsg:@"中心提示信息显示!" viewTag:sender.tag]; } - (IBAction)loadViewShow:(UIButton *)sender { [self showToolMsg:@"加载中..." viewTag:sender.tag]; } - (IBAction)bottomPointViewShow:(UIButton *)sender { [self showToolMsg:@"底部提示信息提示!" viewTag:sender.tag]; } - (void)showToolMsg:(NSString *)msg viewTag:(NSInteger)viewTag { HKUIToolsView *toolsView = [[HKUIToolsView alloc] initWithFrame:[UIScreen mainScreen].bounds withMsg:msg andType:viewTag]; toolsView.tag = 222; [[UIApplication sharedApplication].keyWindow addSubview:toolsView]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
以上是关于iOS自定义一些提示控件的主要内容,如果未能解决你的问题,请参考以下文章