SQReorderableStackView 0.4.2

SQReorderableStackView 0.4.2

Mark E Murray 维护。



  • By
  • markedwardmurray

SQReorderableStackView

CI Status Version License Platform

示例

要运行示例项目,克隆仓库,然后首先从 Example 目录运行 pod install

要求

  • iOS 9.0 以及更新版本
  • Swift 4.2

安装

SQReorderableStackView 可以通过 CocoaPods 获取。要安装它,只需将以下行添加到您的 Podfile 中

pod "SQReorderableStackView"

用法

SQReorderableStackViewUIStackView 的子类。要使用 Interface Builder,添加一个 UIStackView nib 并将其自定义类设置为 SQReorderableStackView。它不需要设置 reorderDelegate
注意:任何 userInteractionEnabled 属性设置为 false 的子视图将无法被用户“捡起”。

公共属性

SQReorderableStackView

    /// Whether or not the subviews can be picked and reordered.
    public var reorderingEnabled = true

    /// The delegate for reordering.
    public var reorderDelegate: SQReorderableStackViewDelegate?
    
    /// The relative scale of the held view's snapshot during reordering to its subview's canonical size. Default is `1.1`
    public var temporaryViewScale: CGFloat = 1.1
    
    /// The releative scale of the other subviews's size during reordering. Default is `0.95`
    public var otherViewsScale: CGFloat = 0.95
    
    /// The alpha of the held view's snapshot curing reordering. Defaults is `0.9`
    public var temporaryViewAlpha: CGFloat = 0.9
    
    /// The gap created once the long press drag is triggered. Default is `5`
    public var dragHintSpacing: CGFloat = 5
    
    /// The longPress duration for activating reordering. Default is `0.2` seconds
    public var longPressMinimumPressDuration = 0.2
    
    /// Determines whether or not the axis is horizontal.
    public var isHorizontal: Bool // readonly
    
    /// Determines whether or not the axis is vertical.
    public var isVertical: Bool // readonly

代理方法

使用reorderDelegate可以更精细地控制子视图的重新排序,并响应用户对子视图顺序所做的更改。

SQReorderableStackViewDelegate

    /// called when a subview is "picked up" by the user
    @objc optional func stackViewDidBeginReordering(_ stackView: SQReorderableStackView)
    
    /// Whenever a user drags a subview for a reordering, the delegate is told whether the direction
    /// was forward (left/down) or backward (right/up), as well as what the max and min X or Y values are of the subview
    @objc optional func stackView(_ stackView: SQReorderableStackView, didDragToReorderInForwardDirection forward: Bool, maxPoint: CGPoint, minPoint: CGPoint)
    
    /// didReorderArrangedSubviews - called when reordering ends only if the selected subview's index changed during reordering
    @objc optional func stackView(_ stackView: SQReorderableStackView, didReorderArrangedSubviews arrangedSubviews: Array<UIView>)
    
    /// didEndReordering - called when reordering ends
    @objc optional func stackViewDidEndReordering(_ stackView: SQReorderableStackView)
    
    /// called when reordering is cancelled
    @objc optional func stackViewDidCancelReordering(_ stackView: SQReorderableStackView)
    
    /// Tells the ReorderableStackView whether or not the pressed subview may be picked up.
    @objc optional func stackView(_ stackView: SQReorderableStackView, canReorderSubview subview: UIView, atIndex index: Int) -> Bool
    
    /// Tells the ReorderableStackView whether or not the held subview can take the spot at which is being held.
    @objc optional func stackView(_ stackView: SQReorderableStackView, shouldAllowSubview subview: UIView, toMoveToIndex index: Int) -> Bool

例如,可以禁止单个子视图重新排序或移动。为了防止栈视图的第三个子视图(索引2)被选中或替换

    func stackView(_ stackView: SQReorderableStackView, canReorderSubview subview: UIView, atIndex index: Int) -> Bool {
        if index == 2 {
            return false
        } else {
            return true
        }
    }

    func stackView(_ stackView: SQReorderableStackView, shouldAllowSubview subview: UIView, toMoveToIndex index: Int) -> Bool {
        if index == 2 {
            return false
        } else {
            return true
        }
    }

此外,代理可以响应用户完成的重新排序操作,例如根据新顺序更新值

    func stackViewDidReorderArrangedSubviews(_ stackView: SQReorderableStackView) {
                var text = ""
        for label in stackView.arrangedSubviews as! [UILabel] {
            text.append(label.text!)
            text.append(" ")
        }
        
        self.label.text = text
    }

注意:stackViewDidEndReordering(_:)会每次用户交互结束时调用,无论子视图是否被重新排序。stackViewDidReorderArrangedSubviews(_:)只有在子视图顺序发生变化时才会被调用。

作者

markedwardmurray, [email protected]

SQReorderableStackView是从GitHub上由Clay Ellis上传的APReorderableStackView适配而来

许可

SQReorderableStackView在MIT许可下可用。更多信息请参阅LICENSE文件。