2012-04-17 31 views
50

Tôi đang tạo chế độ xem trình chiếu đơn giản trong ứng dụng của mình. Tôi muốn liên kết UIPageControl với UIScrollView của mình. Điều này không quá khó, nhưng tôi đã không thể tìm thấy một giải pháp đơn giản ở bất cứ đâu. Dưới đây là mã của tôi.Lập trình Liên kết UIPageControl với UIScrollView

HelpViewController.h

#import <UIKit/UIKit.h> 

@interface HelpViewController : UIViewController{ 
} 
@end 

HelpViewController.m

#import "HelpViewController.h" 

@interface HelpViewController() 

@end 

@implementation HelpViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    CGRect scrollViewFrame = CGRectMake(0, 62, 320, 404); 
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:scrollViewFrame]; 
    [self.view addSubview:scrollView]; 
    CGSize scrollViewContentSize = CGSizeMake(640, 404); 
    [scrollView setContentSize:scrollViewContentSize]; 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(200, 200, 50, 21)]; 
    [label setText:@"Hello"]; 
    [scrollView addSubview:label]; 
    [scrollView setPagingEnabled:YES]; 
    scrollView.showsHorizontalScrollIndicator = NO; 
    UIPageControl *pageControl = [[UIPageControl alloc] init]; 
    pageControl.frame = CGRectMake(110,5,100,100); 
    pageControl.numberOfPages = 2; 
    pageControl.currentPage = 0; 
    [self.view addSubview:pageControl]; 
    pageControl.backgroundColor = [UIColor redColor]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

@end 
+0

Tôi đã trả lời chính xác tại đây https://stackoverflow.com/questions/3533047/how-do-you-combine-uiscrollview-with-uipagecontrol-to-show-different-views/14230698#14230698 tốt hơn để có câu trả lời một nơi… Tôi đã sửa những đề xuất thay đổi ở đó. – Renetik

Trả lời

77

Có lẽ việc này cho bạn

Đừng quên để thiết lập của UIScrollView đại biểu = tự (hoặc bất cứ nơi nào bạn có bộ chọn bên dưới).

- (void)scrollViewDidScroll:(UIScrollView *)scrollView { 
    CGFloat pageWidth = self.scrollView.frame.size.width; // you need to have a **iVar** with getter for scrollView 
    float fractionalPage = self.scrollView.contentOffset.x/pageWidth; 
    NSInteger page = lround(fractionalPage); 
    self.pageControl.currentPage = page; // you need to have a **iVar** with getter for pageControl 
} 

Đối với mã của bạn nó sau đó sẽ là:

tập tin .h

#import <UIKit/UIKit.h> 

@interface HelpViewController : UIViewController{ 
} 

@property (nonatomic, retain) UIScrollView *scrollView; 
@property (nonatomic, retain) UIPageControl * pageControl; 
@end 

tập tin .m

#import "HelpViewController.h" 

@interface HelpViewController() 

@end 

@implementation HelpViewController 
@synthesize scrollView=scrollView_; 
@synthesize pageControl=pageControl_; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    CGRect scrollViewFrame = CGRectMake(0, 62, 320, 404); 
    self.scrollView = [[[UIScrollView alloc] initWithFrame:scrollViewFrame] autorelease]; 
    self.scrollView.delegate = self; 
    [self.view addSubview:self.scrollView]; 
    CGSize scrollViewContentSize = CGSizeMake(640, 404); 
    [self.scrollView setContentSize:scrollViewContentSize]; 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(200, 200, 50, 21)]; 
    [label setText:@"Hello"]; 
    [self.scrollView addSubview:label]; 
    [self.scrollView setPagingEnabled:YES]; 
    self.scrollView.showsHorizontalScrollIndicator = NO; 
    self.pageControl = [[[UIPageControl alloc] init] autorelease]; 
    self.pageControl.frame = CGRectMake(110,5,100,100); 
    self.pageControl.numberOfPages = 2; 
    self.pageControl.currentPage = 0; 
    [self.view addSubview:self.pageControl]; 
    pageControl.backgroundColor = [UIColor redColor]; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView { 
     CGFloat pageWidth = self.scrollView.frame.size.width; 
     float fractionalPage = self.scrollView.contentOffset.x/pageWidth; 
     NSInteger page = lround(fractionalPage); 
     self.pageControl.currentPage = page; 
    } 


@end 
+0

Cảm ơn bạn đã hỗ trợ! Logic là âm thanh, nhưng tôi không hoàn toàn hiểu được việc thực hiện. Tôi đã cập nhật bài đăng của mình để bao gồm các tệp .h và .m đầy đủ của tôi. Đã cập nhật –

+0

! chỉ cần sao chép mã .. :) ... nó chưa được kiểm tra, có thể là lỗi đánh máy. Nhưng nó sẽ hoạt động. –

+0

Cảm ơn bạn rất nhiều. Bạn đã cứu tôi rất nhiều đau đầu.Vấn đề của tôi đã kết thúc với người được ủy quyền. –

2

để có được chỉ số trang chính xác mà không làm tròn giá trị đơn giản đặt pagecontrol.currentpage trong phương thức scrollViewDidEndDecelerating delegate của chế độ xem cuộn.Điều này phù hợp với tôi!

51

Điều này thực sự khá đơn giản để thiết lập. Trước tiên, bạn cần tạo chế độ xem cuộn và kiểm soát trang. Đảm bảo bạn triển khai UIScrollViewDelegate trong giao diện của lớp học.

UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)]; 
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 3, scrollView.frame.size.height); 
    scrollView.delegate = self; 

    UIPageControl * pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 90, scrollView.frame.size.width, 20)]; 
    pageControl.numberOfPages = scrollView.contentSize.width/scrollView.frame.size.width; 
    [pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged]; 

Sau đó, bạn cần phải thêm hai phương pháp sau đây:

- (IBAction)changePage:(id)sender { 
    CGFloat x = pageControl.currentPage * scrollView.frame.size.width; 
    [scrollView setContentOffset:CGPointMake(x, 0) animated:YES]; 
} 

-(void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView { 
    NSInteger pageNumber = roundf(scrollView.contentOffset.x/(scrollView.frame.size.width)); 
    pageControl.currentPage = pageNumber; 
} 

Những phương pháp thêm thông tin liên lạc cần thiết giữa sự kiểm soát trang và xem di chuyển.

+0

Để xác định chính xác pageNumber bạn cần nhiều bởi pageControl.numberOfPages –

0

Tất cả các câu trả lời này đều tuyệt vời nhưng tôi muốn cung cấp giải pháp thay thế, sử dụng ReactiveCocoa!

Mã sau đây giả định rằng bạn có thuộc tính được gọi là scrollView và được gọi là pageControl.

[RACObserve(self.scrollView, contentOffset) 
    subscribeNext:^(NSValue* value){ 
     CGPoint offset = [value CGPointValue]; 
     CGFloat fractional = offset.x/self.scrollView.width; 
     [self.pageControl setCurrentPage:lroundf(fractional)]; 
    }]; 

Tất cả những gì xảy ra ở đây là bạn đang quan sát sự thay đổi trên tài sản của scrollview contentOffset và tính toán các chỉ số trang trên mỗi sự kiện tiếp theo (ví dụ: mỗi khi thay đổi giá trị tài sản).

Nhược điểm duy nhất của việc này là bạn phải mở hộp số CGPoint nhưng ở phía trên không có mã số UIScrollViewDelegate cần thiết!

Các vấn đề liên quan