同步動畫我創建圖像陣列和使用動畫它們:有聲
imageview.animationImages = images;
imageview.animationDuration = 1;
imageview.animationRepeatCount = 1;
[imageview startAnimating];
這裏imageview
是指向對象的UIImageView和images
是數組。我正在使用圓形矩形按鈕來觸發動畫。當我在模擬器打開後第一次運行動畫時......動畫不會與聲音同步,但是在我再次按下按鈕之後使用它一次後,它就會執行動作。任何想法爲什麼?我正在使用系統聲音API播放短暫的聲音。我創建一個id
的聲音,然後使用播放:
AudioServicesPlaySystemSound(soundID);
我的猜測是,當我火了,第一次動畫,仿真需要花費一些時間來處理我使用的圖像,並建立數組。我總共使用了45張圖像。因此,聲音保持在動畫圖像之前。但是當我再次嘗試時,圖像已經被處理,所以動畫和聲音都是同步的。但這只是一個猜測。我不知道該怎麼做。如果你想看看整個代碼在這裏,它是:
-(IBAction)btnclicked:(id)sender {
NSMutableArray *images = [[NSMutableArray alloc]init]; //all alloc does is allocate memory. The -init method is crucial to actually initialize the object into a working state.
for (int imagenumber = 1; imagenumber <= 45; imagenumber++) {
NSMutableString *myString = [NSMutableString stringWithFormat:@"%i.png", imagenumber];
[images addObject:[UIImage imageNamed:myString]];
}
CGRect frame = CGRectMake(0.0, 0.0, 320, 460);
UIImageView *imageview = [[UIImageView alloc] initWithFrame:frame];
imageview.contentMode = UIViewContentModeScaleToFill;
imageview.animationImages = images;
imageview.animationDuration = 3.2;
imageview.animationRepeatCount = 1;
[imageview startAnimating];
[self.view addSubview:imageview];
AudioServicesDisposeSystemSoundID(soundID);
NSString *path = [[NSBundle mainBundle] pathForResource:@"giggity stick around" ofType:@"wav"];
NSURL *url = [NSURL fileURLWithPath:path];
AudioServicesCreateSystemSoundID((CFURLRef)url, &soundID);
AudioServicesPlaySystemSound(soundID);
[imageview release];
}
只是爲了避免這就是爲什麼我建立時使用迭代任何混亂......我的圖片命名爲1.png,2.png等數組。
https://github.com/kstenerud/ObjectAL-for-iPhone – GTSouza 2012-07-14 04:49:46
可能重複的[UIImageView/AVAudioPlayer同步](http://stackoverflow.com/questions/6199015/uiimageview-avaudioplayer-synchronization) – Abizern 2012-07-14 16:29:59