0

I am trying to do a slide show like animation on the home page of my app. An image appears in the proper UI View, but it never transitions to a different photo.

EDIT: Trying to set the image before transition view:

- (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];
}

I checked and imageUrls has 660 elements, so its not an issue of not having enough photos. I would be very grateful for any insight/suggestions, I am very new to iOS.

maddie
  • 1,854
  • 4
  • 30
  • 66
  • try setting the image before the transition! set the image first and then animate the image view! – Teja Nandamuri Aug 10 '17 at 17:02
  • I'm trying to understand -- does the code I added to attempt your solution seem reasonable? I think Im still doing something wrong – maddie Aug 10 '17 at 17:53

1 Answers1

0

I have taken two images for demo purpose, and i have changed the view's tag on swipe action to change the image based on tag value. The imageView transitions from one photo to another. Below the code is given where slideView is an UIImageView.

Swift3

Inside viewDidLoad method:

slideView.image = UIImage(named: "image1")
slideView.tag = 0

On Swipe action called the below method:

func beginSlideShow() {

    UIView.transition(with: slideView, duration: 1.0, options: [.transitionCrossDissolve, .curveEaseOut], animations: { 
        if self.slideView.tag == 1 {
            self.slideView.image = UIImage(named: "image2")
        } else {
            self.slideView.image = UIImage(named: "image1")
        }
    }, completion: { (finished: Bool) in
        if finished {
            NSLog("animation finished")
        }
    })
}

Objective-C

- (void) beginSlideShow {

[UIView transitionWithView:_slideView duration:1.0 options: UIViewAnimationOptionTransitionCrossDissolve animations:^{
        if (_slideView.tag == 1) {
            _slideView.image = [UIImage imageNamed:@"image2"];
        } else {
            _slideView.image = [UIImage imageNamed:@"image1"];
        }
    } completion:^(BOOL finished) {
        if (finished) {
            NSLog(@"animation finished");
        }
    }];
}
Rahul Kumar
  • 3,009
  • 2
  • 16
  • 22
  • this is helpful, but my project is entirely in Objective C – maddie Aug 10 '17 at 18:22
  • How would I do this with an array with over 600 elements/images? – maddie Aug 10 '17 at 18:38
  • You are having an array of images, using random number generator, you get a particular image that you are already doing which is right. I mean transitionWithView is working if you set the image inside animations code block. – Rahul Kumar Aug 10 '17 at 19:07
  • Call beginSlideShow method inside viewDidAppear method, then you can see the transition and make transition duration 1.0 – Rahul Kumar Aug 10 '17 at 19:11
  • I call beginSlideShow inside viewDidAppear already -- I have changed duration to 1.0, but other than that I don't understand what else I need to make different? – maddie Aug 10 '17 at 19:17
  • Are you getting image on getImage: method call? – Rahul Kumar Aug 10 '17 at 19:37
  • Yes, I get an image on getImage call. Wouldn't I need a loop of some sort? Otherwise, beginSlideShow gets called once, sets one image, and exits? – maddie Aug 10 '17 at 19:43
  • Yes, You need a loop to make transition continuous, call beginSlideShow method inside completion:^(BOOL finished) closure when animation is finished. – Rahul Kumar Aug 11 '17 at 03:22