1

I'm using altered sample code from PhotoScroller within my app. I have a table view of image thumbnails, and I can pass the array of images that populate that table to PhotoViewController. Currently, PhotoViewController starts with the first image in my array and I can scroll back and forth. This works properly as Apple's sample code.

Now What I want to do is tap a table cell with thumbnail, and start scrolling images beginning with the image in my array at that index. Ex: if I have 5 images in a table and I tap image #3, I want the first image in PhotoViewController to be that third image, and able to scroll left or right to #2 or #4. Hope this makes sense.

I see in PhotoViewController that sub views are being added per image. Any way I can tell it "jump to view #3" without destroying the other views or their overall order of appearance? Any ideas or advice is welcome. Code can be found on the iOS developer site for PhotoScroller sample code.

Ok, I'm rambling... Thanks in advance for your help!

RodneyJ725
  • 43
  • 9

1 Answers1

1

The way I do this is to have a variable called startingPage which gets set in the initialiser of the photo view controller. Then when the pages are being created, first set the correct offset for the scroll view.

So in the PhotoScroller case that would be in loadView. Like so:

- (void)loadView 
{
    // Step 1: make the outer paging scroll view
    CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
    pagingScrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
    pagingScrollView.pagingEnabled = YES;
    pagingScrollView.backgroundColor = [UIColor blackColor];
    pagingScrollView.showsVerticalScrollIndicator = NO;
    pagingScrollView.showsHorizontalScrollIndicator = NO;
    pagingScrollView.contentSize = [self contentSizeForPagingScrollView];
    pagingScrollView.delegate = self;
    self.view = pagingScrollView;

    // Set the content offset of the scrollview
    CGRect bounds = pagingScrollView.bounds;
    CGPoint contentOffset = CGPointMake(bounds.size.width * startingPage, 0.0f);
    [pagingScrollView setContentOffset:contentOffset animated:NO];

    // Step 2: prepare to tile content
    recycledPages = [[NSMutableSet alloc] init];
    visiblePages  = [[NSMutableSet alloc] init];
    [self tilePages];
}
mattjgalloway
  • 34,792
  • 12
  • 100
  • 110
  • Hello, Matt! Thanks for your help. I've tried your solution, but the behavior I'm getting is a blank initial view, regardless of my value for startingPage (or in my case selectedPhotoIndex). If I "scroll" that blank page, I then get the first image in my array. The rest of PhotoViewController is as in the source code, with the exception of the "Image Wrangling" section, which was updated to use my array of images. I tried to post my code, but seems there is a limited number of characters to be entered in a comment... – RodneyJ725 Dec 22 '11 at 15:09
  • Ok, let me take a look at it shortly - I'll try to supply an example showing how you'd do it with the PhotoScroller code. Sorry, my initial code there must not quite be enough then :-(. – mattjgalloway Dec 22 '11 at 15:23
  • Just to experiment, I discover if I set "animated" to YES, suddenly I get the correct behavior of going to the proper image, albeit with a super fast scroll of the preceding images. Setting it back to NO, I get the behavior I described above (blank, and if scroll the blank page, it goes to the first image regardless). – RodneyJ725 Dec 22 '11 at 15:25
  • Just wanted to say thanks for your time and help. I think your solution must be very close. I wish I could post my own code as maybe the issue is easy to spot if it's there. – RodneyJ725 Dec 22 '11 at 15:27
  • Just FYI. I came across this stack overflow post about setContentOffset not working unless set to YES. maybe will help: [link]http://stackoverflow.com/questions/2917460/setcontentoffset-only-works-if-animated-is-set-to-yes[link] – RodneyJ725 Dec 22 '11 at 16:05
  • Hmm ok so my code works for me if added to the standard PhotoScroller demo app. Here's my patch against the freshly downloaded PhotoScroller (I had to change the project file & `MainWindow.xib` as well to make it work properly) - http://iphone.galloway.me.uk/public/PhotoScrollerStartingPage.patch - To apply the patch, unzip the PhotoScroller example somewhere then open a terminal, `cd` to the root folder of the example project then run: `patch -p1 < PhotoScrollerStartingPage.patch`. – mattjgalloway Dec 22 '11 at 16:38
  • As for why it's not working for you, maybe you're not setting the content offset *before* laying out your pages? You need to set the content offset before you decide which pages you need to layout. – mattjgalloway Dec 22 '11 at 16:41
  • Hi Matt. I'll give your patch a try. My code is exactly as yours above with the exception of my variable name in place of your startingPage, so I think content offset is being set prior to laying out the pages. – RodneyJ725 Dec 22 '11 at 17:00
  • @RodneyJ725 what version of iOS are you trying it on? I was trying it on iOS 5 but I'm pretty sure it should work fine on 4 as well anyway. – mattjgalloway Dec 22 '11 at 17:10
  • your patch works perfectly with PhotoScroller. If I set startingPage to 2, I get the 2nd image, and can scroll to the first or third with no issues. I need to see what is different with my code. So far, all looks the same. I forgot to mention that I'm using iOS 5 and storyboard. From my tableview i segue to a view controller with class as PhotoViewController. Not sure that makes any difference. Thanks again for your help. – RodneyJ725 Dec 22 '11 at 17:17
  • Yeh I've not actually looked at story boarding at all yet. Although there probably are subtleties that are different, but I can't see how that would affect things here. I'd breakpoint in the code that lays out the pages and check it's loading the required pages the first time. – mattjgalloway Dec 22 '11 at 17:18
  • got it. :) works fine with "animated:NO" if I segue to my PhotoViewController via push instead of modal. I then had the issue that the image picked (if the picked image is not the first or last) would have an extra duplicate of itself in the "padding" area (as if the padding was filled with a thin slither of the picked image, and it sat 32points lower that tat picked image...go figure). Since I'm using a navigation bar, I had to set 0.0f for y in your example to "-32.0". That fixed the issue with all images displaying properly, except now the first image sits too low hahaha. (cont below) – RodneyJ725 Dec 23 '11 at 04:58
  • now the issue is the very first image will be the correct size to fit within the viewable area, but sits approximately 16 points lower than the nab bar. if i scroll away, and back to the first image, it appears perfect. I've tried playing with y - 16 if selected image index was 0 (first image) but it is the same behavior. I will look again in storyboard as maybe something for the mode (aspect fill, aspect fit, etc) may help. Anyway, it now works except for that issue with the first image.... :) somehow I'm happier now even with image one being a mess: at least I'm closer lol – RodneyJ725 Dec 23 '11 at 05:00
  • solved: I had to update FrameForPagingScrollingView with specific bounds values. – RodneyJ725 Dec 26 '11 at 00:46
  • I found that PhotoScroller seems to ignore when a navigation bar is present. (With iOS 5 and xib files, it worked). I solved the issue by defining in frameForPagingScrollView a frame with CGRect of X = 0, Y= 64 (20 for status bar + 44 for Nav bar), width = 320 and Height = 416 (480-Nav&Stat bars), when using storyboard. All else was left as in sample code. Then following Matt's solution above, I was able to select any frame, but only it works only if segue to the view controller with "push." [pagingScrollView setContentOffset:contentOffset animated:NO] will not work if segue with modal. – RodneyJ725 Dec 26 '11 at 00:53