iOS 轮播图

- (void)viewDidLoad {

    [super viewDidLoad];

    

    self.view.backgroundColor = [UIColor blackColor];


   

    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, Screen_Height / 2 - 100, Screen_Width, 200)];

    //    self.scrollView.backgroundColor = [UIColor redColor];

    self.scrollView.delegate = self;

    [self.view addSubview:_scrollView];

    

    //    内容大小

    self.scrollView.contentSize = CGSizeMake(Screen_Width * 8, 64);

    //    启用分页

    self.scrollView.pagingEnabled = YES;

    //    显示水平滚动指标

    self.scrollView.showsHorizontalScrollIndicator = NO;

    //    设置内容抵消

    [self.scrollView setContentOffset:CGPointMake(Screen_Width, 0) animated:YES];

    self.automaticallyAdjustsScrollViewInsets = NO;

    

    self.pageVC = [[UIPageControl alloc] initWithFrame:CGRectMake(Screen_Width / 7, self.scrollView.frame.origin.y + self.scrollView.frame.size.height - 30, Screen_Width / 1.5, 20)];

    //    self.pageVC.currentPage = 0;

    self.pageVC.pageIndicatorTintColor = [UIColor lightGrayColor];

    self.pageVC.currentPageIndicatorTintColor = [UIColor blueColor];

    self.pageVC.numberOfPages = 6;

    

    

    self.array = [NSMutableArray arrayWithObjects:@"img00@2x.png",@"img01@2x.png", @"img02@2x.png",@"img03@2x.png",@"img04@2x.png",@"img05@2x.png",nil];

    

    //        设置   第一张 图片

    id firstImage = [self.array firstObject];

    //        设置  最后 一张  图片

    id lastImage = [self.array lastObject];

    //       把第一张图片  加在 数组中

    

    [self.array insertObject:lastImage atIndex:0];

    [self.array addObject:firstImage];

    

  

    

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

        

//        self.scro = [[UIScrollView alloc] initWithFrame:CGRectMake(Screen_Width * i, 0, Screen_Width, Screen_Height)];

//        

//        

//        

//        self.scro.delegate = self;

//        

        self.scro.center = CGPointMake(self.scrollView.frame.size.width / 2, self.scrollView.frame.size.height / 2);

//        

//        //设置最大伸缩比例

//        self.scro.maximumZoomScale=2.0;

//        //设置最小伸缩比例

//        self.scro.minimumZoomScale=0.5;

//        

//        

//        [self.view addSubview:self.scro];

       

        

        self.homeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(Screen_Width * i, 0, Screen_Width, Screen_Height / 3)];


        self.homeImageView.image = [UIImage imageNamed:@"himages_01.png"];

        

        [self.scrollView addSubview:self.homeImageView];

      

        

        

    }

    [self.view addSubview:self.pageVC];

    [self addtimer];


}

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

{

    return self.homeImageView;

}

-(NSUInteger)supportedInterfaceOrientations{

    return UIInterfaceOrientationMaskLandscapeRight;

}

- (void)addtimer

{

    

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

    

    

    

}

- (void)timerAction

{

    

    CGFloat x = (self.myIndex + 1) * self.scrollView.frame.size.width;

   

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


    

    if (self.myIndex == 7) {

        

        CGFloat x = 1 * self.scrollView.frame.size.width;

        

        [self.scrollView setContentOffset:CGPointMake(x, 0) animated:NO];

        

        self.myIndex =  -1;

        

    }


    self.myIndex ++;


}




//滑动结束

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    

    

    if (self.scrollView.contentOffset.x >= self.scrollView.frame.size.width * (8 - 1)) {

        //    显示用户看到的第一页  也就是数组中的第二张

        [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width - 1, 0) animated:NO];

 

    }

    if (self.scrollView.contentOffset.x <= 0) {

        //        如果当前偏移量小于等于0的时候

        [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width * (8 - 2), 0) animated:NO];

    }

  

}


//将要开始拖缀


-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

{

    

    [self removeTimer];

    

}



// 结束拖缀

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate

{

    if (self.timer == nil) {

        [self addtimer];

        

    }

}

// 滑动时

- (void)scrollViewDidScroll:(UIScrollView *)scrollView

{


    //    当页面页面滑动一半的时候  就改变页数

    int X = scrollView.frame.size.width/2;

    NSInteger index = (scrollView.contentOffset.x - self.scrollView.frame.size.width - X) / (scrollView.frame.size.width) + 1;

    self.pageVC.currentPage = index;

    

    self.myIndex = self.pageVC.currentPage;

    if (index == 6) {

        [self.pageVC setCurrentPage:0];

        self.myIndex = 7;

    }


}

-(void)removeTimer

{

    [self.timer invalidate];

    self.timer = nil;

}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值