/
// Created by 银羽2 on 16/4/28.
// Copyright © 2016年 银羽2. All rights reserved.
//
#import "ViewController.h"
#define kImageCount 5
@interface ViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *page;
@property (nonatomic, strong) NSTimer *timer;
@end
@implementation ViewController
-(UIPageControl *)page{
if (_page == nil) {
_page = [[UIPageControl alloc] init];
_page.numberOfPages = kImageCount;
CGSize size = [_page sizeForNumberOfPages:kImageCount];
_page.bounds = CGRectMake(0, 0, size.width, size.height);
_page.center = CGPointMake(self.view.center.x, 130);
_page.pageIndicatorTintColor = [UIColor whiteColor];
_page.currentPageIndicatorTintColor = [UIColor redColor];
//在OC中 绝大多数"控件",都可以监听 UIControlEventValueChanged
[_page addTarget:self action:@selector(pageChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_page];
}
return _page;
}
-(UIScrollView *)scrollView
{
if (_scrollView == nil) {
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 20, 300, 130)];
_scrollView.backgroundColor = [UIColor redColor];
[self.view addSubview:_scrollView];
_scrollView.pagingEnabled = YES;
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.bounces = NO;
_scrollView.delegate = self;
self.scrollView.contentSize = CGSizeMake(kImageCount *_scrollView.bounds.size.width, 130) ;
}
return _scrollView;
}
-(void)viewDidLoad{
[super viewDidLoad];
for (int i = 0; i < kImageCount; i++) {
NSString *imageName = [NSString stringWithFormat:@"img_0%d",i+ 1];
UIImageView *imageV = [[UIImageView alloc] initWithFrame:self.scrollView.bounds];
imageV.image = [UIImage imageNamed:imageName];
[self.scrollView addSubview: imageV];
//计算imgeView的位置
}
[self.scrollView.subviews enumerateObjectsUsingBlock:^(UIImageView *imageV, NSUInteger idx, BOOL * _Nonnull stop) {
//调整x origin ==> frame
CGRect frame =imageV.frame;
frame.origin.x = idx * frame.size.width;
imageV.frame = frame;
}];
self.page.currentPage = 0;
[self startTimer];
}
-(void)startTimer{
self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(updateTimer) userInfo:@"" repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
-(void)updateTimer{
NSLog(@"%s",__func__);
//也好发生变化
int page =(self.page.currentPage +=1)%kImageCount;
self.page.currentPage = page;
[self pageChanged:self.page];
}
-(void)pageChanged:(UIPageControl *)page{
NSLog(@"%d",page.currentPage);
CGFloat x = page.currentPage * self.scrollView.bounds.size.width;
[self.scrollView setContentOffset:CGPointMake(x, 0)];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
NSLog(@"%s",__func__);
self.page.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width;
}
/**
* 使用运行循环后 抓不住图片
解决方法:抓住图片是停止时钟 松手后 开启时钟
*/
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
[self startTimer];
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//停止之后不能再使用, 需要重新使用 重新启用
[self.timer invalidate];
}
@end
本文介绍了一种在iOS应用中实现图片自动轮播的方法,通过UIView和UIScrollView结合UIPageControl来控制页面指示器,利用NSTimer进行定时切换,实现了流畅的轮播效果。

1269

被折叠的 条评论
为什么被折叠?



