UIScrollerView ,UIPageControl混搭使用,添加定时器,无限循环滚动

- (void)viewDidLoad {

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width,240)];

    [self.view addSubview:view];

    //创建实例化UIScrollerView

    imageScrollerview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,[UIScreenmainScreen].bounds.size.width, 240)];

    imageScrollerview.contentSize = CGSizeMake(imageScrollerview.frame.size.width*6,0);   //必须设置的,滚动范围

    imageScrollerview.scrollEnabled = YES;   //是否支持滚动

    imageScrollerview.pagingEnabled = YES;  //整页滚动

    imageScrollerview.delegate = self;

    imageScrollerview.showsVerticalScrollIndicator = false;  //隐藏滑轮

    imageScrollerview.showsHorizontalScrollIndicator = false;

    [view addSubview:imageScrollerview];

    

    for (int i = 0; i<6; i++) {

        NSString *str = [NSString stringWithFormat:@"%d.jpg",i];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width*i, 0,[UIScreen mainScreen].bounds.size.width, imageScrollerview.frame.size.height)];

        imageView.image = [UIImageimageNamed:str];

        [imageScrollerview addSubview:imageView];

    }

    //设置起始位置 setContentOffset设置位置的,固定位置

    [imageScrollerview setContentOffset:CGPointMake([UIScreenmainScreen].bounds.size.width, 0)];

    //UIPageControl初始化

    pageControl = [[UIPageControlalloc] initWithFrame:CGRectMake([UIScreenmainScreen].bounds.size.width-100,imageScrollerview.frame.size.height-20, 100, 20)];

 pageCOntrol.currentPage = 0;   //设置当前页

    pageControl.numberOfPages = 4;

    [view addSubview:pageControl];

    

    //计时器

    NSTimer *time = [NSTimer scheduledTimerWithTimeInterval:3.0target:selfselector:@selector(updateTime:) userInfo:nilrepeats:YES];

    [[NSRunLoop currentRunLoop] addTimer:time forMode:NSDefaultRunLoopMode];

}

//计时器,3s一次

-(void) updateTime:(id) sender

{

    CGPoint point = imageScrollerview.contentOffset;

    if(point.x == [UIScreen mainScreen].bounds.size.width*4)

    {

        [imageScrollerview setContentOffset:CGPointMake(0, 0) animated:NO];

         imageScrollerview.frame = CGRectMake(0, 0, [UIScreenmainScreen].bounds.size.width, 240); //防止位置跳动

        

         [imageScrollerviewscrollRectToVisible:CGRectMake([UIScreenmainScreen].bounds.size.width, 0, [UIScreenmainScreen].bounds.size.width, 240) animated:YES];

        pageControl.currentPage = imageScrollerview.contentOffset.x/[UIScreenmainScreen].bounds.size.width;

    }

    else

    {

        [imageScrollerview scrollRectToVisible:CGRectMake(point.x + [UIScreenmainScreen].bounds.size.width, 0, [UIScreenmainScreen].bounds.size.width, 240) animated:YES];  //移动设置

        pageControl.currentPage = imageScrollerview.contentOffset.x/[UIScreen mainScreen].bounds.size.width;

    }

}

#pragma mark UIScrollerViewDelegate

-(void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{

    if (imageScrollerview.contentOffset.x == 0)

    {

        [imageScrollerview setContentOffset:CGPointMake([UIScreenmainScreen].bounds.size.width*4, 0) animated:NO];

        imageScrollerview.frame = CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 240);//防止位置跳动

        

    }

    elseif (imageScrollerview.contentOffset.x == [UIScreen mainScreen].bounds.size.width*5)

    {

        [imageScrollerview setContentOffset:CGPointMake([UIScreen mainScreen].bounds.size.width, 0) animated:NO];

        imageScrollerview.frame = CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 240);//防止位置跳动

    }

    

    pageControl.currentPage = imageScrollerview.contentOffset.x/[UIScreen mainScreen].bounds.size.width - 1;

}

时间: 2024-07-30 13:43:42

UIScrollerView ,UIPageControl混搭使用,添加定时器,无限循环滚动的相关文章

【Android】ViewPager实现无限循环滚动

最近做的一个项目,客户要求在ViewPager实现的主页面中滑动到最后一页后继续滑动能返回到第一页,也就是实现无限循环滚动,效果如下: 看了下ViewPager没有滑到尽头的回调方法,因此想到的解决方案是,在原来的最后一页之后再加上一个第一页,也就是原本有编号为a1,b,c的三个页面,现在在最后面再加一个a页面,变为a1.b.c.a2四个页面,然后使用OnPageChangeListener中的onPageSelected方法来监听到页面切换,当发现是从第三个页面(c)切换到第四个页面(a1)时

