UIScollView
创建一个简单的UIScollView
在viewDidLoad中实现如下代码
UIScrollView *scroll = [[UIScrollView alloc]init];
scroll.frame = self.view.frame;
//设置代理
scroll.delegate = self;
scroll.backgroundColor=[UIColor redColor];//设置颜色
[self.View addSubView:scroll];
实现代理:UIScrollViewDelegate
- -(void)scrollViewDidScroll:(UIScrollView *)scrollView; //scrollView已经开始滑动
- (void)scrollViewDidZoom:(UIScrollView *)scrollView;//开始缩放 放大或缩小
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;将要开始滑动
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;//scrollVIew已经结束滑动
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView; //将要开始减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView; //已经结束减速
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView;//滑动的动画已经结束
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;//返回yes的时候,返回到顶部 同理还有一个属性scrollsToTop=YES时,避免头部出现空白视图;
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view;//将要开始缩放
一个实现图片缩放的简单代码
- (void)viewDidLoad
{
[super viewDidLoad];
//模拟器上实现缩放 点option
UIScrollView *scroll = [[UIScrollView alloc]init];
scroll.frame = self.view.frame;
//设置代理
scroll.delegate = self;
scroll.backgroundColor=[UIColor redColor];
[self.view addSubview:scroll];
//最大缩放比例
scroll.maximumZoomScale = 3.0;
//最小缩放比例
scroll.minimumZoomScale = 0.5;
_imgview = [[UIImageView alloc]init];
_imgview.frame = CGRectMake(0, 0, 320, 480);
_imgview.image = [UIImage imageNamed:@"sun.png"];
[scroll addSubview:_imgview];
// Do any additional setup after loading the view, typically from a nib.
}
/*
实现缩放的3步:
1.设置最大缩放比例要大于最小缩放比例
2.设置代理
3.实现协议方法viewForZoomoingInScrollView 在这里要返回要进行缩放的子视图
*/
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
//返回要进行缩放的scrollview的子视图
return _imgview;
}
使用NSTimer和UIScrollVIew实现一个图片轮播器,第一张-->第五张-->第一张
.h
@interface ViewController : UIViewController<UIScrollViewDelegate>
{
int _speed;//记录方向
UIScrollView *_scroll;//
NSTimer *_timer;//计时器
float _offsetStartDrag; //开始拖动时,偏移量大小
}
@end
.m
- (void)viewDidLoad
{
[super viewDidLoad];
UIScrollView *scroll = [[UIScrollView alloc]init];
scroll.delegate = self;
scroll.frame = CGRectMake(0, 0, 320, 480);
[self.view addSubview:scroll];
for (int i = 0; i < 5; i ++) {
UIImageView *imgview = [[UIImageView alloc]init];
imgview.frame = CGRectMake(320*i, 0, 320, 480);
imgview.image = [UIImage imageNamed:[NSString stringWithFormat:@"fight%d",i+1]];
[scroll addSubview:imgview];
}
scroll.contentSize = CGSizeMake(320*5, 480);
scroll.pagingEnabled = YES;
_scroll = scroll;
_timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
_speed = 320;
}
-(void)onTimer {
float offset = _scroll.contentOffset.x;
if (offset >= 320*4) {
//已经到达最右边
_speed = -320;
}
else if (offset <= 0){
//已经到达最左边
_speed = 320;
}
offset += _speed;
[_scroll setContentOffset:CGPointMake(offset, 0) animated:YES];
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
//当用户开始拖动scrollview时,让定时器暂停
[_timer setFireDate:[NSDate distantFuture]];
//记录开始拖动时的偏移量 目的跟结果拖动的偏移量进行比较
_offsetStartDrag = scrollView.contentOffset.x;
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
//得到结束拖动到偏移量
float offsetEndDrag = scrollView.contentOffset.x;
if (_offsetStartDrag <= offsetEndDrag) {
//向右拖动
_speed = 320;
}
else{
_speed = -320;
}
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//当scrollview停止滚动时,2.0秒之后重新开启定时器
[_timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2.0]];
}
@end
配合图片轮播的一个黄金搭档-UIPageControl
初始化创建
_pgctl = [[UIPageControl alloc]init];
_pgctl.backgroundColor = [UIColor redColor];//
_pgctl.frame = CGRectMake(0, 20, 320, 20);
_pgctl.numberOfPages = 5;//显示的页码个数
[_pgctl addTarget:self action:@selector(onPgctlClick) forControlEvents:UIControlEventValueChanged];//点击时绑定的方法
[self.view addSubview:_pgctl];
-(void)onPgctlClick {//实现方法
[_timer setFireDate:[NSDate distantFuture]];//将计时器关闭
float offset = _pgctl.currentPage*320;//记录点击时scroll需要的偏移量
[_scroll setContentOffset:CGPointMake(offset, 0) animated:YES];//将scroll滑动到指定的位置;
[self performSelector:@selector(timerreStart) withObject:nil afterDelay:2.0];//两秒之后执行计时器;
/*performSelector的使用理解请结合runtime特性:
它要执行的方法在编译接单,如果没有实现,是不会报错的,但是执行时会崩溃,还有runLoop.... */
}
#pragma mark 启动计时器的方法
-(void)timerreStart {
[_timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:1]];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//当scrollview停止滚动时,2.0秒之后重新开启定时器
[_timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2.0]];
_pgctl.currentPage = scrollView.contentOffset.x/320;
}
-(void)onTimer {//计时器的绑定方法
float offset = _scroll.contentOffset.x;
if (offset >= 320*4) {
//已经到达最右边
_speed = -320;
}
else if (offset <= 0){
//已经到达最左边
_speed = 320;
}
offset += _speed;
[_scroll setContentOffset:CGPointMake(offset, 0) animated:YES];
_pgctl.currentPage = offset/320;
}