UITableview 設置組圓角,以及最後或首行去線處理

效果

調用方法

        cell.setLocationState(items: items,
                              indexPathRow: indexPath.row)

操作

讓cell 遵循協議,並重寫協議方法

extension MQUnscrambleCell: ViewLocationStateProtocl {
    // 重寫設置位置狀態方法
    public func setLocationState(_ locationState: ViewLocationState) {
        // 最後一行去線
        lineV.isHidden = locationState.isLast
        // 設置組圓角
        bgView.setCorner(locationState, cornerRadius: 50)
    }
}

新建文件CellLocationState.swift,並寫入複製以下代碼到文件中

//
//  CellLocationState.swift
//  MQBaseComponents
//
//  Created by 消搖 on 2019/7/20.
//  Copyright © 2019 消搖. All rights reserved.
//

import Foundation
import  UIKit

/// 視圖位置狀態
public enum ViewLocationState {
    case non // 未定義
    case onlyOne // 只有一個
    case begin // 開頭
    case center // 中間
    case end // 結尾
    
    public init() {
        self = .non
    }
}

extension ViewLocationState {
    public var isFirst: Bool {
        return self == .onlyOne || self == .begin
    }
    
    public var isLast: Bool {
        return self == .onlyOne || self == .end
    }
}

extension ViewLocationState {
    // 通過item及indexPathRow獲取cell的位置狀態
    static func state<T>(items: [T],
                         indexPathRow: Int) -> ViewLocationState {
        guard items.count > 0 else { return .non }
        if items.count == 1 {
            return .onlyOne
        }
        
        if indexPathRow == 0 {
            return .begin
        }
        
        if indexPathRow == items.count - 1 {
            return .end
        }
        return .center
    }
}

/// 位置狀態協議
public protocol ViewLocationStateProtocl {
    func setLocationState(_ locationState: ViewLocationState)
}

extension ViewLocationStateProtocl {
    /// 調用該方法設置view的位置狀態
    public func setLocationState<T>(items: [T],
                                    indexPathRow: Int) {
        let locationState = ViewLocationState
            .state(items: items,
                   indexPathRow: indexPathRow)
        setLocationState(locationState)
    }
    
    /// 設置默認實現
    public func setLocationState(_ locationState: ViewLocationState) {
        print("重寫此方法 setLocationState == \(locationState)")
    }
}

/// 給view擴展設置部分角是原角的方法
extension UIView {
    
    /// 設置圓角
    ///
    /// - Parameters:
    ///   - locationState: 位置狀態
    ///   - cornerRadius: 圓角半徑
    public func setCorner(_ locationState: ViewLocationState,
                          cornerRadius: CGFloat = 5) {
        // 相對佈局需要刷新UI
        layoutIfNeeded()
        setNeedsLayout()
        var maskedCorners: CACornerMask
        var rectCorner: UIRectCorner
        switch locationState {
        case .onlyOne:
            maskedCorners = CACornerMask(
                arrayLiteral: CACornerMask.layerMaxXMinYCorner,
                CACornerMask.layerMinXMinYCorner,
                CACornerMask.layerMaxXMaxYCorner,
                CACornerMask.layerMinXMaxYCorner)
            rectCorner = UIRectCorner.allCorners
        case .begin:
            maskedCorners = CACornerMask(
                arrayLiteral: CACornerMask.layerMaxXMinYCorner,
                CACornerMask.layerMinXMinYCorner)
            rectCorner = UIRectCorner(rawValue:  (UIRectCorner.topLeft.rawValue) | (UIRectCorner.topRight.rawValue))
        case .end:
            maskedCorners = CACornerMask(
                arrayLiteral: CACornerMask.layerMaxXMaxYCorner,
                CACornerMask.layerMinXMaxYCorner)
            rectCorner = UIRectCorner(rawValue: (UIRectCorner.bottomLeft.rawValue) | (UIRectCorner.bottomRight.rawValue))
        default:
            layer.cornerRadius = 0
            guard #available(iOS 11.0, *) else {
                let size = CGSize(width: 0, height: 0)
                let rectCorner = UIRectCorner(rawValue: 0)
                filletedCorner(size, rectCorner)
                return
            }
            return
        }
        
        if #available(iOS 11.0, *) {
            layer.cornerRadius = cornerRadius
            layer.masksToBounds = true
            layer.maskedCorners = maskedCorners
        } else {
            
            // 這裏不設置圓角爲0,會被覆蓋
            layer.cornerRadius = 0
            // width 爲橫,height爲豎?這個size有點迷
            let size = CGSize(width: cornerRadius, height: 0)
            filletedCorner(size, rectCorner)
        }
    }
    
    /// 設置多個圓角
    ///
    /// - Parameters:
    ///   - cornerRadii: 圓角幅度
    ///   - roundingCorners: UIRectCorner(rawValue: (UIRectCorner.topRight.rawValue) | (UIRectCorner.bottomRight.rawValue))
    public func filletedCorner(_ cornerRadii: CGSize,
                               _ roundingCorners: UIRectCorner)  {
        
        let fieldPath = UIBezierPath.init(roundedRect: bounds, byRoundingCorners: roundingCorners, cornerRadii:cornerRadii )
        let fieldLayer = CAShapeLayer()
        fieldLayer.frame = bounds
        fieldLayer.path = fieldPath.cgPath
        layer.mask = fieldLayer
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章