UISrcollView自动滚动

//
//

//
//手动左右无缝已经写完,差自动滚动,<方向:向右>带切换动画.
//

//

//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic ,strong)UIScrollView *scrollView;
@property (nonatomic ,strong)UIPageControl *pageControl;
@property (nonatomic ,strong)NSTimer *timer;
@property (nonatomic ,assign)CGFloat width;
@property (nonatomic ,assign)CGFloat height;

@end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    _width = [[UIScreen mainScreen] bounds].size.width;
    _height = [[UIScreen mainScreen] bounds].size.height;
    _scrollView = [[UIScrollView alloc] init];
    _scrollView.frame = self.view.bounds;
    _scrollView.backgroundColor = [UIColor brownColor];
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    _scrollView.contentSize = CGSizeMake(_width*7, _height);
    _scrollView.contentOffset = CGPointMake(_width, 0);

    for (int i = 0; i<7; i++)
    {
    UIImageView imgV = [UIImageView new];
    imgV.frame = CGRectMake(i
    _width, 0, width, _height);
    imgV.contentMode = UIViewContentModeScaleAspectFit;
    NSString *str = [NSString stringWithFormat:@"IMG
    %i",i+1];
    imgV.image = [UIImage imageNamed:str];
    [_scrollView addSubview:imgV];
    }

    [self.view addSubview:_scrollView];

    _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(20, _height*0.8, _width-40, 50)];
    _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    _pageControl.currentPageIndicatorTintColor = [UIColor blueColor];
    _pageControl.numberOfPages = 5;
    _pageControl.currentPage = 0;
    [self.view addSubview:_pageControl];

// _timer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(dddselecteddd) userInfo:nil repeats:YES];

}

//- (void)dddselecteddd
//{
//

// CGFloat aa = _scrollView.contentOffset.x;
// aa += _width;
// [UIView animateWithDuration:0.5 animations:^{
// _scrollView.contentOffset= CGPointMake(aa, 0);
// }];
//

//

// if (_scrollView.contentOffset.x > 5*_width)
// {
// _scrollView.contentOffset = CGPointMake(0, 0);
// }
//

//

//}

#pragma - mark 滑动;

  • (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
    NSInteger page = _scrollView.contentOffset.x/_width;
    _pageControl.currentPage = page-1;

    if (_scrollView.contentOffset.x == 6*_width)
    {
    _scrollView.contentOffset = CGPointMake(_width, 0);
    }
    else if (_scrollView.contentOffset.x == 0*_width)
    {
    _scrollView.contentOffset = CGPointMake(5*_width, 0);
    }
    }

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

@end

原理就是一个长条形的div放置几张图片,然后复制这个div放到右边(上下左都可以,看你滚动方向),隐藏滚动条,然后设置滚动条的偏移,滚动条滚动到最右侧的时候偏移赋值为0,从头开始(我是做js的,你写的真心看不懂,仅供参考)