Ios 为递增的UILabel设置动画
我试图在视图出现时设置UILabel值的动画。因此,我创建了这个函数Ios 为递增的UILabel设置动画,ios,swift,animation,grand-central-dispatch,swift2,Ios,Swift,Animation,Grand Central Dispatch,Swift2,我试图在视图出现时设置UILabel值的动画。因此,我创建了这个函数 private func animateIncrementUILabel(label: SpringLabel, labelValue: Int, animationPeriod: Float?) { animationPeriod != nil ? animationPeriod! : 40 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_P
private func animateIncrementUILabel(label: SpringLabel, labelValue: Int, animationPeriod: Float?)
{
animationPeriod != nil ? animationPeriod! : 40
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), {
for (var i = 0; i < labelValue; i++)
{
usleep(UInt32(animationPeriod!)/100 * 1000000)
dispatch_async(dispatch_get_main_queue(), {
label.text = String(i)
})
}
})
}
它在标签中直接显示值,而不显示“动画增量”
我做错了什么?在这种情况下,不要使用
usleep
和dispatch\u async
。我建议您在之后使用dispatch\u:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(i * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
<#code to be executed after a specified delay#>
});
您还可以将一些参数传递给计时器:
func someFunc(){
var arr = ["one", "two"]
var timer: NSTimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("val:"), userInfo: arr, repeats: true)
}
func val(timer: NSTimer){
//You'll get an array in timer.userInfo
// arr = timer.userInfo.
// Now you'll get you data in each index of arr
}
删除那些分派块并使用NSTimer.schedule活动而不是sleep,但我想给出几个参数。。如何使用NSTimer实现这一点?你能举个例子吗?但这样我不能强迫给出某些参数。这都是可选的
NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: "update", userInfo: nil, repeats: true)
func someFunc(){
var arr = ["one", "two"]
var timer: NSTimer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: Selector("val:"), userInfo: arr, repeats: true)
}
func val(timer: NSTimer){
//You'll get an array in timer.userInfo
// arr = timer.userInfo.
// Now you'll get you data in each index of arr
}