//
// ViewController.m
// Test5
//
// Created by lagou on 15/11/2.
// Copyright © 2015年 lagou. All rights reserved.
//
#import "ViewController.h"
#import "UIView+CVUIViewAdditions.h"
@interface ViewController ()<UIScrollViewDelegate>
@property(nonatomic, strong) UIScrollView *scrollView;
@property(nonatomic, strong) UILabel *label;
@property(nonatomic, strong) NSTimer *timer;
@property(nonatomic, assign) BOOL isDrag;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, self.view.width, 200)];
_scrollView.pagingEnabled = YES;
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.delegate = self;
_scrollView.bounces = NO;
for (int i = 0; i < 7; i++) {
NSString *name = [NSString stringWithFormat:@"icon_shard_%d", i == 0 ? 5 : (i == 6 ? 1 : i)];
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:name]];
imgView.frame = CGRectMake(_scrollView.width*i, 0, _scrollView.width, _scrollView.height);
[_scrollView addSubview:imgView];
}
_scrollView.contentSize = CGSizeMake(_scrollView.width * 7, _scrollView.height);
_scrollView.contentOffset = CGPointMake(_scrollView.width, 0);
[self.view addSubview:_scrollView];
_label = [[UILabel alloc] initWithFrame:CGRectMake(_scrollView.width - 40, _scrollView.bottom - 25, 40, 20)];
_label.text = @"1";
[self.view addSubview:_label];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self addTimer];
}
- (void)addTimer {
_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeUpdate) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}
- (void)deleteTimer {
if (_timer) {
[_timer invalidate];
_timer = nil;
}
}
- (void)timeUpdate {
NSLog(@"update");
int i = (_scrollView.contentOffset.x + 0.5 * _scrollView.width) / _scrollView.width + 1;
if (i == 7) {
i = 2;
}
[_scrollView setContentOffset:CGPointMake(_scrollView.width * i, 0) animated:YES];
}
#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// NSLog(@"scroll");
int i = (_scrollView.contentOffset.x + 0.5 * _scrollView.width) / _scrollView.width;
int number = i == 0 ? 5 : (i == 6 ? 1 : i);
_label.text = [NSString stringWithFormat:@"%d", number];
if (_scrollView.contentOffset.x == 0) {
_scrollView.contentOffset = CGPointMake(_scrollView.width*5, 0);
} else if (_scrollView.contentOffset.x == _scrollView.width * 6) {
_scrollView.contentOffset = CGPointMake(_scrollView.width * 1, 0);
}
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[self deleteTimer];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
[self addTimer];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
一共是5张图片,前面加上最后一张图片,末尾加上最后一张图片,一共7张图片。
注意事项:
1.当用户拖动图片时,图片不应该自动跳转,所以要在drag开始时startTimer,endDrag时deleteTimer
2.一定要设置
_scrollView.bounces =NO;
如果设置成YES,一般情况是觉不出来的,只有当快速滑动图片,当到达最后一张图片时,会卡顿,也不是卡顿,就是需要多滑几次才能过去。
3.计算页数时在写成
int i = (_scrollView.contentOffset.x +0.5 *_scrollView.width) /_scrollView.width;
如果写成
int i = (_scrollView.contentOffset.x) /_scrollView.width; 页数只有当图片全部滑过去的时候才会改变,用户会感觉有延迟。