1

I want to create a bar with buttons inside it. Each button width is 43 pixels. I want to make this scrollview page-enabled but i don't know how to set the page width 43 pixels so it will stop exactly on the center of each button.

I've tried doing that:

#define scrollViewButtonWidth 43

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSLog(@"x offset = %f",scrollView.contentOffset.x);

    int currentX = scrollView.contentOffset.x;
    int wantedX;

    if (currentX % scrollViewButtonWidth < scrollViewButtonWidth / 2) {
        wantedX = (currentX / scrollViewButtonWidth) * scrollViewButtonWidth;
    }else{
        NSLog(@"%d",(currentX / scrollViewButtonWidth));
        wantedX = ((currentX / scrollViewButtonWidth) * scrollViewButtonWidth) + scrollViewButtonWidth;
    }

    [UIScrollView beginAnimations:@"scrollAnimation" context:nil];
    [UIScrollView setAnimationDuration:0.3f];
    [scrollView setContentOffset:CGPointMake(wantedX, 0)];
    [UIScrollView commitAnimations];

}

I tried putting this code in each and every delegate method of scrollview but non did a good job. It didn't move smoothly like it should - and just looked really bad.

Can anyone help me with that?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
user2328703
  • 227
  • 2
  • 11

0 Answers0