自動滾動,自動顯示下一個映像,滾動,自動顯示映像

來源:互聯網
上載者:User

自動滾動,自動顯示下一個映像,滾動,自動顯示映像

每隔1s會自動切換映像和小白點。

 

工程圖:

代碼:

RootViewController.h

#import <UIKit/UIKit.h>@interface RootViewController : UIViewController<UIScrollViewDelegate,UIPageViewControllerDelegate>{    UIScrollView *imageScrollView;    UIPageControl *pageControl;    NSInteger changePage;    NSTimer *showtimer;    }@end

 

RootViewController.m

- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view.        self.title=@"自動滾動,自動顯示下一個映像";        //自動切換頁面    showtimer=[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(changePageNumber) userInfo:nil repeats:YES];    //加捲軸    [self addScrollView];    //加滑動的圓點    [self addPageControl];        }#pragma -mark -functions//加入捲軸-(void)addScrollView {        imageScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 100, 320, 120)];    [imageScrollView setBackgroundColor:[UIColor redColor]];;    [imageScrollView setContentSize:CGSizeMake(320*4, 120)];    [imageScrollView setBounces:NO];    [imageScrollView setDelegate:self];;    imageScrollView.pagingEnabled = YES;    imageScrollView.showsHorizontalScrollIndicator = NO;    imageScrollView.showsVerticalScrollIndicator=NO;    [self.view addSubview:imageScrollView];        int i = 0;    for ( i = 0; i <4; i++) {        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(320*i, 0, 320, 120)];        imageView.image=[UIImage imageNamed:[NSString stringWithFormat:@"%i.jpg",i]];        imageView.tag = 110 + i;        imageView.userInteractionEnabled=YES;        [imageScrollView addSubview:imageView];    }    }//加入白色的滑動條-(void) addPageControl {    pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(145, 190, 100, 40)];    pageControl.numberOfPages =4;    pageControl.tag = 101;    pageControl.currentPage = 0;    pageControl.backgroundColor=[UIColor clearColor];    [self.view addSubview:pageControl];}#pragma -mark -UIPageViewControllerDelegate-(void)changePageNumber{    pageControl.currentPage=pageControl.currentPage+1;    if (pageControl.currentPage==3) {        changePage = changePage+1;            }    if (changePage==2) {        pageControl.currentPage=0;        changePage=0;    }        [imageScrollView scrollRectToVisible:CGRectMake(pageControl.currentPage * 320.0, 65.0, 320.0, 218.0) animated:YES];    }-(void) tap: (UITapGestureRecognizer*)sender{    NSLog(@"tap %ld image",(long)pageControl.currentPage);}#pragma -mark -UIScrollViewDelegate-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {    int current = scrollView.contentOffset.x/320;    NSLog(@"current:%d",current);    pageControl.currentPage = current;    if (pageControl.currentPage==3) {        changePage = changePage+1;            }    if (changePage==2) {        pageControl.currentPage=0;        [imageScrollView scrollRectToVisible:CGRectMake(pageControl.currentPage * 320.0, 65.0, 320.0, 218.0) animated:YES];        changePage=0;    }    }

 

聯繫我們

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