//
// ViewController.m
// 图片轮播
//
// Created by apple on 16/4/20.
// Copyright © 2016年 cuijw. All rights reserved.
//
import “ViewController.h”
define SEECRE_SIZE self.view.bounds.size
@interface ViewController ()
@property (nonatomic,strong)UIScrollView * scrollView;
@property (nonatomic,strong)UIPageControl * pageControl;
@property (nonatomic,copy)NSArray * arr;
@end
@implementation ViewController
(void)viewDidLoad {
[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.
_arr= @[@”1.jpg”,@”2.jpg”,@”3.jpg”,@”4.jpg”,@”5.jpg”,@”6.jpg”,@”7.jpg”,@”8.jpg”];
// _arr= @[@”1.jpg”,@”2.jpg”,@”3.jpg”];
self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 25, SEECRE_SIZE.width, SEECRE_SIZE.width*1.5)];
self.scrollView.delegate = self;
[self.view addSubview:self.scrollView];
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.contentSize = CGSizeMake(SEECRE_SIZE.width*(_arr.count+2), 0);
for (int i = 0; i<_arr.count+2; i++) {
UIImageView * iamge = [[UIImageView alloc]init];
if (i==0) {iamge.image = [UIImage imageNamed:_arr[_arr.count-1]]; } else if (i == _arr.count+1) { iamge.image = [UIImage imageNamed:_arr[0]]; } else { iamge.image = [UIImage imageNamed:_arr[i-1]]; } iamge.frame = CGRectMake(SEECRE_SIZE.width*i, 0, SEECRE_SIZE.width, SEECRE_SIZE.width*1.5); [self.scrollView addSubview:iamge];
}
self.pageControl = [[UIPageControl alloc]init];
self.pageControl.frame = CGRectMake(100, SEECRE_SIZE.height-80, 200, 44);
[self.view addSubview:self.pageControl];
self.pageControl.numberOfPages = _arr.count;
self.pageControl.currentPage = 0;
self.pageControl.pageIndicatorTintColor = [UIColor magentaColor];
self.pageControl.currentPageIndicatorTintColor = [UIColor blueColor];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSInteger currpage = scrollView.contentOffset.x/SEECRE_SIZE.width;
if (currpage==0) {
currpage = _arr.count-1;
self.scrollView.contentOffset = CGPointMake(SEECRE_SIZE.width*_arr.count, 0);
}
else if (currpage == _arr.count+1)
{
self.scrollView.contentOffset = CGPointMake(SEECRE_SIZE.width, 0);
currpage =0;
}
else
{
currpage = currpage-1;
}
self.pageControl.currentPage = currpage;
}
@end