Marquee with buttons inside a view SwiftUI - swift

I'm trying to have a marquee horizontal scrolling effect but my buttons are not clickable. The view renders well, but when I tap the buttons it should print out 'tapped user1', for example but there is no effect.
EDIT: If I put the marquee modifier on the ScrollView as suggested, it causes the extremely buggy scrolling behavior. Ideally, this would just be a marquee'd HStack with a bunch of clickable buttons in it with no scrolling behavior built in, but the module doesn't seem to work without the ScrollView wrapping it.
I used this link to create a Marquee view modifier: https://swiftuirecipes.com/blog/swiftui-marquee
My code for the view is below:
struct MyView: View {
var body: some View {
let users = ["user1", "user2", "user3", "user4", "user5", "user6"]
return ScrollView(.horizontal, showsIndicators: false) {
HStack {
ForEach(users, id: \.self) { user in
if user == users.first {
Button(action: {
print("tapped \(user)")
}, label: {
Text(user)
})
} else {
Text("•")
Button(action: {
print("tapped \(user)")
}, label: {
Text(user)
})
}
}
}.frame(height: 20)
.marquee(duration: 10)
}
}
}
The code from the marquee tutorial is below:
struct Marquee: ViewModifier {
let duration: TimeInterval
let direction: Direction
let autoreverse: Bool
#State private var offset = CGFloat.zero
#State private var parentSize = CGSize.zero
#State private var contentSize = CGSize.zero
func body(content: Content) -> some View {
// measures parent view width
Color.clear
.frame(height: 0)
// measureSize from https://swiftuirecipes.com/blog/getting-size-of-a-view-in-swiftui
.measureSize { size in
parentSize = size
updateAnimation(sizeChanged: true)
}
content
.measureSize { size in
contentSize = size
updateAnimation(sizeChanged: true)
}
.offset(x: offset)
// animationObserver from https://swiftuirecipes.com/blog/swiftui-animation-observer
.animationObserver(for: offset, onComplete: {
updateAnimation(sizeChanged: false)
})
}
private func updateAnimation(sizeChanged: Bool) {
if sizeChanged || !autoreverse {
offset = max(parentSize.width, contentSize.width) * ((direction == .leftToRight) ? -1 : 1)
}
withAnimation(.linear(duration: duration)) {
offset = -offset
}
}
enum Direction {
case leftToRight, rightToLeft
}
}
extension View {
func marquee(duration: TimeInterval,
direction: Marquee.Direction = .rightToLeft,
autoreverse: Bool = false) -> some View {
self.modifier(Marquee(duration: duration,
direction: direction,
autoreverse: autoreverse))
}
}
struct SizePreferenceKey: PreferenceKey {
static var defaultValue: CGSize = .zero
static func reduce(value: inout CGSize, nextValue: () -> CGSize) {
value = nextValue()
}
}
struct MeasureSizeModifier: ViewModifier {
func body(content: Content) -> some View {
content.background(GeometryReader { geometry in
Color.clear.preference(key: SizePreferenceKey.self,
value: geometry.size)
})
}
}
extension View {
func measureSize(perform action: #escaping (CGSize) -> Void) -> some View {
self.modifier(MeasureSizeModifier())
.onPreferenceChange(SizePreferenceKey.self, perform: action)
}
}
public struct AnimationObserverModifier<Value: VectorArithmetic>: AnimatableModifier {
// this is the view property that drives the animation - offset, opacity, etc.
private let observedValue: Value
private let onChange: ((Value) -> Void)?
private let onComplete: (() -> Void)?
// SwiftUI implicity sets this value as the animation progresses
public var animatableData: Value {
didSet {
notifyProgress()
}
}
public init(for observedValue: Value,
onChange: ((Value) -> Void)?,
onComplete: (() -> Void)?) {
self.observedValue = observedValue
self.onChange = onChange
self.onComplete = onComplete
animatableData = observedValue
}
public func body(content: Content) -> some View {
content
}
private func notifyProgress() {
DispatchQueue.main.async {
onChange?(animatableData)
if animatableData == observedValue {
onComplete?()
}
}
}
}
public extension View {
func animationObserver<Value: VectorArithmetic>(for value: Value,
onChange: ((Value) -> Void)? = nil,
onComplete: (() -> Void)? = nil) -> some View {
self.modifier(AnimationObserverModifier(for: value,
onChange: onChange,
onComplete: onComplete))
}
}

