123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- //
- // JXSegmentedIndicatorImageView.swift
- // JXSegmentedView
- //
- // Created by jiaxin on 2019/1/2.
- // Copyright © 2019 jiaxin. All rights reserved.
- //
- import UIKit
- open class JXSegmentedIndicatorImageView: JXSegmentedIndicatorBaseView {
- open var image: UIImage? {
- didSet {
- layer.contents = image?.cgImage
- }
- }
- open override func commonInit() {
- super.commonInit()
- indicatorWidth = 20
- indicatorHeight = 20
- layer.contentsGravity = .resizeAspect
- }
- open override func refreshIndicatorState(model: JXSegmentedIndicatorSelectedParams) {
- super.refreshIndicatorState(model: model)
- backgroundColor = nil
- let width = getIndicatorWidth(itemFrame: model.currentSelectedItemFrame, itemContentWidth: model.currentItemContentWidth)
- let height = getIndicatorHeight(itemFrame: model.currentSelectedItemFrame)
- let x = model.currentSelectedItemFrame.origin.x + (model.currentSelectedItemFrame.size.width - width)/2
- var y: CGFloat = 0
- switch indicatorPosition {
- case .top:
- y = verticalOffset
- case .bottom:
- y = model.currentSelectedItemFrame.size.height - height - verticalOffset
- case .center:
- y = (model.currentSelectedItemFrame.size.height - height)/2 + verticalOffset
- }
- frame = CGRect(x: x, y: y, width: width, height: height)
- }
- open override func contentScrollViewDidScroll(model: JXSegmentedIndicatorTransitionParams) {
- super.contentScrollViewDidScroll(model: model)
- guard canHandleTransition(model: model) else {
- return
- }
- let rightItemFrame = model.rightItemFrame
- let leftItemFrame = model.leftItemFrame
- let percent = model.percent
- let targetWidth = getIndicatorWidth(itemFrame: model.leftItemFrame, itemContentWidth: model.leftItemContentWidth)
- let leftX = leftItemFrame.origin.x + (leftItemFrame.size.width - targetWidth)/2
- let rightX = rightItemFrame.origin.x + (rightItemFrame.size.width - targetWidth)/2
- let targetX = JXSegmentedViewTool.interpolate(from: leftX, to: rightX, percent: CGFloat(percent))
-
- self.frame.origin.x = targetX
- }
- open override func selectItem(model: JXSegmentedIndicatorSelectedParams) {
- super.selectItem(model: model)
- let targetWidth = getIndicatorWidth(itemFrame: model.currentSelectedItemFrame, itemContentWidth: model.currentItemContentWidth)
- var toFrame = self.frame
- toFrame.origin.x = model.currentSelectedItemFrame.origin.x + (model.currentSelectedItemFrame.size.width - targetWidth)/2
- if canSelectedWithAnimation(model: model) {
- UIView.animate(withDuration: scrollAnimationDuration, delay: 0, options: .curveEaseOut, animations: {
- self.frame = toFrame
- }) { (_) in
- }
- }else {
- frame = toFrame
- }
- }
- }
|