iPhone開發筆記 (5) scrollView和pageControl的搭配使用

來源:互聯網
上載者:User
    如所示,下面介紹一下scrollView和pageControl如何進行搭配使用。   1、在viewDidLoad中添加如下代碼
//定義scrollView    scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 180)];    [scrollView setBackgroundColor:[UIColor blackColor]];[scrollView setCanCancelContentTouches:NO];scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;scrollView.clipsToBounds = YES;// default is NO, we want to restrict drawing within our scrollviewscrollView.scrollEnabled = YES;scrollView.pagingEnabled = YES;    scrollView.delegate = self;    scrollView.tag = 1;    scrollView.showsHorizontalScrollIndicator = NO;        //為scrollView添加手勢    //代碼來源:http://stackoverflow.com/questions/5042194/how-to-detect-touch-on-uiimageview-inside-uiscrollview    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];    singleTap.cancelsTouchesInView = NO;     [scrollView addGestureRecognizer:singleTap];       //向scrollView中添加imageViewNSUInteger i;for (i = 1; i <= kNumImages1; i++){NSString *imageName = [NSString stringWithFormat:@"image%d.jpg", i];UIImage *image = [UIImage imageNamed:imageName];UIImageView *imageView = [[UIImageView alloc] initWithImage:image];        //設定frameCGRect rect = imageView.frame;rect.size.height = kScrollObjHeight1;rect.size.width = kScrollObjWidth1;imageView.frame = rect;imageView.tag = i;        [scrollView addSubview:imageView];[imageView release];}    [self layoutScrollImages1]; //設定圖片格式    [featureView addSubview:scrollView]; //將scrollView封裝到featureView        //定義pageControl    pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 180, 320, 20)];    [pageControl setBackgroundColor:[UIColor blackColor]];    pageControl.currentPage = 0;    pageControl.numberOfPages = kNumImages1;    [featureView addSubview:pageControl]; //將pageControl封裝到featureView        //定義顯示店鋪資訊的label    featureLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, 320, 20)];    featureLabel.textAlignment = UITextAlignmentCenter;    featureLabel.backgroundColor = [UIColor blackColor];    featureLabel.textColor = [UIColor whiteColor];    featureLabel.text = [array objectAtIndex:0];    [featureView addSubview:featureLabel]; //將label封裝到featureView

2、還有下面的關於設定scrollView和pageControl的委託代碼,以及用來設定scrollView分頁的代碼

//設定圖片的格式//代碼來源:Apple官方例子Scrolling- (void)layoutScrollImages1{UIImageView *view = nil;NSArray *subviews = [scrollView subviews];    // reposition all image subviews in a horizontal serial fashionCGFloat 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 += (kScrollObjWidth1);}}// set the content size so it can be scrollable[scrollView setContentSize:CGSizeMake((kNumImages1 * kScrollObjWidth1), [scrollView bounds].size.height)];}//UIScrollViewDelegate方法- (void)scrollViewDidEndDecelerating:(UIScrollView *)sView{    if (sView.tag == 1)     {        NSInteger index = fabs(sView.contentOffset.x) / sView.frame.size.width;        //NSLog(@"%d",index);        [pageControl setCurrentPage:index];        featureLabel.text = [array objectAtIndex:index];    }}//UIScrollView響應gesture的action- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture{     CGPoint touchPoint=[gesture locationInView:scrollView];    NSInteger index = touchPoint.x/320;    shopDetailView = [[ShopDetailViewController alloc] init];    [self.navigationController pushViewController:shopDetailView animated:YES];    [shopDetailView release];}
這樣就可以實現以下兩個功能了:一是scrollView的滑動,二是scrollView下面的Label的文字可以隨著scrollView中圖片的變化而變化。一般應用的首頁都會有一個推薦的功能,用UIScrollView和UIPageControl最好不過了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.