iPhone Developer iPhone Developer - 3 months ago 99
iOS Question

UIScrollView Direction RTL for Right to Left Languages

Can we change the behavior of UIScrollView that it scrolls RTL content in it's reverse mode.

Answer
count = 6;

[self.scrollView setFrame:CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, 320, 480)];

[pageControl setNumberOfPages:count];

self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * count, self.scrollView.frame.size.height);

for(int i=count-1; i>=0; i--) { //start adding content in reverse mode
    CGRect frame;
    frame.origin.x = self.scrollView.frame.size.width * (count - i - 1); //ar
    frame.origin.y = 0;
    frame.size = self.scrollView.frame.size;

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
    [imageView setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%i.png", i]]];

    [self.scrollView addSubview:imageView];
}

//scroll to the last frame as it's the first page for RTL languages
CGRect frame;
frame.origin.x = scrollView.frame.size.width * (count - 1);
frame.origin.y = 0;
frame.size = scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:NO];

The page control also needs to be indicate the last dot as the first dot (or first page)

- (void)scrollViewDidScroll:(UIScrollView *)sender {
// Update the page when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = (page % count);
}
Comments