iOS 轮播图实现
来源:互联网 发布:淘宝旅行官网 编辑:程序博客网 时间:2024/05/19 14:52
#define SCREEN_SIZE [UIScreen mainScreen].bounds.size
#define KImageCount 3
#define KImage_Height 250
@interface ViewController ()<UIScrollViewDelegate>
@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];
}
- iOS轮播图的实现
- iOS 轮播图实现
- iOS UIScrollView 实现轮播图
- iOS UICollectionView 实现轮播图
- IOS轮播图实现
- iOS 使用UICollectionView实现轮播图
- iOS 广告轮播图的实现
- iOS 轮播图的实现scrollView
- iOS 无限轮播图的实现思路
- iOS开发 CycleScrollView 轮播图的实现
- iOS 广告轮播图的实现与封装
- IOS 利用UIScrollView实现无限轮播图
- iOS用UICollectonview实现轮播图效果
- iOS 投机流实现 无限轮播图
- iOS - 关于轮播图的实现实例
- iOS:UICollectionView实现无限轮播图(Swift3)
- iOS利用UIPageViewController实现图片轮播图
- iOS使用UIScrollView实现无限循环轮播图
- 利用word2010写csdn博客-带图片
- vc++读写ini文件
- Android:获取手机通讯录中联系人的信息
- 剪花布条
- 用指向指针的指针的方法对字符串和整数排序
- iOS 轮播图实现
- 关于sql语句中最后where 1=1
- js验证密码必须为大小写字母和数字组成
- TCP Three-Way handshake
- Android了解四大基本组件
- Git分布式版本控制系统
- UVa - 11400 - Lighting System Design
- 基于Qt5 描绘物体在二维空间运动轨迹(一)
- 【Android】状态栏通知Notification、NotificationManager详解