• 广告轮播器源码


    1.创建容器视图

       

     UIScrollView * scrollView = [[UIScrollView alloc] init];
    
        [self.view addSubview:scrollView];
    
        scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, 200);
    
        scrollView.backgroundColor = [UIColor yellowColor];
    
        self.scrollView = scrollView;
    
        

    2.添加内容子视图

       

     for(int i = 0; i < 5; i++)
    
        {
    
            UIImageView * pictImageView = [[UIImageView alloc] init];
    
            [scrollView addSubview:pictImageView];
    
            NSString * name = [NSString stringWithFormat:@"ad_%02d",i];
    
            pictImageView.image = [UIImage imageNamed:name];
    
            设置Frame
    
            CGFloat pictW = scrollView.frame.size.width;
    
            CGFloat pictH = scrollView.frame.size.height;
    
            CGFloat pictX = i * pictW;//i * scrollView.frame.size.width
    
            CGFloat pictY = 0;
    
            pictImageView.frame = CGRectMake(pictX, pictY, pictW, pictH);
    
        }//end for
    
     
     

      设置contentSize,只能够横向滚动

        scrollView.contentSize = CGSizeMake(5 * scrollView.frame.size.width, 0);

        scrollView.showsHorizontalScrollIndicator = NO;

        是否支持分页功能

        scrollView.pagingEnabled = YES;

        

        指示器,用来提示用户当前显示到的具体页面提示

        UIPageControl * pageController = [[UIPageControl alloc] init];

        [self.view addSubview:pageController];

        pageController.frame = CGRectMake(0, 180, scrollView.frame.size.width, 20);

        pageController.backgroundColor = [UIColor redColor];

        

        设置一共有多少个提示视图(小圆点)

        pageController.numberOfPages = 5;

        当前指示页面的小点颜色

        pageController.currentPageIndicatorTintColor = [UIColor greenColor];

        其它小点颜色

        pageController.pageIndicatorTintColor = [UIColor grayColor];

        当前指示第几个小点上

        pageController.currentPage = 0;

     
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        1.时时获得当前偏移量
    
        CGFloat offSetX = scrollView.contentOffset.x;
    
        计算出当前滚动到的具体页数
    
        NSInteger currentPage = (offSetX + scrollView.frame.size.width * 0.5 )/ scrollView.frame.size.width;
    
        2.设置pegeControler
    
        self.pageController.currentPage = currentPage;
    }

      为了防止NSTimer 产生的时间累积效果产生的屏幕视图闪动,我们在人为拖拽的时候,取消自动滚动效果

    - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
    {
        取消timer功能,一旦调用了invalidate 方法之后,timer对象是不能够再从新复活
    
        [self.timer invalidate];
    
        清空一下timer对象
    
        self.timer = nil;
    }
    - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
    {
        重新启动timer对象
        [self.timer  fire]; //调用该方法是无用的,一旦调用了invalidate是不能够再次复活的,只能够重新创建
        [self createTimer];
    }
    
     
    - (void)createTimer
    {
       NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(autoScroll) userInfo:nil repeats:YES];
        self.timer = timer;
    
        把timer对象加入到循环回路中,NSRunLoopCommonModes通信模式
    
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    
        
    
    }
    - (void)autoScroll
    {
       NSLog(@"开始自动滚动了");
    
        /*
           scrollView滚动效果,实际上就是改变bounds也就是改变contentOffSet某一个方向的值
    
           当前显示在第几页 *scrollView.width
    
           如果已经显示到最后一页,那么归0
    
       */
        NSInteger currentPage =  self.pageController.currentPage >= 4 ? 0 : self.pageController.currentPage + 1;
    
        CGFloat offsetX = currentPage * self.scrollView.frame.size.width;
    
         self.scrollView.contentOffset = CGPointMake(offsetX, 0);
    }
    
     
  • 相关阅读:
    CA认证
    浅谈个人融资秘诀
    外挂编写完全攻略
    字符串之单词原地逆转
    推荐系统(1)--splitting approaches for context-aware recommendation
    BZOJ 1122 POI2008 账本BBB 单调队列
    一起talk C栗子吧(第一百二十一回:C语言实例--线程知识体系图)
    几种内存数据的总结
    Android学习路线(十九)支持不同设备——支持不同(Android)平台版本号
    毕业课题之------------图像的形态学滤波
  • 原文地址:https://www.cnblogs.com/CLiOS/p/5291610.html
Copyright © 2020-2023  润新知