zoukankan      html  css  js  c++  java
  • ios轮播

    //
    //  ViewController.m
    //  Ocproject
    //
    //  Created by wenzhe yi on 2018/2/28.
    //  Copyright © 2018年 wenzhe yi. All rights reserved.
    //
    
    #import "Shuanghui.h"
    #import "ViewController.h"
    
    
    
    #define imageCount 5
    
    #define kscrollviewsize (_scrollView.frame.size)
    
    
    @interface ViewController()<UIScrollViewDelegate>
    
    @property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
    @property (weak, nonatomic) IBOutlet UIPageControl *pageController;
    
    @property(strong,nonatomic) NSTimer *timer;
    
    @end
    
    
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        
        [self initUi];
        
        [self initPageController];
        [self initTimer];
        
    }
    
    -(void)initTimer{
        _timer  = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(autoBanner) userInfo:nil repeats:YES];
    }
    -(void)initUi{
        [self initScrollView];
       
    }
    
    //开始拖拽的时候计时器停止
    -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
        [_timer invalidate];
    }
    
    
    //拖拽结束的时候在开启定时器
    -(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
        [self initTimer];
    }
    -(void)initPageController{
        _pageController.numberOfPages=imageCount;
        
        
        //非选中颜色
        _pageController.pageIndicatorTintColor=[UIColor grayColor];
        
        //选中的颜色
        _pageController.currentPageIndicatorTintColor=[UIColor redColor];
        
        
        //当前页
        _pageController.currentPage=0;
    }
    -(void)initScrollView{
    //    UIImageView *iv = [[UIImageView alloc]initWithFrame:_scrollView.bounds];
    //
    //    //设图片
    //    iv.image=[UIImage imageNamed:@"img_01"];
    //
    //    //添加
    //    [_scrollView addSubview:iv];
        
        
       // CGSize scrollViewSize=_scrollView.frame.size;
        
        for (int i=0; i<imageCount; i++) {
            
            CGFloat imageX=i*kscrollviewsize.width;
            
            UIImageView *iv =[[UIImageView alloc] initWithFrame:CGRectMake(imageX, 0, kscrollviewsize.width, kscrollviewsize.height)];
            
            //imagepath string
            NSString *path=[NSString stringWithFormat:@"img_%02d",i+1];
            
            //设置pic
            iv.image=[UIImage imageNamed:path];
            
            //add
            
            [_scrollView addSubview:iv];
        }
        _scrollView.contentSize=CGSizeMake(5*kscrollviewsize.width, 0);
        
        //隐藏滚动条
        _scrollView.showsHorizontalScrollIndicator=NO;
        
        //分页效果
        _scrollView.pagingEnabled=YES;
        //代理
        _scrollView.delegate=self;
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    
    
    -(void)needMoreMeet{
        NSLog(@"I LIST");
    }
    -(void)scrollViewDidScroll:(UIScrollView *)scrollView{
       
        
    }
    -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
        
       
        _pageController.currentPage=scrollView.contentOffset.x/kscrollviewsize.width;
        
    }
    
    //无线循环轮播点击事件
    - (IBAction)move:(id)sender {
        [self autoBanner];
    }
    
    -(void)autoBanner{
        CGPoint offset=_scrollView.contentOffset;
        
        NSInteger currentPage = _pageController.currentPage;
        
        if(currentPage==imageCount-1){
            currentPage=0;
            offset=CGPointZero;
            
        }else{
            currentPage+=1;
            offset.x+=kscrollviewsize.width;
        }
        
        _pageController.currentPage=currentPage;
        [_scrollView setContentOffset:offset animated:YES];
        
    }
    
    @end
  • 相关阅读:
    WPF样式统一之DevExpress设置窗体,控件为Office风格
    vs报错 "多步操作产生错误。请检查每一步的状态值"
    WPF属性之理解附加属性
    WPF国际化方式1之资源文件
    EntityFramework经典数据访问层基类——增删改查
    一个sh脚本 同时运行 多个sh脚本
    安装OpenIMSCore的SIP测试客户端 utcimsclient
    No module named 'paddle.fluid'
    “/usr/local/lib/libosipparser2.so.7: could not read symbols: Invalid operation” 异常解决
    把ubuntu自带的高gcc版本降到低版本(如gcc 3.4)的方法
  • 原文地址:https://www.cnblogs.com/norm/p/8485472.html
Copyright © 2011-2022 走看看