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

64 lines
2.0 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.

//
// JXSegmentedIndicatorGradientLineView.swift
// JXSegmentedView
//
// Created by jiaxin on 2020/7/6.
// Copyright © 2020 jiaxin. All rights reserved.
//
import UIKit
/// indicatorColorgradientColors
open class JXSegmentedIndicatorGradientLineView: JXSegmentedIndicatorLineView {
open var colors = [UIColor]()
open var startPoint = CGPoint.zero
open var endPoint = CGPoint(x: 1, y: 0)
open var locations: [NSNumber]?
public let gradientLayer = CAGradientLayer()
open override func commonInit() {
super.commonInit()
layer.masksToBounds = true
layer.addSublayer(gradientLayer)
}
open override func refreshIndicatorState(model: JXSegmentedIndicatorSelectedParams) {
super.refreshIndicatorState(model: model)
backgroundColor = .clear
CATransaction.begin()
CATransaction.setDisableActions(true)
gradientLayer.frame = bounds
gradientLayer.colors = colors.map { $0.cgColor }
gradientLayer.startPoint = startPoint
gradientLayer.endPoint = endPoint
gradientLayer.locations = locations
CATransaction.commit()
}
open override func contentScrollViewDidScroll(model: JXSegmentedIndicatorTransitionParams) {
super.contentScrollViewDidScroll(model: model)
guard canHandleTransition(model: model) else {
return
}
CATransaction.begin()
CATransaction.setDisableActions(true)
gradientLayer.frame = bounds
CATransaction.commit()
}
open override func selectItem(model: JXSegmentedIndicatorSelectedParams) {
super.selectItem(model: model)
let targetWidth = getIndicatorWidth(itemFrame: model.currentSelectedItemFrame, itemContentWidth: model.currentItemContentWidth)
CATransaction.begin()
CATransaction.setAnimationDuration(scrollAnimationDuration)
CATransaction.setAnimationTimingFunction(.init(name: .easeOut))
gradientLayer.frame.size.width = targetWidth
CATransaction.commit()
}
}