2013-10-24 68 views
2

我正在嘗試動畫化一系列圖像。忽略動畫ImageView的持續時間

圖像之間的變化並不一定有一個動畫,但我使用動畫來控制時間:

-(void)nextImage 
{ 
    [UIView animateWithDuration:0.5 animations:^{ 
     self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"myImage%d",index++]]; 
    }completion:^(BOOL completed){ 
     if (index < 50) 
     { 
      [self nextImage]; 
     } 
    }]; 
} 

圖像被改變,但無論我在持續使用,它忽略時間並儘可能快地進行。

如果我改變α,同樣的情況:

-(void)nextImage 
{ 
    [UIView animateWithDuration:0.5 animations:^{ 
     self.imageView.alpha = 1 - index++/100 
    }completion:^(BOOL completed){ 
     if (index < 50) 
     { 
      [self nextImage]; 
     } 
    }]; 
} 

回答

3

UIView某些屬性是animatable:

@property frame 
@property bounds 
@property center 
@property transform 
@property alpha 
@property backgroundColor 
@property contentStretch 

一個UIImageViewimage屬性不是動畫。

如果你有更新UIImageView內的圖像,而不是使用不同的技術,如塊:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC),  
    dispatch_get_current_queue(), ^{ 
     // update your image 
    }); 

(備選:performSelectorAfterDelay:NSTimer我建議使用塊雖然)。

我認爲您的alpha動畫由於您的分區中的int截斷而不起作用。試試這個:

self.imageView.alpha = 1.0f - (float)index++/100.0f; 

與你原來的分工問題是表達a/b,其中兩個都是整數,作爲整數除法進行。因此,如果a < b,結果將爲0 - 換句話說,對於所有值完全透明的alpha設置。

+0

好吧,但爲什麼動畫的阿爾法不工作? – ReloadC

+0

看到我的答案... –

1

是不可能的[UIView animateDuration:animations:completion]; 嘗試使用的NSTimer並調用改變圖像的每一個步驟的功能做到這一點。只有

-1

爲什麼不使用ImageView的images屬性並設置要設置動畫的圖像數組?

+0

這將簡單地改變顯示的圖像,不會有動畫。 –

+0

請參閱原始問題:「圖片之間的變化不一定要有動畫」 – slecorne

2

阿爾法不工作,因爲你寫

self.imageView.alpha = 1 - index++/100; 

這裏的一切是int,因此您的結果只能是整數值,即1或0。使用此相反:

self.imageView.alpha = 1.0f - index++/100.0f; 

編譯器將能夠隱式轉換index爲float,但你可以明確的寫:

self.imageView.alpha = 1.0f - (CGFloat)(index++)/100.0f; 
0

可以使用的UIImageView動畫屬性太,例如:

// arrayWithImages 
arrayPictures = [[NSMutableArray alloc] initWithCapacity:50]; 

// The name of your images should 
for (int i=0; i<=49; i++) { 
    UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"myImage%d.jpg",i]]; 
    [arrayPictures addObject:image]; 
} 

imageView.animationDuration = 0.5; 
imageView.animationImages = arrayPictures; 

[imageView startAnimating]; 

根據圖像的數量,你可以有一些內存的問題,就必須使用動畫較低級解決方案的圖像。

0

如果他們只是動畫持續時間問題,那麼可能是動畫未啓用,因爲我在我的iOS應用程序中遇到了這個問題。這裏是簡單的解決方案:

只有你需要添加[UIView setAnimationsEnabled:YES];在開始動畫塊之前。所以你的完整代碼將是這樣的:

-(void)nextImage 
{ 
    [UIView setAnimationsEnabled:YES] 
    [UIView animateWithDuration:0.5 animations:^{ 
     self.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"myImage%d",index++]]; 
    }completion:^(BOOL completed){ 
     if (index < 50) 
     { 
      [self nextImage]; 
     } 
    }]; 
}