[IOS UIScrollView+PageControl]資訊展示橫幅

來源:互聯網
上載者:User

標籤:style   blog   color   io   os   ar   for   sp   div   

ScrollViewController.h

#import <UIKit/UIKit.h>@interface ScrollViewController : UIViewController<UIScrollViewDelegate,UIPageViewControllerDelegate>{    UIScrollView *_scrollView;    UIPageControl*_pageControl;}@end

 

ScrollViewController.m

#import "ScrollViewController.h"@interface ScrollViewController ()@end@implementation ScrollViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {          }    return self;}- (void)viewDidLoad{    [super viewDidLoad];        //設定ScrollView的整體觸摸與顯示地區    //注意 寬 高不要超過 320X480    //否則會出現無法滾動的情況    _scrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320,120)] autorelease];        //設定ScrollView滾動內容的地區    //它通常是需要大於ScrollerView的顯示地區的    //這樣才有必要在ScrollerView中滾動它    [_scrollView setContentSize:CGSizeMake(320 * 5, 120)];        //開啟滾動分頁功能,如果不需要這個功能關閉即可    [_scrollView setPagingEnabled:YES];        //隱藏橫向與縱向的捲軸    [_scrollView setShowsVerticalScrollIndicator:NO];    [_scrollView setShowsHorizontalScrollIndicator:NO];        //在本類中代理scrollView的整體事件    [_scrollView setDelegate:self];            //如果你開啟橫向或縱向的捲軸,這裡可以設定捲軸的風格    // UIScrollViewIndicatorStyleDefault, 預設風格    // UIScrollViewIndicatorStyleBlack,   黑色風格    // UIScrollViewIndicatorStyleWhite    白色風格    //[_scrollView setIndicatorStyle:UIScrollViewIndicatorStyleBlack]         for (int i =0; i<5; i++)    {                //在這裡給每一個ScrollView添加一個圖片 和一個按鈕        UIImageView *imageView= [[[UIImageView alloc] initWithFrame:CGRectMake(i * 320,0,320,120)] autorelease];        [imageView setImage:[UIImage imageNamed:@"image.png"]];                UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];        button.frame = CGRectMake(i * 320, 10, 100, 30);                [button setTitle:@"這是一個按鈕" forState:UIControlStateNormal];                [button addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];                //把每頁需要顯示的VIEW添加進ScrollerView中        [_scrollView addSubview:imageView];        [_scrollView addSubview:button];    }    //整體再將ScrollerView顯示在視窗中    [self.view addSubview:_scrollView];        //頁面控制小工具    //它會在底部繪製小圓點標誌當前顯示頁面    _pageControl = [[[UIPageControl alloc] initWithFrame:CGRectMake(0,100,self.view.frame.size.width, 20)]autorelease];    //設定頁面的數量    [_pageControl setNumberOfPages:5];    //監聽頁面是否發生改變    [_pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];    [self.view addSubview:_pageControl];    }- (void)changePage:(id)sender{    //得到當前頁面的ID   //int page = [sender currentPage];        //在這裡寫你需要執行的代碼    //......}//手指離開螢幕後ScrollView還會繼續滾動一段時間只到停止- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{      NSLog(@"結束滾動後緩衝滾動徹底結束時調用");}-(void) scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{  NSLog(@"結束滾動後開始緩衝滾動時調用");}-(void)scrollViewDidScroll:(UIScrollView*)scrollView{     //頁面滾動時調用,設定當前頁面的ID     [_pageControl setCurrentPage:fabs(scrollView.contentOffset.x/self.view.frame.size.width)];    NSLog(@"視圖滾動中X軸座標%f",scrollView.contentOffset.x);    NSLog(@"視圖滾動中X軸座標%f",scrollView.contentOffset.y);}-(void)scrollViewWillBeginDragging:(UIScrollView*)scrollView{    NSLog(@"滾動視圖開始滾動,它只調用一次");}-(void)scrollViewDidEndDragging:(UIScrollView*)scrollView willDecelerate:(BOOL)decelerate{   NSLog(@"滾動視圖結束滾動,它只調用一次");}-(void)buttonClick{    NSLog(@"按鈕點擊了");}- (void)viewDidUnload{    [super viewDidUnload];}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{    return (interfaceOrientation == UIInterfaceOrientationPortrait);}@end

 

[IOS UIScrollView+PageControl]資訊展示橫幅

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.