iOS图片轮播器的使用

来源:互联网 发布:看看电视剧的软件 编辑:程序博客网 时间:2024/06/05 14:42


#import "ViewController.h"


@interface ViewController ()<UIScrollViewDelegate>

@property (weak,nonatomic) IBOutletUIScrollView *scrollView;

@property (weak,nonatomic) IBOutletUIPageControl *pageControl;


@property (nonatomic,strong)NSTimer *timer;


@end


@implementation ViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    

    

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

        //获取图片名字

        NSString *imageName = [NSStringstringWithFormat:@"img_%02d",i];

        //获取图片对象

        UIImage *image = [UIImageimageNamed:imageName];

        

        // 创建图片控件

        // 1. 实例化

        UIImageView *imageView = [[UIImageViewalloc]init];

        

        // 2. 设置属性

        //2.1 计算X

        CGFloat imageViewX = i * (self.scrollView.bounds.size.width );

        

        imageView.frame =CGRectMake(imageViewX, 0,self.scrollView.bounds.size.width  , self.scrollView.bounds.size.height);

        

        //2.2 设置图片

        imageView.image = image;

        

        // 3.添加到对应的控件中

        [self.scrollViewaddSubview:imageView];

        

        

    }

    

    //让界面滚动

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

    

    

    // 让界面有分页效果

    self.scrollView.pagingEnabled =YES;

    

    // 取消滚动条

    self.scrollView.showsVerticalScrollIndicator =NO;

    self.scrollView.showsHorizontalScrollIndicator =NO;

    

    //取消弹簧效果  bounces 弹簧

    self.scrollView.bounces =NO;

    

    

    

    //设置指示器

    self.pageControl.numberOfPages =5;

    

    // 设置当前页

    self.pageControl.currentPage =0;

    

  

    //1. 设置代理对象

    self.scrollView.delegate =self;

    

    //2. 遵循协议

//    @interface ViewController ()<UIScrollViewDelegate>

    //3. 实现方法

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

    


    [selfstartTimer];

    

    

}



- (void)startTimer{

    // 添加一个定时器让它自动翻页

    //TimeInterval:<#(NSTimeInterval)#>代表间隔的时间

    //target 设置响应方法的对象

    //selector 响应的方法

    //    userInfo 额外的参数

    //    repeats 是否重复

    //    定时器的强引用是有系统进行维护的

    //    由于系统维护的强引用,所以要通过一个方法进行销毁

    NSTimer *timer = [NSTimerscheduledTimerWithTimeInterval:2target:selfselector:@selector(nextPage)userInfo:nilrepeats:YES];

    


    self.timer = timer;

    

    

    //[注]此处为了防止有textView使用的情况下UIScrollView不工作了

//    NSDefaultRunLoopMode;

//     NSRunLoopCommonModes

    [[NSRunLoopmainRunLoop] addTimer:timerforMode:NSRunLoopCommonModes];


}


- (void)stopTimer{


    //    invalidate使无效

    [self.timerinvalidate];

    //无论timer定义全局属性的时候使用strong还是weak

    // 都必须在调用invalidate方法之后,让成员属性等于nil

    // 官方文档的实例代码也是这么写的-->如果不这样写,很有可能导致定时器无法销毁

    self.timer =nil;

}



//显示下一页

- (void)nextPage{


    

    CGPoint offset =self.scrollView.contentOffset;

    

    if (offset.x >=4 * self.scrollView.bounds.size.width) {

        //最后一页

        offset.x =0;

    }else{

        offset.x +=self.scrollView.bounds.size.width;

    }

    

    [self.scrollViewsetContentOffset:offset animated:YES];


}


/**

 *  正在滚动的时候调用的方法

 */

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

//    

//

//

//    //1.判断scrollView的偏移,来得到当前的页数

//    //1.1 获取偏移

//    CGPoint offset = scrollView.contentOffset;

//  

////    NSLog(@"当前偏移 %@",  NSStringFromCGPoint(offset));

//    

//    //1.2 获取当前的页数

//    NSInteger currentPage = (offset.x + self.scrollView.bounds.size.width * 0.5) / self.scrollView.bounds.size.width;

//

//    

//    //1.3 设置pageControl的页数

//    self.pageControl.currentPage = currentPage;

//

//}



// 开始拖拽

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


    // 停止定时器

    [selfstopTimer];

    

}


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


    //开启定时器

    [selfstartTimer];

}


@end




事例图片


0 0
原创粉丝点击