2014-02-13 76 views
1

這是我的UIScrollView功能,我在一頁中顯示了7張圖片中的7張。它的工作原理,但我想顯示和更改UIPageControl如何在UIScrollView中添加UIPageControl

- (void)layoutScrollImages{ 
    UIImageView *view = nil; 
    NSArray *subviews = [scrollView1 subviews]; 

    curXLoc = 0; 
    for (view in subviews) 
    { 
    if ([view isKindOfClass:[UIImageView class]] && view.tag > 0) 
    { 
     CGRect frame = view.frame; 
     frame.origin = CGPointMake(curXLoc, 0); 
     view.frame = frame; 

     curXLoc += (kScrollObjWidth); 

    } 
} 

// set the content size so it can be scrollable 
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)]; 
} 

- (void)viewDidLoad { 

    kScrollObjHeight = 172.0; 
    kScrollObjWidth = 146.0; 
    kNumImages  = 39; 
    scrollView1=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 596, 1022, 172)]; 

    scrollView1.showsVerticalScrollIndicator=YES; 
    scrollView1.userInteractionEnabled=YES; 
    [self.view addSubview:scrollView1]; 

    self.view.backgroundColor = [UIColor whiteColor]; 
    [scrollView1 setBackgroundColor:[UIColor whiteColor]]; 
[scrollView1 setCanCancelContentTouches:NO]; 
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite; 

scrollView1.clipsToBounds = NO; 
scrollView1.scrollEnabled = YES; 
scrollView1.pagingEnabled = YES; 

    NSUInteger j; 
for (j = 1; j <= kNumImages; j++) 
{ 
    NSString *imageName = [NSString stringWithFormat:@"thumb_%lu.png",(unsigned long)j]; 
    image = [UIImage imageNamed:imageName]; 
    imageView = [[UIImageView alloc] initWithImage:image]; 

    CGRect rect = imageView.frame; 
    rect.size.height = kScrollObjHeight; 
    rect.size.width = kScrollObjWidth; 
    imageView.frame = rect; 
    imageView.tag = j; // tag our images for later use when we place them in serial fashion 

    [scrollView1 addSubview:imageView]; 
} 

    pageCtrl = [[UIPageControl alloc] init]; 
    pageCtrl.frame = CGRectMake(-130, CGRectGetMidY(self.view.bounds)+25, 1024, 100); 
    [pageCtrl setNumberOfPages:5]; 
    pageCtrl.currentPage = 0; 
    pageCtrl.pageIndicatorTintColor = [UIColor lightGrayColor]; 
    pageCtrl.currentPageIndicatorTintColor = [UIColor blackColor]; 
    [self.view addSubview:pageCtrl]; 
} 

如何在這裏使用UIPageControl?

回答

2

添加目標像如下頁面控制,

[pageControl addTarget:self action:@selector(onPageControlPageChanged:) forControlEvents:UIControlEventValueChanged]; 

然後,

- (void)onPageControlPageChanged:(UIPageControl *)pageControl_ { 

    int offsetX = pageControl_.currentPage * kScrollObjWidth; 

    CGPoint offset = CGPointMake(offsetX, 0); 

    [scrollView setContentOffset:offset animated:YES]; 
} 

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView_ { 

    int page = scrollView.contentOffset.x/scrollView.frame.size.width; 
    pageCtrl.currentPage = page; 
} 
+0

我怎麼能滾動滾動視圖 – user3069029

+0

是的,可以在滾動視圖的委託方法來實現,當改變網頁控制 – Venkat

+0

你能不能告訴我的一些腳本滾動視圖委託 – user3069029

0

- (void)scrollViewDidScroll:(UIScrollView *)scrollView;你算當前頁和設置像

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{ 
    pageCtrl.currentPage = (int)(self.scrollView.contentOffset.x/kScrollObjWidth); 
    } 
相關問題