如何延迟 swift 逐个显示子视图
How to show subviews one by one with delay on swift
首先,我刚刚开始 swift 的工作。
我正在 for 循环中创建图像视图,并尝试通过一些延迟逐一显示它们。我试图在 viewDidAppear 函数中实现这一点,但是所有图像视图都显示在一起,没有任何延迟,即使我使用以下函数:
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
var i = 1
var backgroundQueue:dispatch_queue_t = dispatch_queue_create("SerialQueue", DISPATCH_QUEUE_SERIAL)
for award in (awards){
//dispatch_async(backgroundQueue, { () -> () in
var awardView = AwardView(imageName: award, sideLength: awardSize)
var pointX: CGFloat = xOffset + CGFloat(i-1) * (awardSize + COIN_MARGIN)
var pointY: CGFloat = yOffset
awardView.center = CGPointMake( pointX, pointY)
//dispatch_async(dispatch_get_main_queue(), { () -> () in
self.pointsContainerView.addSubview(awardView)
audioController.playEffect(SOUND_DING)
//})
//});
i++
}
我不确定我是否在尝试以正确的方式执行此操作 (viewDidAppear)。
在此先感谢您的帮助。
我想你想按顺序加载子视图。这会在后台连续运行线程,一旦它获得 UIView,它就会更新屏幕并等待一秒钟,然后再加载下一个 UIView。
你可以试试这个方法希望对你有帮助
var awards:NSArray=NSArray(objects: "1.jpg","2.jpg","3.jpg")
var x:CGFloat=10.00
var y:CGFloat=100.00
var backgroundQueue:dispatch_queue_t = dispatch_queue_create("SerialQueue", DISPATCH_QUEUE_SERIAL)
for award in awards{
dispatch_async(backgroundQueue, { () -> () in
// do some task
dispatch_async(dispatch_get_main_queue(), { () -> () in
var image:UIImage=UIImage(named: award as String)!
var awardView :UIImageView = UIImageView(image:image)
awardView.frame=CGRectMake(x, y, 300, 400);
self.view.addSubview(awardView);
x=x+310.00;
})
sleep(1);
});
}
首先,我刚刚开始 swift 的工作。
我正在 for 循环中创建图像视图,并尝试通过一些延迟逐一显示它们。我试图在 viewDidAppear 函数中实现这一点,但是所有图像视图都显示在一起,没有任何延迟,即使我使用以下函数:
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
var i = 1
var backgroundQueue:dispatch_queue_t = dispatch_queue_create("SerialQueue", DISPATCH_QUEUE_SERIAL)
for award in (awards){
//dispatch_async(backgroundQueue, { () -> () in
var awardView = AwardView(imageName: award, sideLength: awardSize)
var pointX: CGFloat = xOffset + CGFloat(i-1) * (awardSize + COIN_MARGIN)
var pointY: CGFloat = yOffset
awardView.center = CGPointMake( pointX, pointY)
//dispatch_async(dispatch_get_main_queue(), { () -> () in
self.pointsContainerView.addSubview(awardView)
audioController.playEffect(SOUND_DING)
//})
//});
i++
}
我不确定我是否在尝试以正确的方式执行此操作 (viewDidAppear)。
在此先感谢您的帮助。
我想你想按顺序加载子视图。这会在后台连续运行线程,一旦它获得 UIView,它就会更新屏幕并等待一秒钟,然后再加载下一个 UIView。 你可以试试这个方法希望对你有帮助
var awards:NSArray=NSArray(objects: "1.jpg","2.jpg","3.jpg")
var x:CGFloat=10.00
var y:CGFloat=100.00
var backgroundQueue:dispatch_queue_t = dispatch_queue_create("SerialQueue", DISPATCH_QUEUE_SERIAL)
for award in awards{
dispatch_async(backgroundQueue, { () -> () in
// do some task
dispatch_async(dispatch_get_main_queue(), { () -> () in
var image:UIImage=UIImage(named: award as String)!
var awardView :UIImageView = UIImageView(image:image)
awardView.frame=CGRectMake(x, y, 300, 400);
self.view.addSubview(awardView);
x=x+310.00;
})
sleep(1);
});
}