简体   繁体   中英

UIScrollView Direction RTL for Right to Left Languages

我们是否可以更改UIScrollView在其反向模式下滚动RTL内容的行为。

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

self.scrollView.transform = CGAffineTransformMakeRotation(M_PI);
subView.transform =  CGAffineTransformMakeRotation(M_PI);
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);
}

我试着为xCode 7和iOS 9做的很好。

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

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.

我将此代码用于Swift 3

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM