码迷,mamicode.com
首页 > 移动开发 > 详细

iOS自定义一些提示控件

时间:2017-07-28 18:21:32      阅读:279      评论:0      收藏:0      [点我收藏+]

标签: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自定义一些提示控件

标签:nbsp   app   code   schedule   ext   ini   void   ==   back   

原文地址:http://www.cnblogs.com/liuhuakun/p/7251691.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!