22 Oct 2008, 22:19
Littleboy_pragsmall

Roman Busyghin (21 posts)

Our brilliant book (I’m talking about «iPhone SDK Development» ;) doesn’t contain information about UIScrollerView. I’m sad. Can anyone give me an advice how to use it? With help of iPhone Developer Portal I have developed a little example which consists of UIViewController -> UIScrollView -> UIView. When program is loaded it adds UIImageView and UIView as subviews to UIView.

So, the problem is that UIScrollView zooms in incorrectly. While zooming UIImageView grows up but active scrolling area shrinks. Why it happens?

- (void)viewDidLoad {
    [super viewDidLoad];

    self.scrollView.maximumZoomScale = 5.0;
    self.scrollView.minimumZoomScale = 1.0;

    self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"blah-blah.gif"]];

    [self.contentView addSubview: self. imageView];
    [self.scrollView setContentSize: CGSizeMake(self. imageView.frame.size.width, self. imageView.frame.size.height)];
    [self.scrollView setScrollEnabled: YES];

    UIView *rect = [[UIView alloc] initWithFrame: CGRectMake(100.0f, 100.0f, 64.0f, 64.0f)];
    rect.backgroundColor = [UIColor redColor];
    [self.contentView addSubview: rect];

    [rect release];
    [self.imageView release];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *) scrollView
{
    return self.contentView;
}
23 Oct 2008, 02:35
Biopic_100x100_pragsmall

Bill Dudney (917 posts)

Hi Roman,

We plan on covering scroll views in the custom view chapter which will likely appear early in the book (but we are not sure exactly where). It won’t make the next beta but will probably be in the one after that.

One note on your code. You should not be releasing self.imageView that could cause imageView to be a pointer to freed memory. Instead do self.imageView = nil; If you have declared imageView to retain then it will release the pointer in the set method for you.

I’m not sure about the strange behavior you are seeing with the scroll view. The image grows as you expect but the scroll view shrinks?

23 Oct 2008, 09:17
Littleboy_pragsmall

Roman Busyghin (21 posts)

One note on your code. You should not be releasing self.imageView that could cause imageView to be a pointer to freed memory. Instead do self.imageView = nil;

Oh, that’s interesting. Property imageView defined as @property (nonatomic, retain) UIImageView *imageView;. When I’m assigning newly allocated UIImage to the property, it should increase retain counter, right? So, after allocating I have self.imageView with retain count equals to 1. After adding self.imageView as subview to self.contentView it should again increase retain count to 2. Thus, I don’t need self.imageView within loadView: and I’m decreasing retain count. Am I wrong?

Also, setting nil value to the property automatically decreases retain count?

I’m not sure about the strange behavior you are seeing with the scroll view. The image grows as you expect but the scroll view shrinks?

I bet my little example href=”http://idisk.mac.com/nskboy-Public/Scroller.zip”>http://idisk.mac.com/nskboy-Public/Scroller.zip will explain everything. Compile it, zoom at maximum level and try to scroll to bottom right corner. Then zoom out at maximum level and see what happens. UIScrollView will be unusable. May be I forgot to correct frame/contentSize somewhere?

23 Oct 2008, 12:40
Littleboy_pragsmall

Roman Busyghin (21 posts)

Thanks God, the problem is solved. There is a problem with UIView frames. That’s how I should initialize and add UIImageView to self.contentView:

    self.contentView.frame = self.imageView.frame;
    [self.contentView addSubview: self.imageView];
    [self.scrollView setContentSize: self.contentView.frame.size];

Bill, but I still need your reply about releasing self.imageView ;-)

23 Oct 2008, 13:12
Biopic_100x100_pragsmall

Bill Dudney (917 posts)

Hi Roman,

Glad you got it working!

You are correct on the release. I misread your code and thought you were auto releasing. So your release is fine.

  You must be logged in to comment