Dựa trên câu trả lời hoàn toàn chính xác của Ben Gottlieb ở đó, tôi có những gì tôi muốn. Dưới đây là đoạn code tôi sử dụng để thực hiện nó:
.h
@interface MyViewController : UIViewController <UIScrollViewDelegate> {
}
@property (nonatomic, retain) UIPageControl *helpPageCon;
.m
@synthesize helpPageCon;
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGRect frame = [[UIScreen mainScreen] applicationFrame];
float roundedValue = round(scrollView.contentOffset.x/frame.size.width);
self.helpPageCon.currentPage = roundedValue;
}
- (void)viewDidLoad
{
UIScrollView *scrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height-50)] autorelease];
scrollView.contentSize = CGSizeMake(frame.size.width*5, frame.size.height-50);
[scrollView setPagingEnabled:YES];
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.delegate = self;
[self.view addSubview:scrollView];
CGRect frame = [[UIScreen mainScreen] applicationFrame];
self.helpPageCon = [[[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 50)] autorelease];
self.helpPageCon.center = CGPointMake(frame.size.width/2, frame.size.height-75);
self.helpPageCon.numberOfPages = 5;
[self.view addSubview:self.helpPageCon];
}
- (void)dealloc
{
[super dealloc];
[helpPageCon release];
}
Nguồn
2011-09-14 12:36:48
Tôi xin lỗi vì downvote, bây giờ liên kết đang hoạt động trở lại. – hariseldon78