Music_Player3/Pods/JXSegmentedView/Sources/Core/JXSegmentedBaseCell.swift
2024-06-03 09:48:39 +08:00

119 lines
3.7 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// JXSegmentedBaseCell.swift
// JXSegmentedView
//
// Created by jiaxin on 2018/12/26.
// Copyright © 2018 jiaxin. All rights reserved.
//
import UIKit
public typealias JXSegmentedCellSelectedAnimationClosure = (CGFloat)->()
open class JXSegmentedBaseCell: UICollectionViewCell, JXSegmentedViewRTLCompatible {
open var itemModel: JXSegmentedBaseItemModel?
open var animator: JXSegmentedAnimator?
private var selectedAnimationClosureArray = [JXSegmentedCellSelectedAnimationClosure]()
deinit {
animator?.stop()
}
open override func prepareForReuse() {
super.prepareForReuse()
animator?.stop()
animator = nil
}
public override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
open func commonInit() {
if segmentedViewShouldRTLLayout() {
segmentedView(horizontalFlipForView: self)
segmentedView(horizontalFlipForView: contentView)
}
}
open func canStartSelectedAnimation(itemModel: JXSegmentedBaseItemModel, selectedType: JXSegmentedViewItemSelectedType) -> Bool {
var isSelectedAnimatable = false
if itemModel.isSelectedAnimable {
if selectedType == .scroll {
//
if !itemModel.isItemTransitionEnabled {
isSelectedAnimatable = true
}
}else if selectedType == .click || selectedType == .code {
//
isSelectedAnimatable = true
}
}
return isSelectedAnimatable
}
open func appendSelectedAnimationClosure(closure: @escaping JXSegmentedCellSelectedAnimationClosure) {
selectedAnimationClosureArray.append(closure)
}
open func startSelectedAnimationIfNeeded(itemModel: JXSegmentedBaseItemModel, selectedType: JXSegmentedViewItemSelectedType) {
if itemModel.isSelectedAnimable && canStartSelectedAnimation(itemModel: itemModel, selectedType: selectedType) {
//isTransitionAnimating
itemModel.isTransitionAnimating = true
animator?.progressClosure = {[weak self] (percent) in
guard self != nil else {
return
}
for closure in self!.selectedAnimationClosureArray {
closure(percent)
}
}
animator?.completedClosure = {[weak self] in
itemModel.isTransitionAnimating = false
self?.selectedAnimationClosureArray.removeAll()
}
animator?.start()
}
}
open func reloadData(itemModel: JXSegmentedBaseItemModel, selectedType: JXSegmentedViewItemSelectedType) {
self.itemModel = itemModel
if itemModel.isSelectedAnimable {
selectedAnimationClosureArray.removeAll()
if canStartSelectedAnimation(itemModel: itemModel, selectedType: selectedType) {
animator = JXSegmentedAnimator()
animator?.duration = itemModel.selectedAnimationDuration
}else {
animator?.stop()
animator = nil
}
}
}
open override var isSelected: Bool {
didSet {
setSelectedStyle(isSelected: isSelected)
}
}
open override var isHighlighted: Bool {
didSet {
setSelectedStyle(isSelected: isHighlighted)
}
}
func setSelectedStyle(isSelected: Bool) {
}
}