触摸开始没有被调用

问题描述 投票:0回答:1

我有一个视图,我想每隔几秒改变它的背景颜色。当我调用函数触摸开始时将不会被调用,但背景颜色每隔几秒就会改变一次。如果我每隔几秒删除一行来改变背景颜色,一切都没问题,这是我的代码。对此问题的任何帮助表示赞赏。我已经尝试了从创建一个新视图到在该视图上调用它到上帝知道什么的一切。请帮忙

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()
}
ios swift
1个回答
0
投票

您需要在动画期间允许用户交互。

UIView.animate(
    withDuration: 2.0,
    delay: 0.0,
    options: [.allowUserInteraction],
    animations: {
        //Your code
    }
)
© www.soinside.com 2019 - 2024. All rights reserved.