내 케이스 :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
나는 그것을 시도했다고 생각합니다. 그것은 작동하지 않았다. numPages - 1은 마지막으로 돌아 가기 전에 이전 페이지로 돌아갑니다. 7 페이지가 있습니다. 6 번째 페이지로 이동합니다. 그러나 오른쪽 또는 왼쪽으로 스크롤하면 원래 페이지 1,2,3 등으로 변경됩니다. 코드에서 변경해야하는 내용이 있어야 역상이됩니다. – viper15
코드를 실제로 사용해 봐야합니다. 나는 오늘 그것을 나중에 시험해보고,보다 완벽한 해결책을 게시 할 것이다. – Itay
안녕 Itay, 고마워요. 많은 사람! 그게 좋을거야. 고맙습니다. – viper15