iOS多个视图重叠,添加点击事件

看下我的需求,如下图所示:

image.png

相当于三个按钮,点击切换的感觉~~~

但是明显底图,头像,徽章。。。。。。都是切的图片。

你一看是不是觉得很简单,在底图上设置tap点击事件,底图的isUserInteractionEnabled设置为true,其他视图的isUserInteractionEnabled为false即可。

确实是这样,这样能实现整个选中区域的点击事件。

但是因为我这里底图imageView是放在了scollView上,所以上面那种实现方法并没有实现,具体原因就和响应事件,事件传递有关系了吧。

正在我无解不知道怎么解决的时候,有人告诉我任何视图放在button上,100%能获取点击事件。

那没办法啊,既然都没办法了,岂不是得试试嘛,说不定就解决了呢。

实现办法:

添加scrollView到父视图;
添加底图到scrollView上;
按钮btn添加到底图上;
其他的视图一次添加底图上;

demo代码:

//
//  ViewController4.swift
//  Test
//
//  Created by 彭思 on 2019/12/31.
//  Copyright © 2019 HaiDilao. All rights reserved.
//

import UIKit
import SnapKit

class ViewController4: UIViewController {

    lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        view.isUserInteractionEnabled = true
        return view
    }()
    
    lazy var btn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.backgroundColor = UIColor.orange
        btn.addTarget(self, action: #selector(tapedTitleLabel(_:)), for: .touchUpInside)
        return btn
    }()
//
//    lazy var greenView: UIView = {
//        let view = UIView()
//        view.backgroundColor = UIColor.green
//        return view
//    }()
//
//    lazy var yelloView: UIView = {
//        let view = UIView()
//        view.backgroundColor = UIColor.yellow
//        return view
//    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.text = "哈哈"
        label.backgroundColor = UIColor.blue
        label.textColor = UIColor.red
        return label
    }()
    
    lazy var redImgView: UIImageView = {
        let imageView = UIImageView()
        imageView.backgroundColor = UIColor.red
        imageView.contentMode = .scaleAspectFit
        imageView.isUserInteractionEnabled = true
        return imageView
    }()
    
    lazy var yelloImgView: UIImageView = {
        let imageView = UIImageView()
        imageView.backgroundColor = UIColor.yellow
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()
    
    lazy var greenImgView: UIImageView = {
        let imageView = UIImageView()
        imageView.backgroundColor = UIColor.green
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()
    
    private lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.showsHorizontalScrollIndicator = false
        scrollView.scrollsToTop = false
        scrollView.backgroundColor = UIColor.gray
        return scrollView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        self.view.addSubview(scrollView)
        scrollView.addSubview(bgView)
        bgView.addSubview(btn)
        self.btn.addSubview(redImgView)
        self.redImgView.addSubview(greenImgView)
        self.redImgView.addSubview(yelloImgView)
        self.yelloImgView.addSubview(titleLabel)
        
        
//        let tapGes = UITapGestureRecognizer(target: self, action: #selector(tapedTitleLabel(_:)))
//        self.bgView.addGestureRecognizer(tapGes)
        self.scrollView.canCancelContentTouches = true
        self.scrollView.delaysContentTouches = false
        
        scrollView.snp.makeConstraints { (make) in
            make.center.equalTo(self.view)
            make.width.equalTo(200)
            make.height.equalTo(100)
        }
        bgView.snp.makeConstraints { (make) in
            make.left.top.equalTo(scrollView)
            make.width.equalTo(200)
            make.height.equalTo(100)
        }
        btn.snp.makeConstraints { (make) in
            make.edges.equalTo(bgView)
        }
        redImgView.snp.makeConstraints { (make) in
            make.edges.equalTo(scrollView)
        }
        greenImgView.snp.makeConstraints { (make) in
            make.centerY.equalTo(self.redImgView)
            make.left.equalTo(20)
            make.width.equalTo(50)
            make.height.equalTo(50)
        }
        yelloImgView.snp.makeConstraints { (make) in
            make.left.equalTo(greenImgView.snp.right).offset(20)
            make.centerY.equalTo(self.redImgView)
            make.width.equalTo(100)
            make.height.equalTo(50)
        }
        titleLabel.snp.makeConstraints { (make) in
            make.edges.equalTo(yelloImgView).inset(10)
        }
    }
    
    @objc private func tapedTitleLabel(_ tapGes : UITapGestureRecognizer) {
        print("我点击了。。。")
    }
}

参考链接:

https://blog.csdn.net/potato512/article/details/77744658 (表示试了无效,可能是我没get到他的点)

你可能感兴趣的:(iOS多个视图重叠,添加点击事件)