iOS-自定义TipsView (Swift + OC)版

//
//  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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
和MaterialShowcaseView一样,也是用于用户第一次使用App时的使用方法引导控件。但是不如前者功能完善,至少没有引导链。还有,就是导入我的demo时,别忘了修改SDK的本:android:targetSdkVersion="",改成你的,我用的是19项目地址:https://github.com/fredericojssilva/ShowTipsView 效果图:如何使用使用比较简单,你不需要修改原有的xml布局。比如我要对下列按钮添加引导:<Button         android:id="@ id/button1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentLeft="true"         android:layout_alignParentTop="true"         android:layout_marginLeft="58dp"         android:layout_marginTop="36dp"         android:text="Button" />java代码Button btn_one = (Button) findViewById(R.id.button1);          // ShowTipsView ShowTipsView showtips = new ShowTipsBuilder(this)                 .setTarget(btn_one).setTitle("A magnific button")                 .setDescription("This button do nothing very well")                 .setDelay(1000)//延时显示                 //.displayOneTime(2)//设置一个ID,整形的。如果设置了该属性,则只显示一次                 .build(); //showtips.setDisplayOneTime(true);//是否只显示一次 //showtips.setButtonText("我知道了");//设置按钮的文字,默认为"Got it" //设置回调 //showtips.setCallback(new ShowTipsViewInterface(){//当您点击"Got it"按钮后触发 //    @Override //    public void gotItClicked() {//设置回调让按钮2也产生引导,形成引导链 //  ShowTipsView showtips = new ShowTipsBuilder(MainActivity.this) //                .setTarget(btn_two).setTitle("A magnific button") //                .setDescription("This button do nothing very well") //                .setDelay(1000) //                .build(); //  showtips.show(MainActivity.this); //    }         // }); showtips.show(this);

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值