0

Is there a way to access an 'onTapGesture' (or similar) for a lottie animated switch. I would like to change a property (isComplete) of a CoreData item when the switch is on. Below is my Lottie Button View. Many thanks.

import SwiftUI
import Lottie

struct LottieButton: UIViewRepresentable {

@State var task: Task
// Create a switch
let animationSwitch = AnimatedSwitch()
var filename = "checkmark"



func makeUIView(context: UIViewRepresentableContext<LottieButton>) -> UIView {
    let view = UIView()

    let animation = Animation.named(filename)
    animationSwitch.animation = animation
    animationSwitch.contentMode = .scaleAspectFit

    animationSwitch.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(animationSwitch)

    animationSwitch.clipsToBounds = false

    animationSwitch.setProgressForState(fromProgress: 0, toProgress: 0.4, forOnState: true)

// fail attempt #1
//    self.onTapGesture {
//        self.task.isComplete.toggle()
//        print(self.task.isComplete)
//    }
// fail attempt #2
//    if animationSwitch.isOn{
//        task.isComplete.toggle()
//        print(task.isComplete)
//        action()
//    }

    NSLayoutConstraint.activate([
        animationSwitch.heightAnchor.constraint(equalTo: view.heightAnchor),
        animationSwitch.widthAnchor.constraint(equalTo: view.widthAnchor),
    ])

    return view

}

func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<LottieButton>) {

    }
}
santi.gs
  • 514
  • 3
  • 15
  • Why don't you add a UITapGestureRecognizer to your view where your animation belongs to ? – Nakwenda Apr 25 '20 at 19:50
  • @Nakwenda I tried adding this using in the HStack using .gesture(onTapGesture() // etc. but the animation would no longer play and it didn't register as a change. – santi.gs Apr 25 '20 at 21:44

0 Answers0