UIScrollView-滚动背景

一.CABasicAnimation基本动画方式实现

1.工程链接

链接:
https://pan.baidu.com/s/1CthDU1zMykmJlnEpp9mk8w 密码:r0kj

2.开发流程

1.创建UIScrollView的一个对象 ,并且取消掉边界的回弹效果和横向和纵向的滚动条

    //滚动视图
    UIScrollView *scroll = [[UIScrollView alloc] 
                                          initWithFrame:self.view.bounds];
    //设置边界是否有回弹效果
    scroll.bounces = NO;
    //隐藏横向和纵向的滚动条
    scroll.showsHorizontalScrollIndicator = NO;
    scroll.showsVerticalScrollIndicator = NO;
    //显示
    [self.view addSubview:scroll];

2.创建UIImageView的一个对象,设置填充模式为按照宽高比缩放填充,并且设置好scroll的contentSize

    //获得图片
    UIImage *img =[UIImage imageNamed:@"beauty"];
    
    //图片视图
    self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width*3, self.view.frame.size.height)];
    _imgView.image = img;
    _imgView.contentMode = UIViewContentModeScaleAspectFill;
    [scroll addSubview:_imgView];
    
    //设置显示范围
    scroll.contentSize = CGSizeMake(self.view.frame.size.width*3, self.view.frame.size.height);

3.设置基本动画,并且允许动画自动返回,重复次数为CGFLOAT_MAX

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    //当前显示的是图片的最左端,往左移动,才会显示图片的右边区域
    CABasicAnimation *ani = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
    ani.fromValue = @(0);
    ani.toValue = @(-self.imgView.frame.size.width+self.view.frame.size.width);
    ani.duration = 4;
    ani.autoreverses = YES;
    ani.repeatCount = CGFLOAT_MAX;
    [self.imgView.layer addAnimation:ani forKey:nil];
}

3.运行结果

滚动背景

二.KVO方式实现

1.工程链接

链接:
https://pan.baidu.com/s/1M24W6fanRQYWgsSUM8Pu5Q 密码:vx5w

2.开发流程

1.创建UIScrollView的一个对象 ,并且取消掉边界的回弹效果和横向和纵向的滚动条

    //滚动视图
    UIScrollView *scroll = [[UIScrollView alloc] 
                                          initWithFrame:self.view.bounds];
    //设置边界是否有回弹效果
    scroll.bounces = NO;
    //隐藏横向和纵向的滚动条
    scroll.showsHorizontalScrollIndicator = NO;
    scroll.showsVerticalScrollIndicator = NO;
    //显示
    [self.view addSubview:scroll];

2.创建UIImageView的一个对象,设置填充模式为按照宽高比缩放填充,并且设置好scroll的contentSize

    //获得图片
    UIImage *img =[UIImage imageNamed:@"beauty"];
    
    //图片视图
    self.imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width*3, self.view.frame.size.height)];
    _imgView.image = img;
    _imgView.contentMode = UIViewContentModeScaleAspectFill;
    [scroll addSubview:_imgView];
    
    //设置显示范围
    scroll.contentSize = CGSizeMake(self.view.frame.size.width*3, self.view.frame.size.height);

3.监听temp属性值得变化

//监听temp属性值的变化
    [self addObserver:self forKeyPath:@"temp" 
             options:NSKeyValueObservingOptionNew context:nil];

4.kvo触发的事件

//kvo触发的事件
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    [UIView animateWithDuration:4 animations:^{
        self.imgView.transform = CGAffineTransformTranslate(self. imgView.transform, -(self.imgView.frame.size.width-self.view.frame.size.width), 0);
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:4 animations:^{
            self.imgView.transform = CGAffineTransformTranslate(self. imgView.transform, self.imgView.frame.size.width-self.view.frame.size.width, 0);
        } completion:^(BOOL finished) {
            //改变属性变量的值
            self.temp = !self.temp;
        }];      
    }];
}

5.改变属性的值,触发监听事件

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    //当前显示的是图片的最左端,往左移动,才会显示图片的右边区域
    
    //改变属性的值 触发 监听事件
    self.temp = 1;
}

3.运行结果

滚动背景
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,743评论 4 61
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,903评论 1 32
  • 文/灵林玖玖 最近,手头有点宽裕,我和我先生决定换一套大一点的房子住。 昨天下午,我们一起去看动车站附近一个小区的...
    灵林玖玖阅读 5,060评论 20 19
  • 天已经快亮了,酒吧的大部分顾客都已离开,酒保也在打盹,氛围瞬间诡异 音乐播放着陈奕迅的一首《约定》: 还记得当天旅...
    西门社长阅读 1,612评论 0 0
  • 不要问自己这世界需要什么,应该问什么能让自己感到生命力,然后就朝那个方向前进,因为这世界正是需要有生命力的人
    琴音_李艳琴阅读 1,625评论 0 1

友情链接更多精彩内容