swift 详情页面,和自定义tab按钮

09/17 08:19
阅读数 22

 

//
//  DetailViewController.swift
//  geekTime
//
//  Created by liuan on 2020/9/16.
//  Copyright © 2020 liuan. All rights reserved.
//

import UIKit
import SnapKit
import Kingfisher

class DetailViewController: BaseViewController {
    var product:Product!
    var avatarView:UIImageView!
    var nameLabel:UILabel!
    var descLabel:UILabel!
    var teacherLabel:UILabel!
    var courseCountLabel:UILabel!
    var studentCountLabel:UILabel!
    var tab: Tab!
   

    override func viewDidLoad() {
        super.viewDidLoad()
        
        title = "详情"
        createTop()
        createMiddle()
        createBottom()


    }
    
    func createTop(){
        let topView = UIView()
             topView.layer.contents = R.image.detailBg()?.cgImage
             let blurEffect = UIBlurEffect(style: .dark)
             let blurView = UIVisualEffectView(effect: blurEffect)
             blurView.layer.masksToBounds = true
             blurView.alpha = 0.7
             view.addSubview(topView)
             topView.addSubview(blurView)
             blurView.snp.makeConstraints { (make) in
                 make.left.right.top.equalToSuperview()
                 make.height.equalTo(200)
             }
             topView.snp.makeConstraints { (make) in
                 make.left.right.top.equalToSuperview()
                 make.height.equalTo(200)
             }
             
             avatarView = UIImageView()
             let round = RoundCornerImageProcessor(cornerRadius: 10)
             avatarView.kf.setImage(with: URL(string: product.imageUrl), placeholder: nil, options: [.processor(round)])
             topView.addSubview(avatarView)
             avatarView.snp.makeConstraints { (make) in
                 make.left.top.equalTo(topView).offset(20)
                 make.width.equalTo(82)
                 make.height.equalTo(108)
             }
             
             nameLabel = UILabel(frame: .zero)
             nameLabel.textColor = .white
             nameLabel.font = UIFont.boldSystemFont(ofSize: 18)
             nameLabel.text = product.name
             topView.addSubview(nameLabel)
             nameLabel.snp.makeConstraints { (make) in
                 make.left.equalTo(avatarView.snp_right).offset(10)
                 make.top.equalTo(avatarView)
                 make.right.equalTo(topView).offset(-15)
             }
             
             descLabel = UILabel(frame: .zero)
             descLabel.textColor = .white
             descLabel.font = UIFont.systemFont(ofSize: 14)
             descLabel.text = product.desc
             descLabel.numberOfLines = 2
             topView.addSubview(descLabel)
             descLabel.snp.makeConstraints { (make) in
                 make.left.equalTo(avatarView.snp_right).offset(10)
                 make.top.equalTo(nameLabel.snp_bottom).offset(5)
                 make.right.equalTo(topView).offset(-15)
             }
             
             teacherLabel = UILabel(frame: .zero)
             teacherLabel.textColor = .white
             teacherLabel.font = UIFont.systemFont(ofSize: 14)
             teacherLabel.text = "讲师:\(product.teacher)"
             topView.addSubview(teacherLabel)
             teacherLabel.snp.makeConstraints { (make) in
                 make.left.equalTo(avatarView.snp_right).offset(10)
                 make.bottom.equalTo(avatarView)
                 make.right.equalTo(topView).offset(-15)
             }
             
             
             let bookAttachment = NSTextAttachment()
             bookAttachment.image = R.image.book()
             bookAttachment.bounds = CGRect(x: -2, y: -3, width: bookAttachment.image?.size.width ?? 0, height: bookAttachment.image?.size.height ?? 0)
             let bookAttachmentString = NSAttributedString(attachment: bookAttachment)
             let courseCountString = NSMutableAttributedString(string: "")
             courseCountString.append(bookAttachmentString)
             
             let courseCountStringAfterIcon = NSAttributedString(string: " 共 \(product.total) 讲 更新至 \(product.update) 讲")
             courseCountString.append(courseCountStringAfterIcon)
             
             courseCountLabel = UILabel(frame: .zero)
             courseCountLabel.textColor = .white
             courseCountLabel.font = UIFont.systemFont(ofSize: 14)
             courseCountLabel.attributedText = courseCountString
             topView.addSubview(courseCountLabel)
             courseCountLabel.snp.makeConstraints { (make) in
                 make.left.equalTo(topView).offset(20)
                 make.bottom.equalTo(topView).offset(-15)
             }
             
             
             let studentAttachment = NSTextAttachment()
             studentAttachment.image = R.image.student()
             studentAttachment.bounds = CGRect(x: -2, y: -3, width: studentAttachment.image?.size.width ?? 0, height: studentAttachment.image?.size.height ?? 0)
             let studentAttachmentString = NSAttributedString(attachment: studentAttachment)
             let studentCountString = NSMutableAttributedString(string: "")
             studentCountString.append(studentAttachmentString)
             
             let studentCountStringAfterIcon = NSAttributedString(string: " 共 \(product.studentCount) 人学习")
             studentCountString.append(studentCountStringAfterIcon)
             
             studentCountLabel = UILabel(frame: .zero)
             studentCountLabel.textColor = .white
             studentCountLabel.font = UIFont.systemFont(ofSize: 14)
             studentCountLabel.attributedText = studentCountString
             topView.addSubview(studentCountLabel)
             studentCountLabel.snp.makeConstraints { (make) in
                 make.right.equalTo(topView).offset(-20)
                 make.bottom.equalTo(topView).offset(-15)
             }
    }

