Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tvOS 14.0 availability check added #19

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 6 additions & 17 deletions Sources/MarqueeText/MarqueeText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public struct MarqueeText : View {
public var rightFade: CGFloat
public var startDelay: Double
public var alignment: Alignment
public var autoReverses: Bool = false

@State private var animate = false
var isCompact = false
Expand All @@ -16,9 +17,9 @@ public struct MarqueeText : View {
let stringHeight = text.heightOfString(usingFont: font)

let animation = Animation
.linear(duration: Double(stringWidth) / 30)
.linear(duration: Double(stringWidth) / 60)
.delay(startDelay)
.repeatForever(autoreverses: false)
.repeatForever(autoreverses: autoReverses)

let nullAnimation = Animation
.linear(duration: 0)
Expand All @@ -30,20 +31,7 @@ public struct MarqueeText : View {
Text(self.text)
.lineLimit(1)
.font(.init(font))
.offset(x: self.animate ? -stringWidth - stringHeight * 2 : 0)
.animation(self.animate ? animation : nullAnimation, value: self.animate)
.onAppear {
DispatchQueue.main.async {
self.animate = geo.size.width < stringWidth
}
}
.fixedSize(horizontal: true, vertical: false)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)

Text(self.text)
.lineLimit(1)
.font(.init(font))
.offset(x: self.animate ? 0 : stringWidth + stringHeight * 2)
.offset(x: self.animate ? -(stringWidth - geo.size.width + rightFade * 2.0) : 0)
.animation(self.animate ? animation : nullAnimation, value: self.animate)
.onAppear {
DispatchQueue.main.async {
Expand Down Expand Up @@ -90,13 +78,14 @@ public struct MarqueeText : View {

}

public init(text: String, font: UIFont, leftFade: CGFloat, rightFade: CGFloat, startDelay: Double, alignment: Alignment? = nil) {
public init(text: String, font: UIFont, leftFade: CGFloat, rightFade: CGFloat, startDelay: Double, alignment: Alignment? = nil, autoReverses: Bool = false) {
self.text = text
self.font = font
self.leftFade = leftFade
self.rightFade = rightFade
self.startDelay = startDelay
self.alignment = alignment != nil ? alignment! : .topLeading
self.autoReverses = autoReverses
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/MarqueeText/SwiftUI+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Combine
extension View {
/// A backwards compatible wrapper for iOS 14 `onChange`
@ViewBuilder func onValueChanged<T: Equatable>(of value: T, perform onChange: @escaping (T) -> Void) -> some View {
if #available(iOS 14.0, *) {
if #available(iOS 14.0, *), #available(tvOS 14.0, *) {
self.onChange(of: value, perform: onChange)
} else {
self.onReceive(Just(value)) { (value) in
Expand Down