[英]Animation without delay (UIView animateWithDuration delay)
我們需要以一定的時間間隔順序顯示10張圖像(默認圖像處於隱藏狀態= yes,並且具有從1111到1120的標簽)。 代碼的結果是立即顯示所有圖像,而不會出現延遲。 動畫是單獨的功能。 可能是什么問題呢? 我正在使用xcode 8.2.1
-(void)doski:(NSInteger)i
{
[UIView animateWithDuration:1.0
delay:5.0
options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionCurveLinear | UIViewAnimationOptionAllowUserInteraction
animations:^(void) {
}
completion:^(BOOL finished) {
[self.view viewWithTag:(i+1110)].hidden=NO;
NSInteger i2=i;
i2++;
if(i2<11)
[self doski:i2];
}];
}
...........
//function call
[self doski:1];
使用此選項時,情況沒有改變:
-(void)doski:(NSInteger)i
{
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut
animations:^(void) {
}
completion:^(BOOL finished){
[UIView animateWithDuration:2.0 delay:1.0 options:
UIViewAnimationOptionCurveEaseIn animations:^{
[self.view viewWithTag:(i+1110)].hidden=NO;
} completion:^ (BOOL completed) {NSInteger i2=i;
i2++;
if(i2<18)
[self doski:i2];}];
}];
}
謝謝。
我認為圖像立即顯示的原因是您的動畫塊為空。 如果動畫塊為空,則會立即調用完成塊(沒有任何動畫可做,所以我們也可以完成)。
要修復您的代碼,您可以在調用animateWithDuration...
之前嘗試類似的animateWithDuration...
UIView *viewToAnimate = [self.view viewWithTag:(i+1110)]
viewToAnimate.alpha = 0
viewToAnimate.hidden = NO
然后將動畫塊內的alpha設置為1,如下所示:
viewToAnimate.alpha = 1
這是一個完整的示例,說明了動畫塊為空的問題。 您可以將其復制並粘貼到操場上。 雖然是Swift,但不是Objective-C
import UIKit
import PlaygroundSupport
class MyView : UIView {
override init(frame: CGRect) {
super.init(frame: frame)
self.setup()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setup() {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
view.backgroundColor = UIColor.red
self.addSubview(view)
UIView.animate(withDuration: 2, delay: 1, options: [], animations: {
// commenting out the following line will
// trigger the completion block immediately
view.frame = CGRect(x: 300, y: 300, width: 50, height: 50)
}, completion: { finished in
print("done")
})
}
}
let view = MyView(frame: CGRect(x: 0, y: 0, width: 400, height: 400))
view.backgroundColor = UIColor.black
PlaygroundPage.current.liveView = view
您的代碼有多個問題。
您不能為視圖的隱藏屬性設置動畫。 隱藏的屬性不可設置動畫。 相反,您需要將Alpha值設置為0並將其動畫化為1.0。 這將導致視圖淡入,我想這就是您想要的。
您的兩個代碼塊在動畫塊內均不執行任何操作。 錯了 在動畫塊內,您需要更改要設置動畫的視圖的一個或多個可設置動畫的屬性。
如果希望一次顯示一個視圖,則應調用animateWithDuration:delay:options:animations:completion:
第一個動畫的延遲值為0,第二個動畫的延遲為這樣,第一個動畫將立即開始,第二個動畫將在第一個動畫結束時開始,依此類推。
您的代碼可能看起來像這樣:
NSTimeInterval duration = 0.25;
for (index = 0; x < 10; x++) {
UIView *viewToAnimate = arrayOfViewsToAnimate[index];
//For each view, set it's hidden property to false
//and it's alpha to zero
viewToAninimate.hidden = false;
viewToAnimate.alpha = 0.0;
[UIView animateWithDuration:duration
delay: duration * index
options: 0
animations: ^ {
//inside the animation block, set the alpha to 1.0
viewToAnimate.alpha = 1.0;
}
completion: nil
];
}
該代碼假定您已創建一個數組arrayOfViewsToAnimate
,其中包含要設置動畫的視圖。 您還可以為視圖分配標簽,並在for循環中使用該標簽查找每個視圖。 無論如何,您都需要使上面的代碼適應您的需求,而不僅僅是將其復制/粘貼到您的程序中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.