美文网首页
自动小轮播图

自动小轮播图

作者: 繁华落尽终是殇 | 来源:发表于2017-11-23 11:15 被阅读0次

//封装UIScrollView 方法

- (void)initscroll{

//初始化

self.scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 20, CGRectGetWidth(self.view.bounds), 220)];

//self.scrollview.backgroundColor = [UIColor redColor];

//添加到视图

[self.view addSubview:self.scrollview];

//设置滚动视图的长度

[self.scrollview setContentSize:CGSizeMake(CGRectGetWidth(self.view.bounds)*4, 0)];

for (NSInteger i = 0; i < 4; i++) {

UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.bounds)*i, 0, CGRectGetWidth(self.view.bounds), 220)];

// 设置图片

[imageview setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%ld.jpg",(long)i]]];

[self.scrollview addSubview:imageview];

}

//设置图片的分页

[self.scrollview setPagingEnabled:YES];

//将图片水平面的横线隐藏

[self.scrollview setShowsHorizontalScrollIndicator:NO];

self.scrollview.delegate = self;

//设置滚动视图下面的点

self.pagecon = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.scrollview.frame)-20, self.view.frame.size.width, 20)];

//设置的点数

[self.pagecon setNumberOfPages:4];

[self.pagecon setPageIndicatorTintColor:[UIColor blackColor]];

[self.view addSubview:self.pagecon];

self.edgesForExtendedLayout = UIRectEdgeNone;

self.automaticallyAdjustsScrollViewInsets = YES;

}

//调用 UIscrollview 的协议方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

[self.pagecon setCurrentPage:(NSInteger)(scrollView.contentOffset.x / CGRectGetWidth(scrollView.frame))];

}

//封装的定时器

- (void)initTimer{

if (!self.timer) {

self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(click) userInfo:nil repeats:YES];

//定时器会跑在标记为common modes的模式下,以上两种通用,像广告轮播等就会用到该模式。

[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

/*self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(click) userInfo:nil repeats:YES];

[[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];

*/

}

}

-(void)click{

NSInteger pagecount = self.pagecon.currentPage + 1;

if (pagecount >= 4) {

pagecount = 0;

}

[self.scrollview setContentOffset:CGPointMake(pagecount * CGRectGetWidth(self.scrollview.frame), 0) animated:YES];

}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{

if (self.timer) {

//invalidate 废弃

[self.timer invalidate];

[self setTimer:nil];

}

}

//结束后返回第一张图片

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

[self initTimer];

}

相关文章

  • 自动小轮播图

    //封装UIScrollView 方法 - (void)initscroll{ //初始化 self.scroll...

  • 移动端swiper轮播图

    完整轮播图: spaceBetween:轮播图每张图之间的空格,设置为0就没有间距autoplay:自动轮播-- ...

  • 异步与回调函数-call

    设置轮播图不自动滚动

  • js轮播图

    最近在网上找了很多写轮播图的例子,有些只有左右轮播,没有自动轮播,而有些只有自动轮播,没有左右轮播,有些又没有下方...

  • android轮播图效果

    先上效果图: viewpager+handler+runnableTask实现轮播图效果。可以自动轮播,左右无限滑...

  • swif_播图

    轮播图 实现图片自动翻转(UIViewController,UIScrollViewDelegate,Timer)

  • 无限Banner轮播图

    GJImageCarouselView 自己写的Banner轮播图,自动循环,无限轮播。可以设置时间间隔、占位图。...

  • 传统&呼吸 轮播

    传统的轮播图 一个 carousel 轮播图,图片实现自动轮播,可以左右按钮播放,点小圆点也能实现换图。同时设置节...

  • 轮播图(2018-08-09)

    轮播图在生活中很常见,比如网页的广告宣传什么的。轮播图的写法也有很多种,有手动点击轮播的,还有自动轮播的,当然也有...

  • 技术练习的demo集

    demo1 实现优酷轮播图效果 页面效果展示 功能:自动轮播,hover停止轮播,点击left/right上一页/...

网友评论

      本文标题:自动小轮播图

      本文链接:https://www.haomeiwen.com/subject/imxzvxtx.html