    private func createMiddle() {
        tab = Tab(items: ["课程介绍", "课程目录"])
        view.addSubview(tab)
        tab.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(200)
            make.height.equalTo(50)
        }
    }
    func createBottom(){
        let button = UIButton(type: .custom)
        button.setTitleColor(.white, for: .normal)
        button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
        button.setTitle("购买¥\(product.price)", for: .normal)
        button.setBackgroundImage(UIColor.hexColor(0xf8892).toImage(), for: .normal)
        button.addTarget(self, action: #selector(didClickBuyButton), for: .touchUpInside)
        view.addSubview(button)
        button.snp.makeConstraints({make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(50)
            
        })
    }
    @objc func didClickBuyButton(){
        let loginVC = LoginViewController()
         navigationController?.pushViewController(loginVC, animated: true)
    }

}
//
//  Tab.swift
//  geekTime
//
//  Created by liuan on 2020/9/16.
//  Copyright © 2020 liuan. All rights reserved.
//

import Foundation
import UIKit
import SnapKit
class Tab: UIView {
    var items:[String]
    var itemButtons:[UIButton]
    var selectedItemButton:UIButton!
    
    var indicatorView:UIView!
    var selectedColor:UIColor?{
        didSet{
            if let color = self.selectedColor{
                self.indicatorView.backgroundColor = color
                itemButtons.forEach{button in
                    button.setTitleColor(color, for: .selected)
                }
            }else{
                self.indicatorView.backgroundColor = UIColor.hexColor(0xf8892e)
                itemButtons.forEach{button in
                    button.setTitleColor(UIColor.hexColor(0xf8892e), for: .selected)
                }
            }
            
        }
    }
    var normalColor:UIColor?
    
    init?(items: [String]) {
        if items.count==0{
            return nil
        }
        //初始化
        self.items = items
        itemButtons = []
        super.init(frame:.zero)
        createViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func createViews()  {
        var lastView:UIView?
        for index in 0..<items.count{
            let button = UIButton(type: .custom)
            button.setTitle(items[index], for: .normal)
            button.setTitleColor(UIColor.hexColor(0x333333), for: .normal)
            button.setTitleColor(UIColor.hexColor(0xf8892e), for: .selected)
            self.addSubview(button)
            if index == 0 {
                selectedItemButton = button
            }
            button.snp.makeConstraints({make in
                if index == 0 {
                    make.left.equalToSuperview()
                }else{
                    make.left.equalTo(lastView!.snp.right)
                    make.width.equalTo(lastView!)
                    
                }
                
                make.top.bottom.equalToSuperview()
                if index==items.count-1{
                    make.right.equalToSuperview()
                }
                
            })
            lastView = button
            button.addTarget(self, action: #selector(didClickButton(_:)), for: .touchUpInside)
            itemButtons.append(button)
        }
        indicatorView = UIView()
        indicatorView.backgroundColor = UIColor.hexColor(0xf8892e)
        self.addSubview(indicatorView)
        indicatorView.snp.makeConstraints({make in
            make.centerX.equalTo(selectedItemButton)
            make.bottom.equalToSuperview()
            make.width.equalTo(80)
            make.height.equalTo(4)
        })
        
        
        
    } 
    
    @objc func didClickButton(_ sender:UIButton){
        guard sender != selectedItemButton else {
            return
        }
        selectedItemButton.isSelected = false
        sender.isSelected = true
        selectedItemButton = sender
        UIView.animate(withDuration: 3, animations: {
            self.indicatorView.snp.remakeConstraints({make in
                make.centerX.equalTo(self.selectedItemButton)
                make.bottom.equalToSuperview()
                make.width.equalTo(80)
                make.height.equalTo(4)
            })
        })
        UIView.animate(withDuration: 0.25) {
                  self.layoutIfNeeded()
              }
    }
    
}

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
在线直播报名
返回顶部
顶部