UIScrollView 滑動試圖,uiscrollview滑動

來源:互聯網
上載者:User

UIScrollView 滑動試圖,uiscrollview滑動

UIScrollView --->UIView

//建立UIScrollView

testScrollView=[[UIScrollView alloc]init];

 testScrollView.frame=CGRectMake(0, 0, 320, 200);

 testScrollView.backgroundColor=[UIColor cyanColor];

//設定UIScrollView的容量

testScrollView.contentSize =CGSizeMake(320*5, 200 );

 //隱藏水平指示條

 testScrollView.showsHorizontalScrollIndicator =NO;

  //隱藏垂直指示條

  testScrollView.showsVerticalScrollIndicator =   NO;

  //是否允許分頁 YES為允許 預設為NO

  testScrollView.pagingEnabled =YES;

  //設定邊緣動畫效果 NO 為無動畫 預設為Yes

  testScrollView.bounces =YES;

 //設定代理

testScrollView.delegate =self;

//顯示ScrollView

[self.view addSubview:testScrollView];

 

//建立一個UIPageControl,圖片下面的計數點

  UIPageControl *page =[[UIPageControl alloc] initWithFrame:CGRectMake(110, 130, 100, 100)];

   // page.backgroundColor =[UIColor redColor];

    //設定當前page的頁數

    page.numberOfPages =5;

    //設定當前page顯示哪一頁

    page.currentPage =0;

    //設定當前未選中頁的顏色

    page.pageIndicatorTintColor =[UIColor yellowColor];

    //設定選中的頁的顏色

    page.currentPageIndicatorTintColor=[UIColor redColor];

 //對UIPageControl進行方法綁定

 [page addTarget:self action:@selector(movePageControl:) forControlEvents:UIControlEventValueChanged];

 //建立定時器

 [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(move) userInfo:nil repeats:YES];

}

//靜態變數 只會執行一次

static int count=-1;

//實現方法

-(void)move

{

    if (page.currentPage==4||page.currentPage==0) {

        count=-count;

    }

    page.currentPage=page.currentPage+count;

    //設定每一次跳轉的位移量

    testScrollView.contentOffset=CGPointMake(page.currentPage*320, 0);

}

//找到位移量方法

-(void)movePageControl:(UIPageControl *)pageControl

{

    //設定位移量無動畫效果

//testScrollView.contentOffset=CGPointMake(320 * page.currentPage, 0);

    //設定scrollView位移量並且使用動畫效果

    [testScrollView setContentOffset:CGPointMake(pageControl.currentPage*320, 0)];

    NSLog(@"%d",pageControl.currentPage);

}

#pragma mark-

#pragma mark-UIScrollViewDelegete

//圖片正在移動時調用

-(void)scrollViewDidScroll:(UIScrollView *)scrollView

{

    NSLog(@"圖片正在移動時調用");

}

//圖片移動開始時會調用一次

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

{

    NSLog(@"圖片移動開始時會調用一次");

}

//當鬆開scrollView時會調用一次

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView

{

     NSLog(@"當鬆開scrollView時會調用一次");

}

//scrollView滑動結束調用一次

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

     NSLog(@"scrollView滑動結束調用一次");

    NSLog(@"%f",scrollView.contentOffset.x);

 

   page.currentPage= scrollView.contentOffset.x/320;

}

 

相關文章

聯繫我們

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