iOS 问答集合收藏swift

Swift struct如何实现类似UIRectCorner.

2018-10-25  本文已影响18人  船长_
demo2.png demo1.png

场景需求,如图,长按cell弹出展示的View有的需要6个item,有的需要4个item

UIRectCorner定义

public struct UIRectCorner : OptionSet {
    
    public init(rawValue: UInt)
    
    public static var topLeft: UIRectCorner { get }
    
    public static var topRight: UIRectCorner { get }
    
    public static var bottomLeft: UIRectCorner { get }
    
    public static var bottomRight: UIRectCorner { get }
    
    public static var allCorners: UIRectCorner { get }
}

参考UIRectCorner,自定义类似实现

// 静态属性集合
struct MenuItemType : OptionSet {
    
    public var rawValue = 0  // 因为RawRepresentable的要求
    public static var copys = MenuItemType(rawValue : 1 << 0)

    public static var transmit = MenuItemType(rawValue : 1 << 1)

    public static var collect = MenuItemType(rawValue : 1 << 2)

    public static var delete = MenuItemType(rawValue : 1 << 3)

    public static var revoke = MenuItemType(rawValue : 1 << 4)

    public static var download = MenuItemType(rawValue : 1 << 5)
    
    public static var allCase: MenuItemType {
        
        return [.copys, .transmit, .collect, .delete, .revoke, .download]
    }
}

用法示例

if message?.msgDirection == .incoming {
    customMenu.itemType = [.copys,.transmit,.collect,.delete]
}else{
   // customMenu.itemType = [.copys,.transmit,.collect,.revoke,.delete]
    customMenu.itemType = .allCase
}

==========================
if itemType == .allCase {
    itemCount = 6
    containerView.addArrangedSubview(copyingButton)
    containerView.addArrangedSubview(transmitButton)
  .
}else if itemType.contains([.copys,.transmit,.collect,.delete,.revoke]){
    itemCount = 5
    containerView.addArrangedSubview(copyingButton)
    ....
}else {
    ...
}
上一篇下一篇

猜你喜欢

热点阅读