2009-08-19 5 views
1

내 케이스 :UIScrollView의 UIPageControl을 사용하는 응용 프로그램을 시작할 때 왼쪽 (역방향) 대신 오른쪽에 기본 페이지를 설정하는 방법은 무엇입니까?

나는 애플의 원래 페이지 제어 응용 프로그램을 가지고있다. UIScrollView의 맨 아래에있는 페이지 컨트롤의 오른쪽에 앱 실행을 수정하고 싶습니다. 기본적으로 시작할 때 - 왼쪽에있는 첫 페이지에 있습니다. 예를 들어, 나는 7 페이지를 가지고있다. 따라서 첫 번째 페이지가 마지막 페이지 인 7 번째 페이지와 두 번째 페이지의 6 번째 페이지에 표시되도록하고 싶습니다. 즉, 기본값의 반대입니다.

이 트릭을 수행하는 코드가 하나 있어야한다는 것을 알고 있습니다. 나는 그것이 무엇인지 모른다. 도와주세요. 감사! 이 응용 프로그램은 내용의 다른 페이지를 탐색하기위한 메커니즘으로 수평 스크롤을 사용할 수있는 UIScrollView의 페이징 기능을 사용하는 방법을 보여줍니다

PageControl : 아래

는 애플의 페이지 제어 응용 프로그램의 설명이다. 각 페이지는 필요할 때만로드되는 자체보기 컨트롤러에 의해 관리됩니다. UIPageControl은 AppDelegate.m에 페이지

사이에 코드를 이동 대안 인터페이스와 윈도우의 하단에 표시됩니다

#import "AppDelegate.h" 
#import "MyViewController.h" 

static NSUInteger kNumberOfPages = 7; 

@interface AppDelegate (PrivateMethods) 

- (void)loadScrollViewWithPage:(int)page; 
- (void)scrollViewDidScroll:(UIScrollView *)sender; 

@end 

@implementation AppDelegate 

@synthesize window, scrollView, pageControl, viewControllers; 

- (void)dealloc { 
    [viewControllers release]; 
    [scrollView release]; 
    [pageControl release]; 
    [window release]; 
    [super dealloc]; 
} 

- (void)applicationDidFinishLaunching:(UIApplication *)application { 
    // view controllers are created lazily 
    // in the meantime, load the array with placeholders which will be replaced on demand 
    NSMutableArray *controllers = [[NSMutableArray alloc] init]; 
    for (unsigned i = 0; i < kNumberOfPages; i++) { 
     [controllers addObject:[NSNull null]]; 
    } 
    self.viewControllers = controllers; 
    [controllers release]; 

    // a page is the width of the scroll view 
    scrollView.pagingEnabled = YES; 
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height); 
    scrollView.showsHorizontalScrollIndicator = NO; 
    scrollView.showsVerticalScrollIndicator = NO; 
    scrollView.scrollsToTop = NO; 
    scrollView.delegate = self; 

    pageControl.numberOfPages = kNumberOfPages; 
    pageControl.currentPage = 0; 

    // pages are created on demand 
    // load the visible page 
    // load the page on either side to avoid flashes when the user starts scrolling 
    [self loadScrollViewWithPage:0]; 
    [self loadScrollViewWithPage:1]; 
} 

