简体   繁体   中英

Touches began not being called

I have a view and I want to change its background color every few seconds. When I call the function touches began won't be called but the background color changes every few seconds. If I remove the line to change the background color every few seconds, everything is okay, Here is my code. Any help to this issue is appreciated. Ive tried everything from creating a new view to calling it on that view to god knows what. Please help

var colors2 : [UIColor] = [.red,.orange,.yellow,.blue,.purple]
var colorCounter : Int = 0

override func viewDidLoad() {
     super.viewDidLoad()

     scheduleBackgroundColor()
 }

  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  print("hellonow")

}

func scheduleBackgroundColor()
{
    let schedul = Timer.scheduledTimer(withTimeInterval: 2.0, repeats: true, block: { (timer) in

        UIView.animate(withDuration: 2.0, animations: {
            self.view.layer.backgroundColor = self.colors[self.colorCounter % 6].cgColor
            if self.colorCounter > 3
            {
                self.colorCounter = 0
            }else
            {
                self.colorCounter += 1
            }

        })
    })
    schedul.fire()
}

You need to allow user interaction during animation.

UIView.animate(
    withDuration: 2.0,
    delay: 0.0,
    options: [.allowUserInteraction],
    animations: {
        //Your code
    }
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM