3

I've started working in objective C in the last couple of days and I'm really struggling to understand uiscrollview. I want to have the view move around an image that is larger than the screen dimensions and possibly zoom in/out each time the screen is tapped. In order to try a few early tests, I wrote a simple code sequence below. The code commented as (2) works and I see the brief animation after a build and run, but (1) doesn't. The image just sits there and doesn't move. I can see to get the scrolling to work at all using zoomToRect which is a problem because I want to zoom to a smaller part of the image rather than just move there at the same zoom level.

I'm sure I'm misunderstanding something fundamental about how this works. Could someone tell me where I'm going wrong (I haven't coded any of this up into classes/methods yet) - just trying to get a feel for how zoomToRect actually works because I think it's what I need.

UIImage *myFirstImage = [UIImage imageNamed:@"page.jpg"];

UIScrollView * myFirstScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 360, 480)];

[myFirstScrollView setContentSize:CGSizeMake(myFirstImage.size.width, myFirstImage.size.height)];

UIImageView *myFirstImageView = [[UIImageView alloc] initWithImage:myFirstImage];

[myFirstImageView setFrame:CGRectMake(0, 0, myFirstImage.size.width, myFirstImage.size.height)];

[myFirstScrollView addSubview:myFirstImageView];

[self.view addSubview:myFirstScrollView];

// 1

[myFirstScrollView zoomToRect:CGRectMake(300, 300, 360, 480)
    animated:YES];  // THIS DOESN'T WORK

// 2

[myFirstScrollView scrollRectToVisible:CGRectMake(300, 300, 360, 480) 
    animated:YES]; // THIS DOES WORK
Nande
  • 637
  • 1
  • 7
  • 18

1 Answers1

0

Here's an answer that may be helpful for you

content size of UIScrollView

your scroll view contentSize is the same as the image size. so at a zero zoom scale, it will not scroll.

Do checkout the SO link, that'll help you.

Community
  • 1
  • 1
Nitin Alabur
  • 5,812
  • 1
  • 34
  • 52