Skip to content

Can't stop animation with delay #102

Closed
@zapletnev

Description

@zapletnev

stop() function doesn't have an effect

import Foundation
import Macaw

class StudioView: MacawView {
    required init?(coder aDecoder: NSCoder) {
        super.init(node: Group(contents: []), coder: aDecoder)
        
        let rect = Rect(
            w: 50.0,
            h: 50.0
        )
        
        let soundBase = Shape(
            form: rect,
            fill: Color.red
        )
        soundBase.place = Transform.move(dx: Double(self.bounds.width) / 2.0 - 25, dy: Double(self.bounds.height) / 2.0 - 25)
        
        let text = Text(
            text: "START",
            align: .mid,
            place: Transform.move(
                dx: (Double(self.bounds.width) / 2),
                dy: (Double(self.bounds.height) / 2)
            ),
            opaque: false
        )
        
        var animation: Animation!
        
        var count: Int = 0
        soundBase.onTap { _ in
            if count % 2 == 0 {
                animation = Array(0...10).map { index in
                    return [
                        soundBase.opacityVar.animation(to: 1.0, during: 0.1),
                        soundBase.opacityVar.animation(to: 0.0, during: 0.1)
                    ].sequence().delay(Double(index))
                }.combine()

                text.text = "STOP!"
                print("START!")
                animation.play()
            } else {
                print("STOP!")
                text.text = "START!"
                animation.stop()
            }
            count = count + 1
        }
        
        self.node = [soundBase, text].group()
      }
}  

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions