iOS UIScrollView

UIScrollView常用的属性

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

UIScrollViewDelegate 常用代理方法

// 返回要缩放的View
- (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {

    // 缩放 范围 (这两个属性不是写在这里的,写着这只是方便参考)
    //self.scrollView.maximumZoomScale = 2;
    //self.scrollView.minimumZoomScale = 0.4;

    return self.imageView;
}


// 正在滚动
- (void)scrollViewDidScroll {
    NSLog(@"正在滚动");
}

// 开始滚动
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    NSLog(@"开始滚动");
}

// 结束滚动
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    NSLog(@"结束滚动");
}

一个简单的例子:

这里写图片描述

这里写图片描述


#import "ViewController.h"

@interface ViewController () <UIScrollViewDelegate>
{

    NSTimer *timer;
}

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    int totalCount = 5;

     CGFloat imageW = self.scrollView.frame.size.width;

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

    NSString *string = [NSString stringWithFormat:@"%d",i+1];

       // NSLog(@"%@",string);

    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:string];

    CGFloat imageX = i * self.scrollView.frame.size.width;
    CGFloat imagaY = 0;
    CGFloat imageH = self.scrollView.frame.size.height;

    imageView.frame = CGRectMake(imageX, imagaY, imageW, imageH);

    [self.scrollView addSubview:imageView];    
  }
    self.scrollView.contentSize = CGSizeMake(totalCount * imageW , 0);

//    self.scrollView.contentMode = UIViewContentModeScaleAspectFit;

    // 滚动条隐藏
    self.scrollView.showsHorizontalScrollIndicator = NO;

    // 按页显示
    self.scrollView.pagingEnabled = YES;

    // 设置pageControl指示器 一个有多少点
    self.pageControl.numberOfPages = totalCount;
    // 起始点
    self.pageControl.currentPage = 0;


    // 设置代理
    self.scrollView.delegate = self;

    /**
     *  scheduledTimerWithTimeInterval 每隔多少秒
     *  target    在哪个对象内部调
     *  selector  你要调用哪个方法
     *  userInfo  是否要传递参数
     *  repeats   是否重复调用
     */

    timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(autoScr0ll) userInfo:nil repeats:YES];

    // 把timer 对象事件 交给主要线程处理 就不会出现阻塞(比如 有别的控件在操作,定时器可能就停止操作)
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

}

- (void)autoScr0ll {
    //1. 计算当前应显示到第几页

    int totalCount = 5;
    int page = self.pageControl.currentPage >= totalCount-1 ? 0: self.pageControl.currentPage+1;

    //2. 计算出ContentOffset的具体偏移量

    CGFloat offSetX = page * self.scrollView.frame.size.width;

    self.scrollView.contentOffset = CGPointMake(offSetX, 0);
    //3. 更新scrollView的偏移量

}


#pragma mark ----  代理方法  ----
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    // 1.获取当前偏移量
    CGPoint contentOffset = scrollView.contentOffset;

    // 2.根据偏移量除以scrollView宽度 得出当前滑动到了第几页
    int page = contentOffset.x / scrollView.frame.size.width;

    // 3.更改指示器
    self.pageControl.currentPage = page;

}


// 处理细节  当我拖动时 计时器就无法启动
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    // 当调用定时器 invalidate方法之后 就无法再调用定时器
    [timer invalidate];
}

// 当我 结束拖动 在启动定时器
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(autoScr0ll) userInfo:nil repeats:YES];

    // 把timer 对象事件 交给主要线程处理 就不会出现阻塞
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

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

@end

效果图

这里写图片描述

源码:下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值