iOS自訂一些提示控制項

來源:互聯網
上載者:User

標籤:nbsp   app   code   schedule   ext   ini   void   ==   back   

代碼如下:

 

.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:2.5 animations:^{            self.alpha = 0;            [NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(removeSlef:) userInfo:nil repeats:NO];        }];            }    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:8                                                                 target:self                                                               selector:@selector(removeLoadViewFromSuperView) userInfo:nil                                                                repeats:NO];    }    }
- (void)removeSlef:(NSTimer *)timer{    [timer invalidate];    [self removeFromSuperview];}//載入逾時隱藏- (void)removeLoadViewFromSuperView{    if ([_loadViewTimeoutTimer isValid])    {        [_loadViewTimeoutTimer invalidate];    }        [UIView animateWithDuration:1.5 animations:^{        self.alpha = 0;        [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(removeLoadView:) userInfo:nil repeats:NO];    }];}- (void)removeLoadView:(NSTimer *)timer{    [timer invalidate];    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自訂一些提示控制項

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.