根据UIScrollView的contentOffset值精确控制动画

简介:

根据UIScrollView的contentOffset值精确控制动画

 

效果

 

原理

根据方程精确计算每一个view在位移值x变化的时候的输出值,并根据这个值实现各种逻辑。

 

源码

https://github.com/YouXianMing/Animations

//
//  ScrollViewAnimationController.m
//  Animations
//
//  Created by YouXianMing on 16/3/13.
//  Copyright © 2016年 YouXianMing. All rights reserved.
//

#import "ScrollViewAnimationController.h"
#import "UIView+SetRect.h"
#import "ScrollComputingValue.h"
#import "ScrollTitleView.h"

typedef enum : NSUInteger {
    
    kTitleViewTag = 1000,
    kPictureTag   = 2000,
    
} EScrollViewAnimationControllerValue;

@interface ScrollViewAnimationController () <UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView         *scrollView;
@property (nonatomic, strong) UIView               *redView;
@property (nonatomic, strong) UIView               *titlesContentView;

@property (nonatomic, strong) NSMutableArray       *computingValuesArray;
@property (nonatomic, strong) NSArray              *titles;
@property (nonatomic, strong) NSArray              *pictures;

@end

@implementation ScrollViewAnimationController

- (void)setup {

    [super setup];
    
    self.scrollView                                = [[UIScrollView alloc] initWithFrame:self.contentView.bounds];
    self.scrollView.contentSize                    = CGSizeMake(self.contentView.width * 3, self.contentView.height);
    self.scrollView.pagingEnabled                  = YES;
    self.scrollView.bounces                        = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate                       = self;
    [self.contentView addSubview:self.scrollView];
    
    self.titlesContentView                        = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Width, TabbarHeight)];
    self.titlesContentView.backgroundColor        = [[UIColor blackColor] colorWithAlphaComponent:0.65f];
    self.titlesContentView.userInteractionEnabled = NO;
    self.titlesContentView.bottom                 = self.contentView.height;
    [self.contentView addSubview:self.titlesContentView];
    
    self.redView                 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Width / 3.f, 1.f)];
    self.redView.backgroundColor = [UIColor redColor];
    [self.titlesContentView addSubview:self.redView];
    
    self.computingValuesArray = [NSMutableArray array];
    self.titles               = @[@"Google", @"DeepMind", @"AlphaGo"];
    self.pictures             = @[@"1", @"2", @"3"];
    for (int i = 0; i < self.titles.count; i++) {
        
        // Setup pictures.
        UIImageView *imageView        = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.width, self.contentView.height)];
        imageView.image               = [UIImage imageNamed:_pictures[i]];
        imageView.contentMode         = UIViewContentModeScaleAspectFill;
        imageView.tag                 = kPictureTag + i;
        imageView.layer.masksToBounds = YES;
        [self.contentView addSubview:imageView];
        
        // Setup TitleViews.
        ScrollTitleView *titleView = [[ScrollTitleView alloc] initWithFrame:CGRectMake(i * Width / 3.f, 0, Width / 3.f, self.titlesContentView.height)];
        titleView.title            = self.titles[i];
        titleView.tag              = kTitleViewTag + i;
        [titleView buildSubViews];
        [self.titlesContentView addSubview:titleView];
        
        // Init values.
        if (i == 0) {
            
            titleView.inputValue = 1.f;
            imageView.alpha      = 1.f;
            
        } else {
        
            titleView.inputValue = 0.f;
            imageView.alpha      = 0.f;
        }
        
        // Setup ScrollComputingValues.
        ScrollComputingValue *value = [ScrollComputingValue new];
        value.startValue            = -Width + i * Width;
        value.midValue              = 0      + i * Width;
        value.endValue              = +Width + i * Width;
        [value makeTheSetupEffective];
        [self.computingValuesArray addObject:value];
    }
    
    [self.contentView bringSubviewToFront:self.titlesContentView];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    CGFloat offsetX = scrollView.contentOffset.x;
    _redView.x      = offsetX / 3.f;
    
    for (int i = 0; i < _titles.count; i++) {
        
        ScrollTitleView      *titleView = [_titlesContentView viewWithTag:kTitleViewTag + i];
        UIImageView          *imageView = [self.contentView viewWithTag:kPictureTag + i];
        ScrollComputingValue *value     = _computingValuesArray[i];
        
        value.offsetValue    = offsetX;
        titleView.inputValue = value.outputValue;
        imageView.alpha      = value.outputValue;
    }
}

@end

细节

 

目录
相关文章
|
3月前
|
前端开发
css动画 —— 自定义不规则的动画路径 offset-path (含不规则的动画路径参数获取方法)
css动画 —— 自定义不规则的动画路径 offset-path (含不规则的动画路径参数获取方法)
87 1
|
5月前
|
JavaScript Serverless
Vue 封装一个函数,小球原始高度不固定,弹起比例不固定、计算谈几次后,高度低于1米
Vue 封装一个函数,小球原始高度不固定,弹起比例不固定、计算谈几次后,高度低于1米
20 1
labview布尔型数据开关控制指示灯比较数值颜色变化条件判断
labview布尔型数据开关控制指示灯比较数值颜色变化条件判断
643 0
|
XML 数据格式
【opencv3】滑动条调节RGB颜色阈值并输出滑动条的值
【opencv3】滑动条调节RGB颜色阈值并输出滑动条的值
|
XML 图形学 数据格式
Animation组合动画踩坑-实现循环播放动画,可控制次数
Animation组合动画踩坑-实现循环播放动画,可控制次数
|
JavaScript 开发者
动画-小球动画 flag 标识符的作用分析|学习笔记
快速学习动画-小球动画 flag 标识符的作用分析
160 0
动画-小球动画 flag 标识符的作用分析|学习笔记
|
JavaScript 开发者
动画-小球动画flag标识符的作用分析|学习笔记
快速学习动画-小球动画flag标识符的作用分析
108 0
动画-小球动画flag标识符的作用分析|学习笔记
|
JavaScript