自动无限轮播图

来源:互联网 发布:淘宝买东西好评怎么说 编辑:程序博客网 时间:2024/06/06 01:15

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

一/添加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 {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

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

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

    self.automaticallyAdjustsScrollViewInsets =NO;

    

    self.tabBarItem = [[UITabBarItemalloc] initWithTitle:@"轮播图"image:[UIImageimageNamed:@"Unknown-1"]tag:222];

    

    

    [selfsetScrollView];

    [selfsetPageControl];

    

    [self setTimer];

    

}

- (void)setTimer {

    NSTimer *timer = [NSTimerscheduledTimerWithTimeInterval:1.0 target:selfselector:@selector(myTime:)userInfo:nilrepeats:YES];

}

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

    UIScrollView *s = [self.viewviewWithTag:111];

    NSInteger i = s.contentOffset.x;

    UIPageControl *pc = [self.viewviewWithTag: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 = [[UIScrollViewalloc] 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 = [[UIImageViewalloc] initWithFrame:CGRectMake(WIDTH * i, 80,WIDTH, 250)];

        imageV.tag = 200 + i;

        if (i == 0) {

            imageV.image = [UIImageimageNamed:[NSStringstringWithFormat:@"8.jpg"]];

        }else if (i == 9){

            imageV.image = [UIImageimageNamed:[NSStringstringWithFormat:@"1.jpg"]];

        }else{

            imageV.image = [UIImageimageNamed:[NSStringstringWithFormat:@"%d.jpg", i]];

        }

        [scrollV addSubview:imageV];

        [imageV release];

    }

    [self.viewaddSubview:scrollV];

    [scrollV release];

}

- (void)setPageControl{

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

    pc.numberOfPages = 8;

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

    pc.tag = 300;

    [self.viewaddSubview:pc];

    [pc release];

}

#pragma mark - page点击事件

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

    UIScrollView *scrollV = [self.viewviewWithTag:111];

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

}

#pragma mark - scroll代理时机

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

    UIPageControl *pc = [self.viewviewWithTag: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 {

    [superdidReceiveMemoryWarning];

    // 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



1 0
原创粉丝点击