Skip to content

Commit

Permalink
Release 0.0.10
Browse files Browse the repository at this point in the history
- PageMenuBar 添加 淡入淡出效果
  • Loading branch information
changsanjiang committed May 7, 2020
1 parent 8c8c53b commit f21ecf6
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
2 changes: 1 addition & 1 deletion SJPageViewController.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'SJPageViewController'
s.version = '0.0.9'
s.version = '0.0.10'
s.summary = 'SJPageViewController.'

# This description is used to generate tags and improve search results.
Expand Down
85 changes: 85 additions & 0 deletions SJPageViewController/Swift/PageMenuBar/SJPageMenuBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,24 @@ open class SJPageMenuBar: UIView {
get { return _scrollIndicator }
}

/// enable fade in on the left. default is `NO`.
open var isEnabledFadeIn: Bool = false {
didSet {
if ( oldValue != isEnabledFadeIn ) {
resetFadeMask()
}
}
}

/// enable fade out on the right. default is `NO`.
open var isEnabledFadeOut: Bool = false {
didSet {
if ( oldValue != isEnabledFadeOut ) {
resetFadeMask()
}
}
}

// MARK: -

public enum Distribution {
Expand Down Expand Up @@ -246,6 +264,8 @@ open class SJPageMenuBar: UIView {

private var previousBounds = CGRect.zero

private var fadeMaskLayer: CAGradientLayer?

private func setupViews() {
if #available(iOS 13.0, *) {
backgroundColor = .systemGroupedBackground
Expand All @@ -265,6 +285,7 @@ open class SJPageMenuBar: UIView {
if ( previousBounds.equalTo(bounds) == false ) {
previousBounds = bounds
scrollView.frame = bounds
resetFadeMask()
remakeConstraints()
setContentOffsetForScrollViewToIndex(focusedIndex)
}
Expand Down Expand Up @@ -623,6 +644,70 @@ private extension SJPageMenuBar {
}
}


private extension SJPageMenuBar {
func resetFadeMask() {
if ( isEnabledFadeIn || isEnabledFadeOut ) {
if ( bounds.size.width == 0 ) { return; }
CATransaction.begin()
CATransaction.setDisableActions(true)

if ( fadeMaskLayer == nil ) {
fadeMaskLayer = CAGradientLayer()
fadeMaskLayer?.startPoint = .zero
fadeMaskLayer?.endPoint = .init(x: 1, y: 0)
fadeMaskLayer?.frame = bounds
}

let width: CGFloat = 16.0
let widthCenti = width / bounds.size.width

var locations = Array<NSNumber>()
var colors = Array<CGColor>()
if ( isEnabledFadeIn ) {
locations.append(NSNumber(value: Float(0)))
locations.append(NSNumber(value: Float(widthCenti)))

colors.append(UIColor.clear.cgColor)
colors.append(UIColor.white.cgColor)

locations.append(locations.last!)
locations.append(NSNumber(value: Float(1 - widthCenti)))

colors.append(UIColor.white.cgColor)
colors.append(UIColor.white.cgColor)
}

if ( isEnabledFadeOut ) {
if ( !isEnabledFadeIn ) {
locations.append(NSNumber(value: Float(0)))
locations.append(NSNumber(value: Float(1 - widthCenti)))

colors.append(UIColor.white.cgColor)
colors.append(UIColor.white.cgColor)
}

locations.append(locations.last!)
locations.append(NSNumber(value: Float(1.0)))

colors.append(UIColor.white.cgColor)
colors.append(UIColor.clear.cgColor)
}

fadeMaskLayer?.locations = locations
fadeMaskLayer?.colors = colors
fadeMaskLayer?.frame = bounds
CATransaction.commit()

if ( self.layer.mask != fadeMaskLayer ) { self.layer.mask = fadeMaskLayer }
}
else if fadeMaskLayer != nil {
self.layer.mask = nil
fadeMaskLayer = nil
}
}
}

private class SJPageMenuBarScrollIndicator: UIView, SJPageMenuBarScrollIndicatorProtocol {
override func layoutSubviews() {
super.layoutSubviews()
Expand Down

0 comments on commit f21ecf6

Please sign in to comment.