登录 立即注册
金钱:

Code4App-iOS开发-iOS 开源代码库-iOS代码实例搜索-iOS特效示例-iOS代码例子下载-Code4App.com

无限 滑动滚动视图

[复制链接]
来自: colervie 分类: iOS精品源码 上传时间: 2016-5-24 20:56:17

项目介绍:

之前见过一些自动滚动视图感觉翻到头的时候会有特别难看的效果,看了好多别人写的,感觉看不懂自己写了一下新的。。。,希望大家建议

主要实现代码:
[Objective-C] 查看源文件 复制代码
#import "ViewController.h"
#define SCREENWIDTH [UIScreen mainScreen].bounds.size.width
#define IMAGE_COUNT self.imagearr.count
@interface ViewController ()<UIScrollViewDelegate>
//轮播弃
@property(strong,nonatomic) UIScrollView *scrollview;
@property(strong,nonatomic)UIImageView *leftimage;
@property(strong,nonatomic)UIImageView *rightimage;
@property(strong,nonatomic)UIImageView *currentimage;
//图片数组
@property(strong,nonatomic) NSArray* imagearr;
//当前图片序号
@property(assign,nonatomic)NSInteger currentimageindex;
@property(strong,nonatomic)NSTimer *scrolltimer;
@end
/**/
@implementation ViewController
#pragma -
#pragma - 懒加载
-(NSTimer *)scrolltimer
{
    if (!_scrolltimer) {
        _scrolltimer=[NSTimer timerWithTimeInterval:2 target:self selector:@selector(actionview) userInfo:nil repeats:YES];
    }
    return _scrolltimer;
}
-(void)actionview
{
    [UIView beginAnimations:@"move" context:nil];
    [UIView setAnimationDuration:0.3];
    self.scrollview.contentOffset =CGPointMake(SCREENWIDTH*2, 0);
//    if (self.currentimageindex<2) {
//         self.currentimageindex++;
//    }
//    else{
//     _currentimageindex=(_currentimageindex +1)%2;
//    }
    [UIView commitAnimations];
    [self performSelector:@selector(scrollViewDidEndDecelerating:) withObject:nil afterDelay:0.31];
}
//图片数组
-(NSArray *)imagearr
{
//    NSMutableArray *arr=[[NSMutableArray alloc]initWithCapacity:5];

    if (!_imagearr) {
        NSMutableArray *arr=[[NSMutableArray alloc]initWithCapacity:10];
        NSInteger i=0;
        for ( i=0; i<5; i++) {
           
            NSString *imagenamed=[[NSString alloc]initWithFormat:@"%li",i+1];
           UIImage * image = [UIImage imageNamed:imagenamed];
            
            [arr addObject:image];
//            NSLog(@"%@",arr);
        }
        
        _imagearr=[[NSArray alloc]initWithArray:arr];
    }
    
    

    return _imagearr;
}
-(UIScrollView *)scrollview
{
    if (!_scrollview) {
//        滚动视图
        _scrollview =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 200, SCREENWIDTH, 150)];
        _scrollview.pagingEnabled=YES;
        _scrollview.contentSize=CGSizeMake(SCREENWIDTH*3, 0);
        _scrollview.bounces= NO;
        _scrollview.showsHorizontalScrollIndicator=NO;
        _scrollview.showsVerticalScrollIndicator=NO;
        _scrollview.delegate  = self;
//        初始偏移量
        _scrollview.contentOffset =CGPointMake(SCREENWIDTH, 0);
//        设置中间图片的序号是0;
        self.currentimageindex=0;
//   设置三个图片视图
        
        self.leftimage= [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 150)];
        self.leftimage.image=[self.imagearr lastObject];
        [_scrollview addSubview:self.leftimage];
        
        
        self.currentimage=[[UIImageView alloc]initWithFrame:CGRectMake(SCREENWIDTH, 0, SCREENWIDTH, 150)];
        self.currentimage.image=[self.imagearr objectAtIndex:0];
        [_scrollview addSubview:self.currentimage];
        
        self.rightimage =[[UIImageView alloc]initWithFrame:CGRectMake(SCREENWIDTH*2, 0, SCREENWIDTH, 150)];
        self.rightimage.image=[self.imagearr objectAtIndex:1];
        [_scrollview addSubview:self.rightimage];
        
    
    
    
    
    }

    return _scrollview;

}

#pragma -
#pragma - 滚动视图的代理方法
//    每次滑动视图移动结束后自动调用
//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//    更新图片
    [self reloadimages];
//  将滚动视图的偏移量修改回screenwidth
//    [self.scrollview setContentOffset:CGPointMake(SCREENWIDTH, 0) animated:NO];
    [self.scrollview setContentOffset:CGPointMake(SCREENWIDTH, 0) animated:NO];

}
-(void)reloadimages
{
    NSInteger leftimageindex = 0;
    NSInteger rightimageindex =0;
    //偏移量
    CGPoint offSet =self.scrollview.contentOffset;
//   判断滚动是向左还是向右
    if(offSet.x == SCREENWIDTH *2)
    {
        _currentimageindex=(_currentimageindex +1)%IMAGE_COUNT;
    
    }
    else if(offSet.x == SCREENWIDTH *0)
    {   //更新中间显示的图片;
        if (_currentimageindex-1<0) {
//            如果是初始位置那么计算公式如下
//            整体长度-(当前下标-1)的绝对值;
//            IMAGE_COUNT-(-(_currentimageindex -1))%IMAGE_COUNT;
            _currentimageindex=IMAGE_COUNT-1;
        }else{
        _currentimageindex=(_currentimageindex -1)%IMAGE_COUNT;
            }
    
    }
    _currentimage.image =[self.imagearr objectAtIndex:_currentimageindex];
//    跟新左右两张图的图片
    if (_currentimageindex-1<0) {
       leftimageindex = IMAGE_COUNT-1;
    }else{
            leftimageindex = (_currentimageindex -1)%IMAGE_COUNT;
    }
   
    rightimageindex =(_currentimageindex +1)%IMAGE_COUNT;
    _leftimage.image=[self.imagearr objectAtIndex:leftimageindex];
    _rightimage.image=[self.imagearr objectAtIndex:rightimageindex];
}
#pragma -
#pragma - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor grayColor];
    [self.view addSubview:self.scrollview];
    [[NSRunLoop currentRunLoop]addTimer:self.scrolltimer forMode:NSRunLoopCommonModes];

}

@end



自动轮播.zip (552.8 KB, 下载次数: 347)
1020 1 0
联系我们
首页/微信公众账号投稿

帖子代码编辑/版权问题

QQ:435399051,742864542


代码贡献英雄榜
用户名 下载数
通过邮件订阅最新 Code4App 信息
上一条 /4 下一条
联系我们
关闭
合作电话:
13802416937
Email:
435399051@qq.com
商务市场合作/投稿
问题反馈及帮助
联系我们

广告投放| 广东互联网违法和不良信息举报中心|中国互联网举报中心|Github|申请友链|手机版|Code4App ( 粤ICP备15117877号-1 )

快速回复 返回顶部 返回列表