Swift实现滑动和点击切换控制器

2020-07-06  本文已影响0人  玉思盈蝶

效果如下:

image.png

代码如下:

//
//  TestViewController2.swift
//  Swift_shop
//
//  Created by 彭思 on 2020/7/5.
//  Copyright © 2020 HaiDilao. All rights reserved.
//

import UIKit
import SnapKit

class TestViewController2: UIViewController {

    lazy var firstBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = UIColor.white
        button.setTitle("选择锅型", for: .normal)
        button.setTitleColor(UIColor.gray, for: .normal)
        button.setTitleColor(UIColor.red, for: .selected)
        button.tag = 10000
        button.isSelected = true
        button.addTarget(self, action: #selector(btnClick(button:)), for: .touchUpInside)
        return button
    }()
    
    lazy var secondBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = UIColor.white
        button.setTitle("选择口味", for: .normal)
        button.setTitleColor(UIColor.gray, for: .normal)
        button.setTitleColor(UIColor.red, for: .selected)
        button.tag = 10001
        button.addTarget(self, action: #selector(btnClick(button:)), for: .touchUpInside)
        return button
    }()
    
    lazy var threeBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = UIColor.white
        button.setTitle("调整位置", for: .normal)
        button.setTitleColor(UIColor.gray, for: .normal)
        button.setTitleColor(UIColor.red, for: .selected)
        button.tag = 10002
        button.addTarget(self, action: #selector(btnClick(button:)), for: .touchUpInside)
        return button
    }()
    
    lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView(frame: CGRect.zero)
        scrollView.delegate = self
        scrollView.bounces = true
        scrollView.isPagingEnabled = true
        return scrollView
    }()
    
    var vc1 = ViewController3()
    var vc2 = ViewController4()
    var vc3 = ViewController5()
    var stackView:UIStackView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(firstBtn)
        view.addSubview(secondBtn)
        view.addSubview(threeBtn)
        view.addSubview(scrollView)
//        firstBtn.snp.makeConstraints { (make) in
//            make.left.equalTo(view).offset(20)
//            make.top.equalTo(20)
//            make.width.equalTo(100)
//            make.height.equalTo(30)
//        }
//        secondBtn.snp.makeConstraints { (make) in
//            make.left.equalTo(firstBtn.snp.right).offset(50)
//            make.top.width.height.equalTo(firstBtn)
//        }
//       threeBtn.snp.makeConstraints { (make) in
//            make.left.equalTo(secondBtn.snp.right).offset(50)
//            make.top.width.height.equalTo(firstBtn)
//        }
        
        stackView = UIStackView(arrangedSubviews: [firstBtn, secondBtn, threeBtn])
        stackView.frame = CGRect(x: 10, y: 0, width: kScreenW - 20, height: 50)
        //设置UIStackView内View的排列:横向或纵向
        stackView.axis = NSLayoutConstraint.Axis.horizontal
        //设置UIStackView内View对齐方式
        stackView.alignment = UIStackView.Alignment.fill
        //设置UIStackView内View沿axis排列的布局规则
        stackView.distribution = UIStackView.Distribution.fillEqually
        //设置UIStackView内View之间的间距
        stackView.spacing = 20
        //设置UIStackView背景颜色
        stackView.backgroundColor = UIColor.blue
        //添加到视图中
        self.view.addSubview(stackView)
        
        scrollView.snp.makeConstraints { (make) in
            make.top.equalTo(stackView.snp.bottom).offset(5)
            make.left.right.bottom.equalTo(view)
        }
        vc1.view.frame = CGRect(x: 0, y: 0, width: kScreenW, height: kScreenH - 50)
        vc2.view.frame = CGRect(x: kScreenW, y: 0, width: kScreenW, height: kScreenH - 50)
        vc3.view.frame = CGRect(x: kScreenW * 2, y: 0, width: kScreenW, height: kScreenH - 50)
        scrollView.addSubview(vc1.view)
        scrollView.addSubview(vc2.view)
        scrollView.addSubview(vc3.view)
        scrollView.contentSize = CGSize(width: kScreenW * 3, height: kScreenH)
        
    }
    
    @objc func btnClick(button: UIButton) {
        if button.tag == 10000 {
            scrollView.contentOffset = CGPoint(x: 0, y: 0)
            firstBtn.isSelected = true
            secondBtn.isSelected = false
            threeBtn.isSelected = false
        } else if button.tag == 10001 {
            scrollView.contentOffset = CGPoint(x: kScreenW, y: 0)
            firstBtn.isSelected = false
            secondBtn.isSelected = true
            threeBtn.isSelected = false
        } else {
            scrollView.contentOffset = CGPoint(x: kScreenW * 2, y: 0)
            firstBtn.isSelected = false
            secondBtn.isSelected = false
            threeBtn.isSelected = true
        }
    }
}

extension TestViewController2: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let currentPage = Int(scrollView.contentOffset.x / kScreenW)
        print(currentPage)
        switch currentPage {
        case 0:
            firstBtn.isSelected = true
            secondBtn.isSelected = false
            threeBtn.isSelected = false
        case 1:
            firstBtn.isSelected = false
            secondBtn.isSelected = true
            threeBtn.isSelected = false
        case 2:
            firstBtn.isSelected = false
            secondBtn.isSelected = false
            threeBtn.isSelected = true
        default:
            break
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读