Put the modifier on scrollview, it will fix your issue
Like this.
import SwiftUI
struct MyView: View {
var body: some View {
let users = ["user1", "user2", "user3", "user4", "user5", "user6"]
return ScrollView(.horizontal, showsIndicators: false) {
HStack {
ForEach(users, id: \.self) { user in
if user == users.first {
Button(action: {
print("tapped \(user)")
}, label: {
Text(user)
})
} else {
Text("•")
Button(action: {
print("tapped \(user)")
}, label: {
Text(user)
})
}
}
}.frame(height: 20)
}
.marquee(duration: 10)
}
}

Try adding the modifier on the button .buttonStyle(PlainButtonStyle()) or similar with other button styles. If that doesn't work, then try using just the button label that you desire and add the modifier .onTapGesture {your code}

Related

Using EqualWidthIconDomain gives error that nobody else in post gets

This post is mainly just meant for rob mayoff, but anybody else is free to help, because I don't know how to contact him other than making a post as I don't have enough reputation to comment.
In your answer on SwiftUI Multiple Labels Vertically Aligned, I tried to implement the code, and even though I changed nothing besides iOS requirements as per XCode warnings, I get the error of Cannot convert value of type 'VStack<TupleView<(Text, Divider, HStack<TupleView<(VStack<TupleView<(Label<Text, Text>, Label<Text, Image>)>>, VStack<TupleView<(Label<Text, Image>, Label<Text, Image>)>>)>>)>>' to closure result type 'Content' Is this the result of Xcode 13.4.1, or is it something else? I didn't see anybody have issues with the code.
My code structure is:
fileprivate struct IconWidthKey: PreferenceKey {
static var defaultValue: CGFloat? { nil }
static func reduce(value: inout CGFloat?, nextValue: () -> CGFloat?) {
switch (value, nextValue()) {
case (nil, let next): value = next
case (_, nil): break
case (.some(let current), .some(let next)): value = max(current, next)
}
}
}
extension IconWidthKey: EnvironmentKey { }
extension EnvironmentValues {
fileprivate var iconWidth: CGFloat? {
get { self[IconWidthKey.self] }
set { self[IconWidthKey.self] = newValue }
}
}
fileprivate struct IconWidthModifier: ViewModifier {
#Environment(\.iconWidth) var width
func body(content: Content) -> some View {
content
.background(GeometryReader { proxy in
Color.clear
.preference(key: IconWidthKey.self, value: proxy.size.width)
})
.frame(width: width)
}
}
struct EqualIconWidthLabelStyle: LabelStyle {
#available(iOS 14.0, *)
func makeBody(configuration: Configuration) -> some View {
HStack {
configuration.icon.modifier(IconWidthModifier())
configuration.title
}
}
}
#available(iOS 14.0, *)
struct EqualIconWidthDomain<Content: View>: View {
let content: Content
#State var iconWidth: CGFloat? = nil
init(#ViewBuilder _ content: () -> Content) {
self.content = content()
}
var body: some View {
content
.environment(\.iconWidth, iconWidth)
.onPreferenceChange(IconWidthKey.self) { self.iconWidth = $0 }
.labelStyle(EqualIconWidthLabelStyle())
}
struct WelcomeScreen: View{
...
#ViewBuilder
func playWelcomeScreen() -> some View{
...
EqualIconWidthDomain {
VStack {
Text("Le Menu")
.font(.caption)
Divider()
HStack {
VStack(alignment: .leading) {
Label(
title: { Text("Strawberry") },
icon: { Text("🍓") })
Label("Money", systemImage: "banknote")
}
VStack(alignment: .leading) {
Label("People", systemImage: "person.3")
Label("Star", systemImage: "star")
}
}
}
}
...
}
}
It is just bad copy-pasting, you loose one brace
#available(iOS 14.0, *)
struct EqualIconWidthDomain<Content: View>: View {
let content: Content
#State var iconWidth: CGFloat? = nil
init(#ViewBuilder _ content: () -> Content) {
self.content = content()
}
var body: some View {
content
.environment(\.iconWidth, iconWidth)
.onPreferenceChange(IconWidthKey.self) { self.iconWidth = $0 }
.labelStyle(EqualIconWidthLabelStyle())
} // << here !!
}

Handle single click and double click while updating the view

I'm trying to implement a single and double click for grid view items on macOS. On the first click it should highlight the item. On the second click it should open a detail view.
struct MyGridView: View {
var items: [String]
#State var selectedItem: String?
var body: some View {
LazyVGrid(columns: [
GridItem(.adaptive(minimum: 200, maximum: 270))
], alignment: .center, spacing: 8, pinnedViews: []) {
ForEach(items, id: \.self) { item in
Text("Test")
.gesture(TapGesture(count: 2).onEnded {
print("double clicked")
})
.simultaneousGesture(TapGesture().onEnded {
self.selectedItem = item
})
.background(self.selectedItem == item ? Color.red : .blue)
}
}
}
Problem
The highlighting works as expected without delay. But because the first click updates the view, the TapGesture for the double click is ignored. Only once the item was selected before, the double click also works, because it doesn't need to update the view again.
I was following https://stackoverflow.com/a/59992192/1752496 but it doesn't consider view updates after the first click.
You can use a custom click handler like this:
class TapState {
static var taps: [String: Date] = [:]
static func isDoubleTap<Content: View>(for view: Content) -> Bool {
let key = "\(view)"
let date = taps[key]
taps[key] = Date()
if let date = date, date.timeIntervalSince1970 >= Date().timeIntervalSince1970 - 0.4 {
return true
} else {
return false
}
}
}
extension View {
public func onTapGesture(firstTap: #escaping () -> Void, secondTap: #escaping () -> Void) -> some View {
onTapGesture(count: 1) {
if TapState.isDoubleTap(for: self) {
secondTap()
} else {
firstTap()
}
}
}
}
And implement it like this:
struct MyView: View {
#State var isSelected: Bool = false
#State var isShowingDetail: Bool = false
var body: some View {
Text("Text")
.background(isSelected ? Color.green : Color.blue)
.onTapGesture(firstTap: {
isSelected = true
}, secondTap: {
isShowingDetail = true
})
.sheet(isPresented: $isShowingDetail) {
Text("Detail")
}
}
}
Swift Package
I've also created a small package you can use: https://github.com/lukaswuerzburger/DoubleTap
As far as I know there is no build in function or something like that to understand deference between singleTap or doubleTap, but we have endless freedom to build what we want, I found this way:
You can apply tapRecognizer to anything you want, will work the same! for showing the use case I just applied to a Circle().
import SwiftUI
struct ContentView: View {
var body: some View {
Circle()
.fill(Color.red)
.frame(width: 100, height: 100, alignment: .center)
.tapRecognizer(tapSensitivity: 0.2, singleTapAction: singleTapAction, doubleTapAction: doubleTapAction)
}
func singleTapAction() { print("singleTapAction") }
func doubleTapAction() { print("doubleTapAction") }
}
struct TapRecognizerViewModifier: ViewModifier {
#State private var singleTapIsTaped: Bool = Bool()
var tapSensitivity: Double
var singleTapAction: () -> Void
var doubleTapAction: () -> Void
init(tapSensitivity: Double, singleTapAction: #escaping () -> Void, doubleTapAction: #escaping () -> Void) {
self.tapSensitivity = tapSensitivity
self.singleTapAction = singleTapAction
self.doubleTapAction = doubleTapAction
}
func body(content: Content) -> some View {
return content
.gesture(simultaneouslyGesture)
}
private var singleTapGesture: some Gesture { TapGesture(count: 1).onEnded{
singleTapIsTaped = true
DispatchQueue.main.asyncAfter(deadline: .now() + tapSensitivity) { if singleTapIsTaped { singleTapAction() } }
} }
private var doubleTapGesture: some Gesture { TapGesture(count: 2).onEnded{ singleTapIsTaped = false; doubleTapAction() } }
private var simultaneouslyGesture: some Gesture { singleTapGesture.simultaneously(with: doubleTapGesture) }
}
extension View {
func tapRecognizer(tapSensitivity: Double, singleTapAction: #escaping () -> Void, doubleTapAction: #escaping () -> Void) -> some View {
return self.modifier(TapRecognizerViewModifier(tapSensitivity: tapSensitivity, singleTapAction: singleTapAction, doubleTapAction: doubleTapAction))
}
}

SwiftUI ScrollView not following the chained animation

Back again for some SwiftUI issues. haha
So I have a scroll view, and I simulate a user switching elements in the scrollview pretty fast.
(ex. Music Lyrics that adjust to where the user is listening).
My issue here is the animation are not following the speed and I was wondering if there was a way to prevent that. Cancel the previous animation for example? I haven't found anything atm to fix this problem.
The following code is used to reproduce the Animation issue.
import SwiftUI
struct ContentView: View {
#ObservedObject var viewModel = ViewModel()
var body: some View {
ScrollView(showsIndicators: false, content: {
ScrollViewReader(content: { scrollViewProxy in
LazyVStack(content: {
ForEach(viewModel.fragments, id: \.id) { fragment in
Text(fragment.content + fragment.id)
.background(fragment == viewModel.currentFragment ? Color.yellow : Color.gray)
.frame(height: 100)
.font(.largeTitle)
.id(fragment.id)
}
})
.id("ContentView-LazyVStack-Animation")
.onReceive(viewModel.$currentFragment, perform: { currentFragment in
guard let currentFragment = currentFragment else {
return
}
withAnimation(.easeInOut(duration: 2)) {
scrollViewProxy.scrollTo(currentFragment.id, anchor: .center)
}
})
})
})
.id("ContentView-ScrollView-Animation")
}
}
final class ViewModel: ObservableObject {
#Published var fragments: [Fragment] = [Int](0..<100).map({ Fragment(id: "\($0)", content: "Some text yeah! super cool.") })
#Published var currentFragment: Fragment?
private var scrollingTimer: Timer?
init() {
currentFragment = fragments.first
setupRandomScroll()
}
func setupRandomScroll() {
scrollingTimer = Timer.scheduledTimer(withTimeInterval: 0.2,
repeats: true,
block: { [weak self] _ in
guard let self = self else {
return
}
let newIndex = Int.random(in: 70..<100)
self.currentFragment = self.fragments[newIndex]
})
}
}
final class Fragment: ObservableObject, Equatable, Hashable {
var id: String
#Published var content: String
init(id: String, content: String) {
self.id = id
self.content = content
}
static func == (lhs: Fragment, rhs: Fragment) -> Bool {
return lhs.id == rhs.id
}
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}
Thanks in advance for any help given! :)

How to use increment & decrement functions in stepper while also using onChange modifier

I have my audioPlayer setup already
In addition to the current functionality of the stepper, I want to also play separate sounds for onIncrement & onDecrement.
This project uses Core Data to persist. $estimatorData.qty listens to published var my View Model when the qty changes the new qty is saved in my view model estimatorData.save()
Here is a link to docs Stepper
I am trying to wrap my head around if one of the initializers would fit with what I am trying to accomplish
Stepper("", value: $estimatorData.qty.onChange { qty in
estimatorData.save()
}, in: 0...10000)
.frame(width: 100, height: 35)
.offset(x: -4)
.background(colorScheme == .dark ? Color.blue : Color.blue)
.cornerRadius(8)
Here are my players
func incrementTaped() {
playSound(sound: "plus", type: "mp3")
}
func decrementTaped() {
playSound(sound: "minus", type: "m4a")
}
Problem
Currently there is no initialiser which combines both onIncrement / onDecrement functions and value / bounds / step parameters.
You can either have onIncrement and onDecrement:
public init(onIncrement: (() -> Void)?, onDecrement: (() -> Void)?, onEditingChanged: #escaping (Bool) -> Void = { _ in }, #ViewBuilder label: () -> Label)
or value, bounds and step:
public init<V>(value: Binding<V>, in bounds: ClosedRange<V>, step: V.Stride = 1, onEditingChanged: #escaping (Bool) -> Void = { _ in }, #ViewBuilder label: () -> Label) where V : Strideable
Solution
Instead, you can create a custom Stepper to combine both initialisers:
struct CustomStepper<Label, Value>: View where Label: View, Value: Strideable {
#Binding private var value: Value
private let bounds: ClosedRange<Value>
private let step: Value.Stride
private let onIncrement: (() -> Void)?
private let onDecrement: (() -> Void)?
private let label: () -> Label
#State private var previousValue: Value
public init(
value: Binding<Value>,
in bounds: ClosedRange<Value>,
step: Value.Stride = 1,
onIncrement: (() -> Void)? = nil,
onDecrement: (() -> Void)? = nil,
#ViewBuilder label: #escaping () -> Label
) {
self._value = value
self.bounds = bounds
self.step = step
self.onIncrement = onIncrement
self.onDecrement = onDecrement
self.label = label
self._previousValue = .init(initialValue: value.wrappedValue)
}
var body: some View {
Stepper(
value: $value,
in: bounds,
step: step,
onEditingChanged: onEditingChanged,
label: label
)
}
func onEditingChanged(isEditing: Bool) {
guard !isEditing else {
previousValue = value
return
}
if previousValue < value {
onIncrement?()
} else if previousValue > value {
onDecrement?()
}
}
}
Demo
struct ContentView: View {
#State private var value = 1
var body: some View {
CustomStepper(value: $value, in: 1...10, onIncrement: onIncrement, onDecrement: onDecrement) {
Text(String(value))
}
.onChange(of: value) {
print("onChange value: \($0)")
}
}
func onIncrement() {
print("onIncrement")
}
func onDecrement() {
print("onDecrement")
}
}
Generally speaking the business logic should not go in your view since views are values that get created and destroyed all the time. Move it into a ViewModel. You can watch any published property by using its projectedValue to get a publisher. For example:
import SwiftUI
import PlaygroundSupport
import Combine
final class ViewModel: ObservableObject {
#Published var steps: Int = 0
#Published var title: String = ""
init() {
$steps
.handleEvents(receiveOutput: { value in
// Perform your audio side effect for this value here
})
.map {"The current value is \($0)" }
.assign(to: &$title)
}
}
struct ContentView: View {
#StateObject var viewModel = ViewModel()
var body: some View {
Stepper(viewModel.title, value: $viewModel.steps)
}
}
PlaygroundPage.current.liveView = UIHostingController(rootView: ContentView())

How to properly update a modifier?

I'm trying to have every object in the view be shifted up when a text field is present, and to reduce amount of code, I tried making the modifiers into a modifier class, but the issue is when the y value gets changed for the keyboard handlers, it doesn't move. This exact code works if you don't use the separate function, so I don't know what's wrong.
I've tried making the value of the y a state in the modifier, but it still doesn't update - and like I said, it works if it's not in the custom modifier.
ContentView.swift
struct ContentView: View {
#State private var name = Array<String>.init(repeating: "", count: 3)
#ObservedObject private var kGuardian = KeyboardGuardian(textFieldCount: 1)
var body: some View {
Background {
VStack {
Group {
Text("Some filler text").font(.largeTitle)
Text("Some filler text").font(.largeTitle)
}
TextField("enter text #1", text: self.$name[0])
.textFieldStyle(RoundedBorderTextFieldStyle())
TextField("enter text #2", text: self.$name[1])
.textFieldStyle(RoundedBorderTextFieldStyle())
TextField("enter text #3", text: self.$name[2])
.textFieldStyle(RoundedBorderTextFieldStyle())
.background(GeometryGetter(rect: self.$kGuardian.rects[0]))
}
}.modifier(BetterTextField(kGuardian: kGuardian, slide: kGuardian.slide))
}
}
KeyboardModifications.swift
struct GeometryGetter: View {
#Binding var rect: CGRect
var body: some View {
GeometryReader { geometry in
Group { () -> AnyView in
DispatchQueue.main.async {
self.rect = geometry.frame(in: .global)
}
return AnyView(Color.clear)
}
}
}
}
final class KeyboardGuardian: ObservableObject {
public var rects: Array<CGRect>
public var keyboardRect: CGRect = CGRect()
// keyboardWillShow notification may be posted repeatedly,
// this flag makes sure we only act once per keyboard appearance
public var keyboardIsHidden = true
#Published var slide: CGFloat = 0
var appearenceDuration : Double = 0
var showField: Int = 0 {
didSet {
updateSlide()
}
}
init(textFieldCount: Int) {
self.rects = Array<CGRect>(repeating: CGRect(), count: textFieldCount)
NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
#objc func keyBoardWillShow(notification: Notification) {
guard let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double else {return}
appearenceDuration = duration
if keyboardIsHidden {
keyboardIsHidden = false
if let rect = notification.userInfo?["UIKeyboardFrameEndUserInfoKey"] as? CGRect {
keyboardRect = rect
updateSlide()
}
}
}
#objc func keyBoardWillHide(notification: Notification) {
keyboardIsHidden = true
updateSlide()
}
func updateSlide() {
if keyboardIsHidden {
slide = 0
} else {
let tfRect = self.rects[self.showField]
let diff = keyboardRect.minY - tfRect.maxY
if diff > 0 {
slide += diff
} else {
slide += min(diff, 0)
}
}
}
}
struct Background<Content: View>: View {
private var content: Content
init(#ViewBuilder content: #escaping () -> Content) {
self.content = content()
}
var body: some View {
Color.white
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
.overlay(content)
}
}
The custom modifier class:
struct BetterTextField: ViewModifier {
public var kGuardian : KeyboardGuardian
#State public var slide : CGFloat
func body(content: Content) -> some View {
content
.offset(y: slide).animation(.easeIn(duration: kGuardian.appearenceDuration))
.onTapGesture {
let keyWindow = UIApplication.shared.connectedScenes
.filter({$0.activationState == .foregroundActive})
.map({$0 as? UIWindowScene})
.compactMap({$0})
.first?.windows
.filter({$0.isKeyWindow}).first
keyWindow!.endEditing(true)
}
}
}
What should happen is the entire screen should shift up with the keyboard, but it stays as it is. I've been trying things for around 2 hours to no avail.
I guess your slide variable is not updated. Try with the following change code in BetterTextField modifier:
struct BetterTextField: ViewModifier {
#ObservedObject var kGuardian : KeyboardGuardian
func body(content: Content) -> some View {
content
.offset(y: kGuardian.slide).animation(.easeIn(duration: kGuardian.appearenceDuration))
.onTapGesture {
let keyWindow = UIApplication.shared.connectedScenes
.filter({$0.activationState == .foregroundActive})
.map({$0 as? UIWindowScene})
.compactMap({$0})
.first?.windows
.filter({$0.isKeyWindow}).first
keyWindow!.endEditing(true)
}
}
}