objective-c实现轮播图(17-08-09)
2017-08-09 本文已影响0人
Hilarylii
屏幕快照 2017-08-09 下午8.39.01.png
屏幕快照 2017-08-09 下午8.43.35.png
注:轮播图是7张,但要给8张图片,并且第一张和最后一张是同一张图片
//
// ViewController.m
// 轮播图
//
// Created by lanou3g on 17/8/9.
// Copyright © 2017年 lanou3g. All rights reserved.
//
#import "ViewController.h"
@interface ViewController () <UIScrollViewDelegate>
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIPageControl *pageControl;
@property (nonatomic, assign) NSTimer *timer;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.scrollView.delegate = self;
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width*7, self.view.frame.size.height);
[self.view addSubview:self.scrollView];
for (int i = 1; i <= 8; i++) {
NSString *imageName1 = [NSString stringWithFormat:@"h%d.jpeg",i];
CGFloat x = (i-1)*self.view.frame.size.width;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageView.image = [UIImage imageNamed:imageName1];
[self.scrollView addSubview:imageView];
}
self.scrollView.pagingEnabled = YES;
self.scrollView.bounces = NO;
self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(100, 25, self.view.frame.size.width-200, 30)];
self.pageControl.numberOfPages = 7;
self.pageControl.currentPage = 0;
[self.pageControl addTarget:self action:@selector(pageControlAction) forControlEvents:UIControlEventEditingChanged];
[self.view addSubview:self.pageControl];
self.timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
[self.timer fire];
}
- (void)timerAction{
CGFloat offsetX = self.scrollView.contentOffset.x;
CGFloat offsetY = self.scrollView.contentOffset.y;
CGFloat width = self.view.frame.size.width;
[self.scrollView setContentOffset:CGPointMake(offsetX+width, offsetY) animated:YES];
if (self.scrollView.contentOffset.x >= width * 7) {
CGPoint point = CGPointMake(0, 0);
self.scrollView.contentOffset = point;
}
}
- (void)pageControlAction{
CGFloat index = self.pageControl.currentPage;
CGPoint point = CGPointMake(index*self.view.frame.size.width, 0);
[self.scrollView setContentOffset:point animated:YES];
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
[self.timer invalidate];
self.timer = nil;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
[self.timer fire];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat x = scrollView.contentOffset.x;
CGFloat width = self.view.frame.size.width;
if (x>=width*7) {
self.pageControl.currentPage = 0;
}else {
self.pageControl.currentPage = x/width;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end