我想在我的應用程序的主頁上做一個像動畫一樣的幻燈片放映。圖像顯示在正確的用戶界面視圖中,但不會轉換爲其他照片。UIView transitionWithView永遠不會轉換到另一張照片
編輯:嘗試設置變化圖之前的圖像:
- (void)viewDidLoad {
[super viewDidLoad];
/*set image before transition */
_slideShow.image = [UIImage imageNamed:@"Slide Show"];
[self fetchSpecies];
[self beginSlideShow];
}
- (void) beginSlideShow{
NSLog(@"Called");
if([imageUrls_ count] < 1){
NSLog(@"EMPTY");
return;
}
[UIView transitionWithView:_slideShow duration:0.2 options: UIViewAnimationOptionTransitionCrossDissolve animations:^{
int index = arc4random() % [imageUrls_ count];
_slideShow.image = [self getImage:index];
// Account for the case where some images just aren't there
while (_slideShow.image == nil) {
index = arc4random() % [imageUrls_ count];
_slideShow.image = [self getImage:index];
}
} completion:^(BOOL finished) {
if (finished){
//logic
}
}];
}
- (void) fetchSpecies{
NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:@ "Species"];
NSError* error = nil;
NSManagedObjectContext* context = [(LeafletAppDelegate*)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSArray* species = [context executeFetchRequest:request error:&error];
if (!species || error) { // nil is an error
NSLog(@"error");
// handle error
}
// Get all the image urls and description to use for the random images
imageUrls_ = [[NSMutableArray arrayWithCapacity:100] retain];
imageDescs_ = [[NSMutableArray arrayWithCapacity:100] retain];
NSString *url;
NSString *name;
for (Species *s in species) {
name = [NSString stringWithFormat:@"%@", [s commonNameFirstLast]];
url = [s.ExampleImageLeaf pathForLocalImageUsingThumbnail:YES];
if (url) {
[imageUrls_ addObject:url];
[imageDescs_ addObject:[NSString stringWithFormat:@"%@", name]];
}
url = [s.ExampleImageFlower pathForLocalImageUsingThumbnail:YES];
if (url) {
[imageUrls_ addObject:url];
[imageDescs_ addObject:[NSString stringWithFormat:@"%@", name]];
}
url = [s.ExampleImageFruit pathForLocalImageUsingThumbnail:YES];
if (url) {
[imageUrls_ addObject:url];
[imageDescs_ addObject:[NSString stringWithFormat:@"%@", name]];
}
}
}
- (UIImage *) getImage:(NSUInteger)index
{
// NSLog(@"empty");
NSString *url = [imageUrls_ objectAtIndex:index];
return [UIImage imageWithContentsOfFile:url];
}
我檢查和imageUrls有660元,所以它不是沒有足夠的照片的問題。我會非常感謝任何見解/建議,我對iOS很新。
嘗試在轉換之前設置圖像!首先設置圖像,然後爲圖像視圖設置動畫! –
我試圖理解 - 我添加的代碼嘗試你的解決方案看起來是否合理?我認爲我仍然做錯了 – Matt