我在處理旋轉時在iPhone上使用UIScrollView時遇到了問題。我使用Apple的PageControl示例作爲模型,但是我自己在View中創建了scrollview。使用分頁旋轉UIScrollView的問題
1)滾動視圖按預期工作在默認的縱向加載時,向左滾動向右水平
2)開箱,旋轉CW和重新初始化時,大多把它在其一側,現在旋轉垂直向下,而不是期望的水平靜止。看起來scrollview框架的寬度/高度不會改變,只是它自己的方向。
3)我試圖添加一些方向檢測邏輯,以正確的大小和頁面滾動視圖的方向,這種工作方式,但只有當我將CCW旋轉到橫向。如果我去CW,它會從右邊開始滾動到左邊,並且不起作用。
很明顯,這是越來越hackish,所以有沒有一種適當的方式來設置我的UIScrollview並重新使它旋轉,所以它總是從左到右滾動?
- (void)loadView {
[self setupPage];
}
-(void) setupPage {
// lazy init view controllers
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i = kNumberOfPages) return;
// replace the placeholder if necessary
MyViewController *controller = [viewControllers objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
controller = [[MyViewController alloc] initWithPageNumber:page];
[viewControllers replaceObjectAtIndex:page withObject:controller];
[controller release];
}
// add the controller's view to the scroll view
if (nil == controller.view.superview) {
CGRect frame = scrollView.frame;
int x = 0;
int y = 0;
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if(orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight){
x = 0;
y = frame.size.height * page;
} else {
x = frame.size.width * page;
y = 0;
}
frame.origin.x = x;
frame.origin.y = y;
controller.view.frame = frame;
[scrollView addSubview:controller.view];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
// which a scroll event generated from the user hitting the page control triggers updates from
// the delegate method. We use a boolean to disable the delegate logic when the page control is used.
if (pageControlUsed) {
// do nothing - the scroll was initiated from the page control, not the user dragging
return;
}
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = scrollView.frame.size.width;
int offset = 0;
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if(orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight){
pageWidth = scrollView.frame.size.height;
offset = scrollView.contentOffset.y;
} else {
pageWidth = scrollView.frame.size.width;
offset = scrollView.contentOffset.x ;
}
//CGFloat pageWidth = scrollView.frame.size.width;
int page = floor((offset - pageWidth/2)/pageWidth) + 1;
pageControl.currentPage = page;
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
// A possible optimization would be to unload the views+controllers which are no longer visible
}
// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
pageControlUsed = NO;
}
// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
pageControlUsed = NO;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[self setupPage];
}