UITableview 设置组圆角,以及最后或首行去线处理
2019-07-20 本文已影响5人
何以消摇
效果
效果图.png调用方法
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
}
}