UIScrollView Direction RTL for Right to Left Langu

2019-04-25 08:52发布

问题:

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

回答1:

You can achieve it by rotating UIScrollView in Child views by 180 (Pi).

self.scrollView.transform = CGAffineTransformMakeRotation(M_PI);
subView.transform =  CGAffineTransformMakeRotation(M_PI);


回答2:

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);
}


回答3:

I try it for xCode 7 and iOS 9 work good.

[self._imagesScrollView  setTransform:CGAffineTransformMakeScale(-1, 1)];


回答4:

I m not sure if I understand the question correctly but I take it like this: You want to have a (let's say) horizontal UIScrollView where you can add elements from right to left and when the contents exceed the scroll view frame you want to be able to scroll to the left rather than to the right in order to see the last elements.

In this case you can do this. For every new element calculate the total width you need and set it as contentOffset:

[scrollView setContentSize:CGSizeMake(self.childView.frame.size.width,
                                      scrollView.frame.size.height)]; 

Above I have a childView where the elements are added. It is placed at the right most position inside the scrollView minus 1 element width. Every time an element is added call:

[scrollView setContentOffset:CGPointMake(scrollView.contentOffset.x + kElementWidth,0.0)];

which will offset everything to the left. Then:

self.videoQueueCollectionView.center = CGPointMake(self.childView.center.x + kElementWidth,
                                                   self.childView.center.y);

That will snap eveything back to where it was BUT with the contentOffset set to the left. Which means that you can now scroll to the right.



回答5:

I use this code for Swift 3

self.scrollView.transform = CGAffineTransform(scaleX:-1,y: 1);