iOS无限循环滚动实现

原文链接: iOS无限循环滚动实现 简书主页:http://www.jianshu.com/users/37f2920f6848 Github主页:https://github.com/MajorLMJ iOS开发者公会-技术1群 QQ群号:87440292 iOS开发者公会-技术2群 QQ群号:232702419 iOS开发者公会-议事区   QQ群号:413102158

unity 背景无限循环滚动效果

背景无限循环滚动效果如下示: 步骤如下: 导入背景图片后,设置图片的格式,如下图: 2.图片格式也可以设置是Texture格式,但是Wrap Mode 一定要是Repeat[重复发生]:然后记得Apply一下.[解释:Wrap mode :循环模式:换行模式:包裹模式:缠绕] 3.在Hierachy视图中,新建2D Object-〉Sprite 或者一个Image,将之前设置好的精灵放入Texture. 4.在Project视图鼠标右键新建一个Shader,命名随意,然后在编辑器中修改Shade

iOS无限循环滚动scrollview

经常有园友会问"博主,有没有图片无限滚动的Demo呀?", 正儿八经的图片滚动的Demo我这儿还真没有,今天呢就封装一个可以在项目中直接使用的图片轮播.没看过其他iOS图片无限轮播的代码,也不了解他们的原理,我今天封装这个图片无限轮播是借鉴Web前端中的做法,因为之前写Web前端的时候,实现幻灯片就是这么做的,今天就在iPhone上搞搞.下面的东西是自己写的了,关于轮播的东西这个开源项目也是相当不错的https://github.com/nicklockwood/iCarousel ,

无限循环滚动视图思路一

一.思路分析 不考虑内存 1.如果有5张图片,可以放7张UIImageView,排列是 4 0 1 2 3 4 0,但图片多时对内存太依赖. 优化内存 1.需要用3个UIImageView来实现即可. 初始状态 - 一开始,把contentoffset设置到中间的那张imageView上,也就是显示中间的imageView,并且以后每次滚动完都会进行这样的设置,这里先记住 - 图中显示的是第0张图片,所以向左滑动显示的肯定是最后一张图片,也就是第4张,同理,右滑是第1张 - 所以,三张image

Cocos2dx中利用双向链表实现无限循环滚动层

[Qboy原创] 在Cocos2dX 3.0 中已经实现一些牛逼的滚动层,但是对于有一些需要实现循环滚动的要求确没有实现,笔者在前段时间的一个做了一个游戏,需求是实现在少有的(13个)英雄中进行循环滚动层,即用户可以无限的向一个方向滚动,当到最后时,由前面的进行重复出现. 如下图: 为了满足以上需求,我第一反应就想到了采用大学数据结构中所学的双向链表.想想还真称靠谱诶.那就说干就干吧. 1.定义双向链接表结构: struct CycNode{//构建双向链表结构 CycNode* preNode

viewpager实现无限循环滚动幻灯片

一直想做循环滑动幻灯片的效果,类似pptv等的首页效果十分抱歉,不会整gif动图--- 废话少说,先上图看效果:    思路是:设置pageradapter的count为Integer.MAX_VALUE,但实际的item只有几个,用取余的方式取item,在设置adapter时同时设置currentItem为实际item数的N倍(足够大就好).这样就可以左右无缝循环滑动!(毕竟你不可能滑上亿次),并且不用担心内存问题,就那几个玩意--- 这里不能用FragmentPagerAdapter或Fra

详细分析Android viewpager 无限循环滚动图片

由于最近在忙于项目,就没时间更新博客了,于是趁着周日在房间把最近的在项目中遇到的技术总结下.最近在项目中要做一个在viewpager无限滚动图片的需求,其实百度一下有好多的例子,但是大部分虽然实现了,但是讲的都不清楚,我查了很多资料,我就知道position/list.size().但是怎么用,我还是不明白.我后来看了百度工程师任玉刚的循环广告位组件的实现突然明白了无限滚动的原理.总结如下: 1.要在int getCount() 方法里 写 return BANNER_SIZE.  这个 BAN

最简单粗暴的方式实现无限循环滚动的UIScrollView

// // ViewController.swift // UIScrollView_Example // // Created by xxx on 16/4/1. // Copyright © 2016年 xxx. All rights reserved. // import UIKit class ViewController: UIViewController, UIScrollViewDelegate { var scrollView: UIScrollView! var childCa