1

I have several Circles in swiftui, that react on a touchgesture. I want them to increase the opac value and afterwards decrease it again:

HStack {
   Spacer(minLength: 20.0)
   Circle()
      //#e74c3c
      .foregroundColor(Color(red: 231.0/255.0, green: 76.0/255.0, blue: 60.0/255.0, opacity: buttonOpac[buttonColor.red.rawValue]))
      .gesture(TapGesture()
         .onEnded({if self.buttonActive {
             self.buttonActive = false
             self.blink(buttonColor.red)
          } else {
             print("button inactive")
          }
       })) ...

the method blink animates the change of the opac value:

func blink(_ buttonCol: buttonColor) {
        self.currentNumber += 1
        withAnimation(.easeOut(duration: blinkDuration)){
            self.buttonOpac[buttonCol.rawValue] = 1.0
        }
        withAnimation(Animation.easeIn(duration: blinkDuration).delay(blinkDuration)){
            self.buttonOpac[buttonCol.rawValue] = 0.5
        }
        withAnimation(Animation.linear.delay(2 * blinkDuration)){
            self.buttonActive = true
        }

    }

For the time that this animation is active I want the circle to be disabled and not reacting on touches. I'll try to reach that by the @Status var buttonActive

This works only to the point, that setting this value to false on entry of the .onEnded block. Now I want to reset it in the blink method after the other animations have ended. But the delay modifier does not work for the third "animation". In fact the value is set immediately back to true so that the circle continues to react on touches.

Is the only way to reach the goal without the usage of timers?

MrSpock
  • 19
  • 2
  • Can you give us the declarations of buttonOpac, buttonColor, currentNumber and blinkDuration ? – Nicolas Mandica Apr 25 '20 at 15:41
  • You can use same solution as I did for scaling in [how to do a simple scaling animation and why isn't this working?](https://stackoverflow.com/a/61017784/12299030) – Asperi Apr 25 '20 at 16:44
  • @Asperi Thank you for your scaling solution. To better understand how that works, I had to understand the concept of animation, which is very good explained in (https://swiftui-lab.com/swiftui-animations-part1/). Together with the theory your solution is good to understand and does exactly what I wanted to achieve. – MrSpock May 02 '20 at 11:09

1 Answers1

-1

Here are the declarations:

enum buttonColor: Int { 
   case red 
   case blue 
   case green 
   case yellow } 

let blinkDuration = 0.5

@State private var buttonOpac: [Double] = [0.5, 0.5, 0.5, 0.5]
@State private var currentNumber = 0
MrSpock
  • 19
  • 2