iOS 輪播圖實現

來源:互聯網
上載者:User

iOS 輪播圖實現

 

#define SCREEN_SIZE [UIScreen mainScreen].bounds.size
#define KImageCount 3
#define KImage_Height 250

@interface ViewController ()
@property (nonatomic, strong) UIScrollView * scrollView;
@property (nonatomic, strong) UIPageControl * pageControl;
@property (nonatomic, strong) NSTimer * timer;
@end

@implementation ViewController
- (UIScrollView *)scrollView{
if (_scrollView == nil) {
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE.width, KImage_Height)];
_scrollView.pagingEnabled = YES;
_scrollView.showsHorizontalScrollIndicator = NO;
}
return _scrollView;
}
- (UIPageControl *)pageControl{
if (_pageControl == nil) {
_pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.scrollView.frame.size.height - 30, SCREEN_SIZE.width, 20)];
_pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:65 / 255.0 green:168 / 255.0 blue:100/255.0 alpha:1.0];
_pageControl.pageIndicatorTintColor = [UIColor grayColor];

}
return _pageControl;
}
- (void)setView{
[self.view addSubview:self.scrollView];
[self.view addSubview:self.pageControl];
CGFloat imageY = 0;
CGFloat imageW = SCREEN_SIZE.width;
CGFloat imageH = KImage_Height;
for (int i = 0; i < KImageCount; i++) {
UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * imageW, imageY, imageW, imageH)];
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@%i.jpg, i+1]];
[self.scrollView addSubview:imageView];
}
self.scrollView.contentSize = CGSizeMake(KImageCount * imageW, 0);
self.scrollView.delegate = self;
self.pageControl.numberOfPages = KImageCount;
[self addTimer];
}
- (void)addTimer {
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop]addTimer:self.timer forMode:NSDefaultRunLoopMode];
}
- (void)nextImage{
int i = (int)self.pageControl.currentPage;
if (i == KImageCount - 1) {
i = -1;
}
i++;
[self.scrollView setContentOffset:CGPointMake(i * self.scrollView.frame.size.width, 0) animated:YES];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
[self turnOffTimer];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
[self addTimer];
}
- (void)turnOffTimer{
[self.timer invalidate];
self.timer = nil;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
self.pageControl.currentPage = (self.scrollView.frame.size.width * 0.5 + self.scrollView.contentOffset.x) / self.scrollView.frame.size.width;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @輪播圖;
[self setView];
}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.