Location of the object after scrolling in UIScrollView

Posted by ludo on Stack Overflow See other posts from Stack Overflow or by ludo
Published on 2010-04-23T02:34:09Z Indexed on 2010/04/23 2:43 UTC
Read the original article Hit count: 369

Filed under:
|
|

Hi,

I have a scrollView (width:320px height:100px) on my UIView, inside it I add 10 images with width:106.5px each. After that I use this function to have 3 different parts inside my scrollView, so everytime I scroll, an image will automatically be center.


-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

    [self returnToPosition:self.scrollView];
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    [self returnToPosition:self.scrollView];
}

-(void)returnToPosition:(UIScrollView *)scrollView {

    CGFloat itemWidth = 106.5f;
    CGFloat position = [self.scrollView contentOffset].x;
    CGFloat newPosition = 0.0f;
    CGFloat offSet = position / itemWidth;
    NSUInteger target = (NSUInteger)(offSet + 0.5f);

    newPosition = target * itemWidth;

    [self.scrollView setContentOffset:CGPointMake(newPosition, 0.0f) animated:YES];

}

Here is my question, I want to be able to know which image will be in the middle position after the user scroll the View, because I want to display on my UIView a text, specific to that image. But I don't know how to do that.

Some Ideas?

© Stack Overflow or respective owner

Related posts about uiscrollview

Related posts about objective-c