<UIScrollViewDelegate>
#define WIDTH [[UIScreen mainScreen] bounds].size.width
#define HEIGHT [[UIScreen mainScreen] bounds].size.height
@property (nonatomic, strong)NSTimer *timer;
@property (nonatomic, retain)NSMutableArray *arr;
@property (nonatomic, retain)UIView *headerView;
@property (nonatomic, retain)UIImageView *image;
@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain)UIPageControl *pageC;
- (void)headImage{
self.arr = [NSMutableArray arrayWithObjects:@"8.jpg",@"1.jpg", @"2.jpg", @"3.jpg", @"4.jpg", @"5.jpg", @"6.jpg", @"7.jpg", @"8.jpg", @"1.jpg", nil];
self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 200 * HEIGHT/667)];
self.scrollView.backgroundColor = [UIColor clearColor];
self.scrollView.contentSize = CGSizeMake(WIDTH * self.arr.count, 0);
self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.bounces = NO;
[self.headerView addSubview:self.scrollView];
[_scrollView release];
for (int i = 0 ; i < self.arr.count; i++) {
NSString *name = [self.arr objectAtIndex:i];
UIImage *img = [UIImage imageNamed:name];
self.image = [[UIImageView alloc]init];
self.image.frame = CGRectMake(i * WIDTH, 0, WIDTH, 200 * HEIGHT/667);
self.image.image = img;
[self.scrollView addSubview:self.image];
[_image release];
}
self.scrollView.delegate = self;
self.pageC = [[UIPageControl alloc]initWithFrame:CGRectMake(100 * WIDTH/375, 120 * HEIGHT/667, 200* WIDTH/375, 60*HEIGHT/667)];
self.pageC.backgroundColor = [UIColor clearColor];
[self.headerView addSubview:self.pageC];
self.pageC.numberOfPages = self.arr.count;
self.pageC.currentPageIndicatorTintColor = [UIColor brownColor];
self.pageC.pageIndicatorTintColor = [UIColor grayColor];
self.pageC.currentPage = 0;
[self.pageC addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
[_pageC release];
[self addTimer];
}
//定时器执行方法
- (void)change:(NSTimer *)time{
if (self.pageC.currentPage == self.pageC.numberOfPages - 1) {
self.pageC.currentPage = 0
} else if (self.pageC.currentPage < self.pageC.numberOfPages - 1) {
self.pageC.currentPage++
}
[self.scrollView setContentOffset:CGPointMake((self.pageC.currentPage + 1) * WIDTH, 0) animated:NO] }
原文来自:http://blog.youkuaiyun.com/shanhu_t/article/details/50359469
转载请注明