ios轮播

Posted layfork

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于ios轮播的主要内容,如果未能解决你的问题,请参考以下文章

iOS-图片轮播-SDCycleSCrollView的使用

iOS UICollectionView 实现轮播图

最全最详细publiccms常用的代码片段

iOS UIScrollView 实现轮播图

iOS开发项目实战——Swift实现图片轮播与浏览

iOS UIScrollView 实现轮播图