iOS UIScrollView 实现轮播图
来源:互联网 发布:手机淘宝1元购在哪里 编辑:程序博客网 时间:2024/05/22 12:56
利用UIScrollView实现轮播图 , 需要三个ImageView轮流切换,具体原理就不讲解了.
具体实现代码如下:
<span style="font-size:24px;">//// ViewController.m// PhotosShowDemo//// Created by 帝炎魔 on 16/5/29.// Copyright © 2016年 帝炎魔. All rights reserved.//#import "ViewController.h"#define kWidth [UIScreen mainScreen].bounds.size.width#define kHeitht [UIScreen mainScreen].bounds.size.height#define kRedColor [UIColor redColor]#define kWhitColor [UIColor yellowColor]#define kBlueColor [UIColor blueColor]#define kGreenColor [UIColor greenColor]#define kBrownColor [UIColor brownColor]@interface ViewController () <UIScrollViewDelegate>@property (nonatomic, strong) UIScrollView *scrollView;@property (nonatomic, strong) UIPageControl *pageControl;@property (nonatomic, strong) UIView *leftView;@property (nonatomic, strong) UIView *currentView;@property (nonatomic, strong) UIView *rightView;@property (nonatomic, assign) int viewCount;@property (nonatomic, assign) int currentViewIndex;@property (nonatomic, copy) NSArray *viewColorArray;@property (nonatomic, copy) NSTimer *timer;@property (nonatomic, assign) BOOL isScroll;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.viewColorArray = @[kRedColor, kBlueColor, kBrownColor, kWhitColor, kGreenColor ]; // 添加scrollView [self.view addSubview:self.scrollView]; [self.view addSubview:self.pageControl]; [self loadImageView]; // 创建定时器 实现轮播 [self zidong]; // Do any additional setup after loading the view, typically from a nib.}// 开启定时器- (void)zidong{ _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(nextPage) userInfo:NULL repeats:YES];}// 定时器的方法-(void)nextPage{ _isScroll = YES; int rightCout; [_scrollView setContentOffset:CGPointMake(kWidth * 2, 0)animated:YES]; _currentViewIndex = (_currentViewIndex + 1) % 5; _currentView.backgroundColor = _viewColorArray[_currentViewIndex]; rightCout = (_currentViewIndex + 1) % 5; _rightView.backgroundColor = _viewColorArray[rightCout]; _pageControl.currentPage = _currentViewIndex; [_scrollView setContentOffset:CGPointMake(kWidth, 0) animated:NO]; }// 添加滑动视图- (UIScrollView *)scrollView{ if (!_scrollView) { _scrollView = [[UIScrollView alloc] init]; _scrollView.frame = CGRectMake(0, 0, kWidth, kHeitht); _scrollView.contentSize = CGSizeMake(kWidth * 3, kHeitht); _scrollView.pagingEnabled = YES; _scrollView.delegate = self; [_scrollView setContentOffset:CGPointMake(kWidth, 0)]; _scrollView.showsVerticalScrollIndicator = NO; _scrollView.showsHorizontalScrollIndicator = NO; } return _scrollView;}// 添加按钮-(UIPageControl *)pageControl{ if (!_pageControl) { _pageControl = [[UIPageControl alloc] init]; _pageControl.frame = CGRectMake(0, kHeitht - 100, kWidth, 30); _pageControl.currentPage = _currentViewIndex; _pageControl.numberOfPages = 5; _pageControl.pageIndicatorTintColor = kBlueColor; _pageControl.currentPageIndicatorTintColor = kWhitColor; } return _pageControl;}// 加载图片- (void)loadImageView{ _leftView = [[UIView alloc] init]; _leftView.backgroundColor = _viewColorArray[4]; _leftView.frame = CGRectMake(0, 0, kWidth, kHeitht); [_scrollView addSubview:_leftView]; _currentView = [[UIView alloc] initWithFrame:CGRectMake(kWidth, 0, kWidth, kHeitht)]; _currentView.backgroundColor = _viewColorArray[0]; [_scrollView addSubview:_currentView]; _rightView = [[UIView alloc] initWithFrame:CGRectMake(2 * kWidth, 0, kWidth, kHeitht)]; _rightView.backgroundColor = _viewColorArray[1]; [_scrollView addSubview:_rightView]; _currentViewIndex = 0; }#pragma mark ---- 滚动将要停止的时候- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ // 滚动视图停止减速 // contentOffset 偏移量 // 开启定时器 if (_isScroll == NO) { [self zidong]; _isScroll = YES; } [self loadData]; _pageControl.currentPage = _currentViewIndex; [_scrollView setContentOffset:CGPointMake(kWidth, 0)]; // UIControl 响应的控件 的父类 // UIControl }-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ // 移除定时器 [self.timer invalidate]; if (_isScroll == YES) { scrollView.contentOffset = CGPointMake(kWidth, 0); _currentView.backgroundColor = _viewColorArray[_currentViewIndex]; } _isScroll = NO; // _currentView = _viewColorArray[_currentViewIndex];}- (void)loadData{ if (_scrollView.contentOffset.x > kWidth) { NSLog(@"右滑动"); _currentViewIndex = (_currentViewIndex + 1) % 5; } else if (_scrollView.contentOffset.x < kWidth) { NSLog(@"左滑动"); _currentViewIndex = (_currentViewIndex + 4 ) % 5; } _currentView.backgroundColor = _viewColorArray[_currentViewIndex]; _leftView.backgroundColor = _viewColorArray[(_currentViewIndex + 4) % 5]; _rightView.backgroundColor = _viewColorArray[(_currentViewIndex + 1) % 5]; }- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end</span>
0 0
- iOS UIScrollView 实现轮播图
- IOS 利用UIScrollView实现无限轮播图
- iOS使用UIScrollView实现无限循环轮播图
- iOS 底层实现 - UIScrollView
- iOS UISCrollView循环滚动 实现
- iOS UISCrollView循环滚动 实现
- IOS 轮播图(UIScrollView)
- iphone ios UIScrollView 机制及实现
- IOS--UiScrollView和UIPageControl实现滑动翻页
- 【IOS-DEMO】UIScrollView+UIPageControl 实现欢迎页面
- iOS中UIScrollView实现图片循环播放
- IOS-UIScrollView滚动图片的简单实现
- [iOS]UIScrollview自定义分页的实现方法
- ios基于UIScrollView实现滑动引导页
- iOS开发UIScrollView的底层实现
- iOS 使用UIScrollView实现图片的缩放
- iOS之RAC实现UIScrollView刷新
- iOS UIScrollView
- Linux Cpu占用高调试方法
- 游戏编程中的人工智能技术-遗传算法入门(四)
- 欧拉函数
- 有N个台阶,可以走两步也可以走一步 一共有多少种走法
- FCK编辑器(完整详解)
- iOS UIScrollView 实现轮播图
- 卡尔曼滤波原理
- TCP的三次握手过程图解
- 实用css技巧——清除浮动方法总结
- 将Myeclipse项目改成Eclipse项目
- linux学习笔记(5):dup,dup2,fcntl
- 使用Session防止表单重复提交
- Apache、Mysql、php搭建指南
- FFT