London
London

Reputation: 15274

Advice for horizontal scroll view IOS

I'm trying to create horizontal scroll view(s). Each view containing one image. [done]

Initially when app is opened I'd display couple of images i.e 3, so user can scroll back and forth between images. [done]

However I want to be able to go to another view controller and pick another image(s) maybe two for example and display five images in the scroll view instead of displaying 3 initially.

How would one do that? to "re-refresh" the initial scroll view ?

Update Should I use delegate for this communication between view controllers? or how is this done? 1 main controller, other containing image selection?

This part above and much more explained by article here.(not advertising, I hope it will help someone as well).

Bounty update part 1 :

I think now that I found my way around delegates, I have additional question that I cannot find answer to, all examples I saw were about updating table views.

Bounty part 2 : If I were to have a scrollview inside my view controller and some nsimages inside scroll view. i.e :

- (void)viewDidLoad {
    [super viewDidLoad];

    NSMutableArray *images = ...some images array;
    for (int i = 0; i < images.count; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;

        UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];
        subview.image = [images objectAtIndex:i];
        [self.scrollView addSubview:subview];
    }

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height);
}

And let say my view controller implemented some delegate method didAddImage or didRemoveImage.

Meaning that about images array would get updated.

Actual bounty question :

How would one actually "tell" the view controller, O.K now your scrollview has one more image to display, please re-fresh or reload?

If I were to have a table view instead scroll view and was inserting images I'd do it something like this(in my delegate method) :

-(void) mockDelegatetablemethod:(...) ...{
 [self.images addObject:image];
    NSIndexPath *indexPath = 
     [NSIndexPath indexPathForRow:[self.images count] - 1 
       inSection:0];
    [self.tableView insertRowsAtIndexPaths:
      [NSArray arrayWithObject:indexPath] 
       withRowAnimation:UITableViewRowAnimationAutomatic];
    [self dismissViewControllerAnimated:YES completion:nil];
}

How would one do this to the scrollview?

Bounty update part 3:

This is "simple" case described above, naturally I'd have to support removing images also as well as if one wants to remove all images and add some new.

Upvotes: 1

Views: 5853

Answers (3)

NSIntegerMax
NSIntegerMax

Reputation: 542

This is already implemented by Apple in their iOS sample project "PhotoScroller". It has view reuse as well. They even have a WWDC 2010 video on this. I think its advanced scrollview techniques OR designing apps with scrollviews. Shouldn't be hard to port over to mac

Upvotes: 2

mattjgalloway
mattjgalloway

Reputation: 34912

How would one actually "tell" the view controller, O.K now your scrollview has one more image to display, please re-fresh or reload?

Well, you would just add the new image as a subview of the scroll view. So something like:

- (void)addImage:(UIImage*)image {
    [images addObject:image];

    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * images.count;
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;

    UIImageView *subview = [[UIImageView alloc] initWithFrame:frame];
    subview.image = image;
    [self.scrollView addSubview:subview];

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * images.count, self.scrollView.frame.size.height);
}

Or you could have a method that goes and "resets" the scroll view by removing all subviews and then re-adds all the images in your images array. Then call this method in viewDidLoad instead of your code there so that you're not duplicating code.

Basically, there's no silver bullet here. You roll your own.

Upvotes: 10

jsd
jsd

Reputation: 7693

You're going to need make your image scroller a class that you can tell to load up certain images. You'll need a reference to the existing image scroller, pass that pointer into your other "pick more images" controller. Then the "pick more images" controller can tell the image scroller that there is a new list of images.

Upvotes: 2

Related Questions