- (void)loadScrollViewWithPage:(int)page { 
    if (page < 0) return; 
    if (page >= 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; 
     frame.origin.x = frame.size.width * page; 
     frame.origin.y = 0; 
     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 page = floor((scrollView.contentOffset.x - 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 end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl 
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { 
    pageControlUsed = NO; 
} 

- (IBAction)changePage:(id)sender { 
    int page = pageControl.currentPage; 
    // 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]; 
    // update the scroll view to the appropriate page 
    CGRect frame = scrollView.frame; 
    frame.origin.x = frame.size.width * page; 
    frame.origin.y = 0; 
    [scrollView scrollRectToVisible:frame animated:YES]; 
    // Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above. 
    pageControlUsed = YES; 
} 

@end 

답변

2

Apple에서 PageControl 샘플을 가져 와서이 작업을 수행하는 코드는 다음과 같습니다. 나는 그것을 테스트하고 잘하고, 첫 번째 점에서 마지막 점으로 애플 리케이션을 시작할 때 페이지 컨트롤 점프를 제외하고,하지만 그 해결 방법을 모르겠다. 이것이 당신을 위해 작동하지 않으면 알려주십시오.

- (void)applicationDidFinishLaunching:(UIApplication *)application { 
    // view controllers are created lazily 
    // in the meantime, load the array with placeholders which will be replaced on demand 
    NSMutableArray *controllers = [[NSMutableArray alloc] init]; 
    for (unsigned i = 0; i < kNumberOfPages; i++) { 
     [controllers addObject:[NSNull null]]; 
    } 
    self.viewControllers = controllers; 
    [controllers release]; 

    // a page is the width of the scroll view 
    scrollView.pagingEnabled = YES; 
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * kNumberOfPages, scrollView.frame.size.height); 
    scrollView.showsHorizontalScrollIndicator = NO; 
    scrollView.showsVerticalScrollIndicator = NO; 
    scrollView.scrollsToTop = NO; 
    scrollView.delegate = self; 

    // First, set the number of pages 
    pageControl.numberOfPages = kNumberOfPages; 
    // Then, tell the page control that you're on the last page (numPages - 1, since we're 0-indexed) 
    pageControl.currentPage = kNumberOfPages - 1; 

    // Now, we have to tell the frame that we're at the end of the frame 
    // So we set the content offset to be the last page 
    scrollView.contentOffset = CGPointMake(scrollView.frame.size.width * (kNumberOfPages - 1), 0); 

    // pages are created on demand 
    // load the visible page 
    // load the page on either side to avoid flashes when the user starts scrolling 
    // [self loadScrollViewWithPage:0]; 
    // [self loadScrollViewWithPage:1]; 

    // Now, instead of loading the original page, we load the last two pages 
    [self loadScrollViewWithPage:kNumberOfPages - 1]; 
    [self loadScrollViewWithPage:kNumberOfPages - 2]; 
} 
+0

나는 그것을 시도했다고 생각합니다. 그것은 작동하지 않았다. numPages - 1은 마지막으로 돌아 가기 전에 이전 페이지로 돌아갑니다. 7 페이지가 있습니다. 6 번째 페이지로 이동합니다. 그러나 오른쪽 또는 왼쪽으로 스크롤하면 원래 페이지 1,2,3 등으로 변경됩니다. 코드에서 변경해야하는 내용이 있어야 역상이됩니다. – viper15

+0

코드를 실제로 사용해 봐야합니다. 나는 오늘 그것을 나중에 시험해보고,보다 완벽한 해결책을 게시 할 것이다. – Itay

+0

안녕 Itay, 고마워요. 많은 사람! 그게 좋을거야. 고맙습니다. – viper15

2

당신은 당신의 scrollViews는 마지막 페이지 오프셋 (offset)를 설정할 수 있습니다 음 그러면 마지막 페이지에서 시작하여 왼쪽 대신 오른쪽으로 스크롤해야합니다. use UIScrollViews - (void) setContentOffset : (CGPoint) contentOffset animated : (BOOL) 애니메이션 메서드입니다.

+0

미안하지만 나는이 물건에 조금은 초보자입니다. setContentOffset을 두어야한다고 가정합니다. 코드에 정확히 어디에 넣으시겠습니까? (코드를 참조 용으로 넣었습니다.) 또한 setContentOffset을 사용한다면 UIPageControl에도 영향을 줍니까? – viper15

+0

어쩌면 바로 scrollView를 인스턴스화 한 후에 p agesControl에 대해 내가 생각하는 활성 페이지를 설정하는 것입니다. – Daniel