我有一个功能,可以让图像在我的视野中来回“摇晃”。我可以设置我想要它摇动的时间和速度,但我真正需要的是它每 10 秒摇动一次。
这是我正在使用的函数,只需在 viewDidLoad 中调用一次即可使其运行一次。
func shakeView(){
let shake:CABasicAnimation = CABasicAnimation(keyPath: "position")
shake.duration = 0.4
shake.repeatCount = 5
shake.autoreverses = true
shake.speed = 9.0
let from_point:CGPoint = CGPointMake(homeLogo.center.x - 5, homeLogo.center.y)
let from_value:NSValue = NSValue(CGPoint: from_point)
let to_point:CGPoint = CGPointMake(homeLogo.center.x + 5, homeLogo.center.y)
let to_value:NSValue = NSValue(CGPoint: to_point)
shake.fromValue = from_value
shake.toValue = to_value
homeLogo.layer.addAnimation(shake, forKey: "position")
}
我还没有找到我如何能够做到这一点的幸运。任何帮助或指向正确方向的任何帮助将不胜感激。
最佳答案
你需要使用 NSTimer。使用以下代码:
override func viewDidLoad() {
super.viewDidLoad()
var helloWorldTimer = NSTimer.scheduledTimerWithTimeInterval(10.0, target: self, selector: Selector("shakeView"), userInfo: nil, repeats: true)
}
关于ios - 按时间间隔重复功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36258189/