UIScrollView的自动滑动,使用NSTimer定时器实现
来源:互联网 发布:中小企业员工流失数据 编辑:程序博客网 时间:2024/06/06 07:53
@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic,strong)UIPageControl *pageControl;
@property(nonatomic,strong)NSTimer *timer;
@property(nonatomic,assign)int timeIndex;
- (void)viewDidLoad {
[superviewDidLoad];
[
_timeIndex=0;
_timer = [NSTimerscheduledTimerWithTimeInterval:3target:selfselector:@selector(scrollChange)userInfo:nilrepeats:YES];
[[NSRunLoopcurrentRunLoop]addTimer:_timerforMode:NSDefaultRunLoopMode];
_scrollView = [UIScrollViewnew];
_scrollView.showsVerticalScrollIndicator =NO;
_scrollView.delegate =self;
_scrollView.pagingEnabled =YES;
_scrollView.frame=CGRectMake(0,64, self.view.bounds.size.width,136);
self.automaticallyAdjustsScrollViewInsets=NO;
_scrollView.showsHorizontalScrollIndicator =NO;
CGFloat imageW =self.scrollView.frame.size.width;
CGFloat imageH =self.scrollView.frame.size.height;
CGFloat imageInitialY =0;
CGFloat imageInitialX =0;
NSArray *arr=[NSArrayarrayWithObjects:@"1",@"2",@"3",@"4",nil];
/*添加按钮
for (int i=0; i<4; i++) {
UIButton *button=[UIButtonbuttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImageimageNamed:[arr objectAtIndex:i]]forState:UIControlStateNormal];
button.tag=i;
CGFloat imageX = i*imageW + imageInitialX;
button.frame=CGRectMake(imageX, imageInitialY, imageW, imageH);
[button addTarget:selfaction:@selector(buttonClick:)forControlEvents:UIControlEventTouchUpInside];
[_scrollViewaddSubview:button];
}
*/
CGFloat contentw=4*imageW;
self.scrollView.contentSize=CGSizeMake(contentw,0);
self.scrollView.pagingEnabled = YES;
[self.viewaddSubview:self.scrollView];
_pageControl = [UIPageControlnew];
_pageControl.frame=CGRectMake(0,200,WEIDTH ,5);
_pageControl.accessibilityNavigationStyle =
UIAccessibilityNavigationStyleAutomatic;
_pageControl.numberOfPages=4;
_pageControl.pageIndicatorTintColor = [UIColorredColor];//设置非选中页的圆点颜色
_pageControl.currentPageIndicatorTintColor = [UIColorblueColor];// 设置选中页的圆点颜色
[self.viewaddSubview:self.pageControl];
}
-(void)scrollChange{
NSLog(@"123");
_timeIndex++;
if (_timeIndex ==4) {
_timeIndex =0;
_scrollView.contentOffset =CGPointMake(WEIDTH *_timeIndex, 0);
}
else {
_scrollView.contentOffset =CGPointMake(_timeIndex *WEIDTH,0);
}
}
//手动滑动时候,定时器关闭
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
NSLog(@"1");
[_timerinvalidate];
_timer =nil;
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
_timeIndex = scrollView.contentOffset.x/WEIDTH ;
NSLog(@"_timeIndex = %d",_timeIndex);
_timer = [NSTimerscheduledTimerWithTimeInterval:3target:selfselector:@selector(scrollChange)userInfo:nilrepeats:YES];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
int page = scrollView.contentOffset.x / scrollView.frame.size.width;
_pageControl.currentPage = page;
}
- UIScrollView的自动滑动,使用NSTimer定时器实现
- UIScrollView+UIPageControl+NSTimer实现图片的自动滑动以及用户可手动切换,UIPageControl可点击
- UIScrollView+UIPageControl+NSTimer实现图片的自动滑动以及用户可手动切换,UIPageControl可点击
- UIScrollView+UIPageControl+NSTimer实现图片的自动滑动以及用户可手动切换,UIPageControl可点击
- UIScrollView+UIPageControl+NSTimer实现图片的自动滑动以及用户可手动切换,UIPageControl可点击
- UIScrollView+UIPageControl+NSTimer实现图片的自动滑动以及用户可手动切换,UIPageControl可点击
- NSTimer 定时器的使用
- NSTimer(定时器)的使用
- NSTimer定时器的使用
- NSTimer定时器的使用
- 图片自动播放(用UIScrollView和NSTimer)时定时器的开启和关闭
- ios-day05-04(图片轮播器 UIScrollView的分页、UIPageControl的使用、NSTimer(定时器)的使用)
- 滑动时NSTimer定时器停止的解决方法
- IOS 定时器NSTimer的使用
- iOS NSTimer 定时器的使用
- iOS-定时器的使用NSTimer
- 定时器NSTimer的简单使用
- UIScrollView使用自动布局不能滑动的解决方法
- Struts2工作原理以及核心思想
- python(哈希性hashable和不可改变性imutable)
- 如何从CentOS官网下载我们想要的版本
- 静态html实现倒计时
- iOS开发 ☞ 初识RAC
- UIScrollView的自动滑动,使用NSTimer定时器实现
- 数据专家必知必会的 7 款 Python 工具
- Macos 使用dd命令刻盘做u盘启动ubuntu系统
- 【今日荐文】你欠缺的是赶紧静下心来好好学习---猿哥
- 数组的常见操作
- read/fread write/fwrite 的实现原理
- 美俄总统今将在G20首会,为何握手比会谈结果更吸睛
- 使用TabHost点击第一次无法获得数据的问题,自定义TabHost。
- CSRF原理