//
// YHTipsView.swift
// 自定义TipsView
//
// Created by YHIOS002 on 16/12/21.
// Copyright © 2016年 YHSoft. All rights reserved.
//
import Foundation
import UIKit
import SnapKit
class YHTipsView : UIView{
var tips:String? = nil {
didSet{
self.tipsLabel.text = tips
}
}
var underView:UIView = {
let _underView = UIView()
_underView.backgroundColor = UIColor.black.withAlphaComponent(0.8)
_underView.layer.cornerRadius = 5
_underView.layer.masksToBounds = true
return _underView
}()
fileprivate lazy var tipsLabel:UILabel = {
let _tipsLabel = UILabel()
_tipsLabel.textColor = UIColor.white
_tipsLabel.numberOfLines = 0
_tipsLabel.textAlignment = .center
_tipsLabel.font = UIFont.systemFont(ofSize: 13.0)
return _tipsLabel
}()
// MARK: - init
override init(frame: CGRect) {
super.init(frame: frame)
setupUI()
layoutUI()
}
private func setupUI(){
isUserInteractionEnabled = true
addSubview(self.underView)
addSubview(self.tipsLabel)
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(YHTipsView.onTap))
addGestureRecognizer(tapGesture)
}
private func layoutUI(){
self.tipsLabel.snp.makeConstraints{ [unowned self] (make) -> Void in
make.center.equalTo(self)
make.height.greaterThanOrEqualTo(40)
make.width.lessThanOrEqualTo(SCREEN_WIDTH-30)
}
self.underView.snp.makeConstraints{ [unowned self] (make) -> Void in
make.top.bottom.equalTo(self.tipsLabel)
make.left.equalTo(self.tipsLabel.snp.left).offset(-5)
make.right.equalTo(self.tipsLabel.snp.right).offset(5)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Life
deinit {
NotificationCenter.default.removeObserver(self)
}
// MARK: - Action
func onTap() {
hide()
}
// MARK: - Public
open func showTouchHide(touchHide:Bool){
let window = UIApplication.shared.value(forKey: "_statusBarWindow") as? UIWindow
guard window != nil else {
DDLog("window is nil")
return
}
window?.windowLevel = UIWindowLevelStatusBar + 1.0
window?.addSubview(self)
self.snp.makeConstraints { (make) -> Void in
make.center.equalTo(window!)
make.width.equalTo(SCREEN_WIDTH)
make.height.equalTo(40)
}
if touchHide == false {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+2.5, execute: {
self.hide()
})
}
}
// MARK: - Private
private func hide(){
self.removeFromSuperview()
}
}
// OC 版
//
// YHTipsView.m
// 自定义TipsViewOC
//
// Created by YHIOS002 on 16/6/14.
// Copyright © 2016年 YHSoft. All rights reserved.
//
#import "YHTipsView.h"
@interface YHTipsView ()
@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) UILabel *tipsLabel;
@property (nonatomic, strong) UITapGestureRecognizer *tapGesture;
@end
@implementation YHTipsView
/*
* // Only override drawRect: if you perform custom drawing.
* // An empty implementation adversely affects performance during animation.
* - (void)drawRect:(CGRect)rect {
* // Drawing code
* }
*/
- (instancetype)init
{
if (self = [super init])
{
self.userInteractionEnabled = YES;
self.tipsLabel = [[UILabel alloc] init];
self.tipsLabel.textColor = [UIColor whiteColor];
self.tipsLabel.numberOfLines = 0;
self.tipsLabel.textAlignment = NSTextAlignmentCenter;
self.tipsLabel.font = [UIFont systemFontOfSize:13.0f];
self.underView = [[UIView alloc] init];
self.underView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8];
self.underView.layer.cornerRadius = 5;
self.underView.layer.masksToBounds = YES;
// self.underView.layer.borderColor = [UIColor whiteColor].CGColor;
// self.underView.layer.borderWidth = 1;
[self addSubview:self.underView];
[self addSubview:self.tipsLabel];
MyWeakSelf
[self.tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(ws);
make.height.mas_equalTo(40);
make.width.mas_lessThanOrEqualTo(SCREEN_WIDTH-15);
}];
[self.underView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.bottom.equalTo(ws.tipsLabel);
make.left.equalTo(ws.tipsLabel.mas_left).offset(-5);
make.right.equalTo(ws.tipsLabel.mas_right).offset(5);
}];
}
return self;
}
#pragma mark - Getter
- (UITapGestureRecognizer *)tapGesture{
if (!_tapGesture) {
_tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap)];
[self addGestureRecognizer:_tapGesture];
}
return _tapGesture;
}
- (void)onTap{
[self hide];
}
#pragma mark - Setter
- (void)setTips:(NSString *)tips
{
_tips = tips;
self.tipsLabel.text = _tips;
}
#pragma mark - Public
- (void)showTouchHide:(BOOL)touchHide{
UIWindow *statusBarWindow = [self statusBarWindow];
[statusBarWindow addSubview:self];
[self mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(statusBarWindow);
make.width.mas_equalTo(SCREEN_WIDTH);
make.height.mas_equalTo(40);
}];
if (touchHide)
{
if (!_tapGesture)
{
_tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTap)];
[self addGestureRecognizer:_tapGesture];
}
}
else
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self hide];
});
}
}
#pragma mark - Private
- (UIWindow *)statusBarWindow
{
return [[UIApplication sharedApplication] valueForKey:@"_statusBarWindow"];
}
- (void)hide
{
[self removeFromSuperview];
}
#pragma mark - Life
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end