在第一次安装App, 常常会出现一个引导界面,实现引导页部分透明,部分蒙层的引导, 网上也没有很好的实现demo,就用消息响应链写了一个很简单的例子,可以实现功能,供大家参考。
1.实现引导页代码
1.1 定义一个热区视图。
var lucencyView:UIView = {
let view = UIView()
view.backgroundColor = .green
view.isUserInteractionEnabled = false
return view
}()
1.2 消息过滤
在热区内的消息都丢弃到上一级响应者处理,非热区的消息都自己处理掉。
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
if self.lucencyView.frame.contains(point) {
return false
}else{
return true
}
}
2. 引导页都继承于GuideView
引导页实现之后,就可以实现引导页的功能了。
3.GuideView的源码
//
// GuideView.swift
// GuideDemo
//
// Created by liuyinghui on 2020/4/9.
// Copyright © 2020 langnuo. All rights reserved.
//
import UIKit
class GuideView: UIView {
var lucencyView:UIView = {
let view = UIView()
view.backgroundColor = .green
view.isUserInteractionEnabled = false
return view
}()
override init(frame: CGRect) {
super.init(frame: frame)
self.addSubview(lucencyView)
lucencyView.frame = CGRect.init(x: 100, y: 100, width: 100, height: 100)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
print("引导页 \(String(describing: touches.first?.location(in: self)))")
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
print("引导页 \(String(describing: touches.first?.location(in: self)))")
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
print("引导页 \(String(describing: touches.first?.location(in: self)))")
}
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
if self.lucencyView.frame.contains(point) {
return false
}else{
return true
}
}
}
Demo源码:https://download.youkuaiyun.com/download/liuyinghui523/12324732