自动无限轮播图

前期工作,图片导入进来,定义宏

一/添加scrollView,设置为屏幕大小,并定义scrollView 的content size为图片数+2的宽度

二/将图片添加以循环的方式添加到scrollView上,并加以判断.第一张的位置添加图片的最后一个图(为了形成视觉欺骗);并在最后一张的位置添加第一张图(加以偏移量的处理可以达到无限滑动)

三/添加pageControl(小圆点)

四/添加timer,设置timer的属性

五/在timer的运行事件中,对轮播图做处理,图片便宜到最后一张的位置时,瞬间跳转到第二张图也就是真正第一张的位置;并且将pagecontroller的小圆点偏移到第一个的位置.



viewController.m

#import "secondViewController.h"

@interface secondViewController ()<UIScrollViewDelegate>


@end


@implementation secondViewController

#define WIDTH self.view.frame.size.width

#define HEIGHT self.view.frame.size.height


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    self.navigationItem.title = @"第二页";

//    self.navigationController.navigationBar.translucent = NO;

    self.automaticallyAdjustsScrollViewInsets = NO;

    

    self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"轮播图" image:[UIImage imageNamed:@"Unknown-1"] tag:222];

    

    

    [self setScrollView];

    [self setPageControl];

    

    [self setTimer];

    

}

- (void)setTimer {

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(myTime:) userInfo:nil repeats:YES];

}

- (void)myTime:(NSTimer *)time{

    UIScrollView *s = [self.view viewWithTag:111];

    NSInteger i = s.contentOffset.x;

    UIPageControl *pc = [self.view viewWithTag:300];

    [s setContentOffset:CGPointMake(i + WIDTH, 0) animated:YES];

    pc.currentPage = s.contentOffset.x/WIDTH;

    

    if (s.contentOffset.x == WIDTH * 8) {

        s.contentOffset = CGPointMake(0, 0);

        pc.currentPage = 0;

    }

}


- (void)setScrollView{

    UIScrollView *scrollV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];

    scrollV.contentSize = CGSizeMake(10 * WIDTH, 0);

    scrollV.tag = 111;

    scrollV.pagingEnabled = YES;

    scrollV.delegate = self;

    scrollV.showsVerticalScrollIndicator = NO;

    scrollV.showsHorizontalScrollIndicator = NO;

    scrollV.contentOffset = CGPointMake(WIDTH, 0);

    

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

        UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 80, WIDTH, 250)];

        imageV.tag = 200 + i;

        if (i == 0) {

            imageV.image = [UIImage imageNamed:[NSString stringWithFormat:@"8.jpg"]];

        }else if (i == 9){

            imageV.image = [UIImage imageNamed:[NSString stringWithFormat:@"1.jpg"]];

        }else{

            imageV.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg", i]];

        }

        [scrollV addSubview:imageV];

        [imageV release];

    }

    [self.view addSubview:scrollV];

    [scrollV release];

}

- (void)setPageControl{

    UIPageControl *pc = [[UIPageControl alloc] initWithFrame:CGRectMake(150, 300, 75, 30)];

    pc.numberOfPages = 8;

    [pc addTarget:self action:@selector(handlePC:) forControlEvents:UIControlEventValueChanged];

    pc.tag = 300;

    [self.view addSubview:pc];

    [pc release];

}

#pragma mark - page点击事件

- (void)handlePC:(UIPageControl *)pc{

    UIScrollView *scrollV = [self.view viewWithTag:111];

    [scrollV setContentOffset:CGPointMake(pc.currentPage * WIDTH, 0) animated:YES];

}

#pragma mark - scroll代理时机

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

    UIPageControl *pc = [self.view viewWithTag:300];

    if (scrollView.contentOffset.x == 9 * WIDTH) {

        pc.currentPage = 0;

        scrollView.contentOffset = CGPointMake(WIDTH, 0);

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

        pc.currentPage = 7;

        scrollView.contentOffset = CGPointMake(8 * WIDTH, 0);

    }else{

        pc.currentPage = scrollView.contentOffset.x / WIDTH - 1;

    }

}





- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end



你可能感兴趣的:(自动无限轮播图)