IOS开发(19)IOS 自定义UIView实现请求等待控件(模态效果)

自定义UIView主要内容就是重写几个继承下来的几个方法,把自己想要的东西放进去,布局好即可,由于我这个控件比较简单,所以我没有自定义模型数据。
以下代码拷贝到项目中,调用运行即可。

WITLoadingView.h

    //
    //  WITLoadingView.h
    //  UIActivityIndicatorViewTest
    //
    //  Created by witsystem on 2018/3/29.
    //  Copyright © 2018年 witsystem. All rights reserved.
    //

    #import <UIKit/UIKit.h>

    @interface WITLoadingView : UIView

    @end

WITLoadingView.m

    //
    //  WITLoadingView.m
    //  UIActivityIndicatorViewTest
    //
    //  Created by witsystem on 2018/3/29.
    //  Copyright © 2018年 witsystem. All rights reserved.
    //

    #import "WITLoadingView.h"

    @interface WITLoadingView ()

    @property (nonatomic, weak) UIActivityIndicatorView *activityIndicatorView;
    @property (nonatomic, weak) UILabel *markLabel;

    @end

    @implementation WITLoadingView

    -(instancetype)initWithFrame:(CGRect)frame {

        if (self = [super initWithFrame:frame]) {
            UIActivityIndicatorView *aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleGray];
            self.activityIndicatorView = aiv;
            [aiv startAnimating];
            [self addSubview:aiv];

            UILabel *label = [[UILabel alloc] init];
            label.text = @"正在加载请稍后...";
            self.markLabel = label;
            [self addSubview: label];
            self.markLabel.textColor = [UIColor grayColor];
            self.backgroundColor = [UIColor colorWithRed: 240/255.0 green:240/255.0 blue: 240/255.0 alpha: 0.5];

        }
        return self;
    }

    -(void)layoutSubviews {

        [super layoutSubviews];

        CGFloat mlW = 120;
        CGFloat mlH = 20;
        CGFloat aivW = 20;
        CGFloat aivH = 20;

        CGFloat parentW = [UIScreen mainScreen].bounds.size.width;
        CGFloat parentH = [UIScreen mainScreen].bounds.size.height;

        //控件间隔宽度
        CGFloat intervalW = 10;

        CGFloat aivX = parentW * 0.5 - mlW * 0.5 - aivW * 0.5;
        CGFloat aivY = parentH * 0.5 - aivW * 0.5;

        CGFloat mlX = aivX + aivW + intervalW;
        CGFloat mlY = aivY;

        self.activityIndicatorView.frame = CGRectMake(aivX, aivY, aivW, aivH);
        self.markLabel.frame = CGRectMake(mlX, mlY, mlW, mlH);

        self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);

    }

    @end

调用方法

    #import "WITLoadingView.h"
    WITLoadingView *loadingView = [[WITLoadingView alloc] init];
    [self.view.window addSubview: loadingView];

隐藏等待控件

    [loadingView removeFromSuperview];
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值