iOS 用UIScrollView,UIPageControl制作轮播图

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    [self layoutScrollView];

    [self layoutImageView];

    [self layoutPageControl];


    [self addTimer1];

}


#pragma mark - 布局scrollView

- (void)layoutScrollView{

    self.scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];

    self.scrollView.contentSize = CGSizeMake(kScreenWidth * 7, kScreenHeight);

    self.scrollView.pagingEnabled = YES;

    self.scrollView.showsHorizontalScrollIndicator = NO;

    self.scrollView.showsVerticalScrollIndicator = NO;

    self.scrollView.delegate = self;

    [self.view addSubview:self.scrollView];

    [self.scrollView release];

    

}

#pragma mark - 布局图片

- (void)layoutImageView{

    for (int i = 0; i < 7; i++) {

        UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth * (i + 1), 20, kScreenWidth, kScreenHeight / 3)];

        NSString *imageName = [NSString stringWithFormat:@"p%d", i + 1];

        NSString *filePath = [[NSBundle mainBundle] pathForResource:imageName ofType:@"jpg"];

        imageV.image = [UIImage imageWithContentsOfFile:filePath];

        [self.scrollView addSubview:imageV];

        [imageV release];

    }

    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, kScreenWidth, kScreenHeight / 3)];

    NSString *filePath1 = [[NSBundle mainBundle] pathForResource:@"p6" ofType:@"jpg"];

    imageView1.image = [UIImage imageWithContentsOfFile:filePath1];

    [self.scrollView addSubview:imageView1];

    

    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth * 7, 20, kScreenWidth, kScreenHeight / 3)];

    NSString *filePath2 = [[NSBundle mainBundle] pathForResource:@"p1" ofType:@"jpg"];

    imageView2.image = [UIImage imageWithContentsOfFile:filePath2];

    [self.scrollView addSubview:imageView2];

}



#pragma mark - 布局pageControl

- (void)layoutPageControl{

    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake((kScreenWidth - 120) / 2, 580, 120, 20)];

    self.pageControl.pageIndicatorTintColor = [UIColor cyanColor];

    self.pageControl.currentPageIndicatorTintColor = [UIColor redColor];

    self.pageControl.numberOfPages = 6;

    self.pageControl.currentPage = 0;

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

    [self.view addSubview:self.pageControl];

    [self.pageControl release];

    

    

}



- (void)addTimer1{

    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(handleTime) userInfo:nil repeats:YES];

}


- (void)handleTime{


      [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x + 375, 0) animated:YES];

    if (self.scrollView.contentOffset.x == kScreenWidth * 6) {

        self.scrollView.contentOffset = CGPointZero;

        self.pageControl.currentPage = 0;

    }else if (self.scrollView.contentOffset.x == 0){

        self.pageControl.currentPage = 6;

    }else{

        self.pageControl.currentPage = self.scrollView.contentOffset.x / kScreenWidth;

    }

    

}

#pragma mark - UIScrollViewDelegate

//第一张图片向左拖拽会显示最后一张,最后一张向右拖拽会到第一张

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

    if (self.scrollView.contentOffset.x == kScreenWidth * 7) {

        self.pageControl.currentPage = 0;

        self.scrollView.contentOffset = CGPointMake(kScreenWidth, 0);

    }else if (self.scrollView.contentOffset.x == 0){

        self.pageControl.currentPage = 6;

        self.scrollView.contentOffset = CGPointMake(kScreenWidth * 6, 0);

    }else{

        self.pageControl.currentPage = scrollView.contentOffset.x / kScreenWidth - 1;

    }

}





- (void)handlePageControl:(UIPageControl *)pageControl{

    [self.scrollView setContentOffset:CGPointMake(pageControl.currentPage * kScreenWidth, 0) animated:YES];

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值