根据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 = ,
kPictureTag = , } 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 * , 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(, , 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(, , Width / .f, .f)];
self.redView.backgroundColor = [UIColor redColor];
[self.titlesContentView addSubview:self.redView]; self.computingValuesArray = [NSMutableArray array];
self.titles = @[@"Google", @"DeepMind", @"AlphaGo"];
self.pictures = @[@"", @"", @""];
for (int i = ; i < self.titles.count; i++) { // Setup pictures.
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(, , 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 / .f, , Width / .f, self.titlesContentView.height)];
titleView.title = self.titles[i];
titleView.tag = kTitleViewTag + i;
[titleView buildSubViews];
[self.titlesContentView addSubview:titleView]; // Init values.
if (i == ) { titleView.inputValue = .f;
imageView.alpha = .f; } else { titleView.inputValue = .f;
imageView.alpha = .f;
} // Setup ScrollComputingValues.
ScrollComputingValue *value = [ScrollComputingValue new];
value.startValue = -Width + i * Width;
value.midValue = + 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 / .f; for (int i = ; 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

细节

根据UIScrollView的contentOffset值精确控制动画的更多相关文章

  1. 【WPF学习笔记】之如何设置下拉框读取SqlServer数据库的值:动画系列之(一)

    先前条件:设置好数据库,需要三个文件CommandInfo.cs.DbHelperSQL.cs.myHelper.cs,需要修改命名空间,参照之前随笔http://www.cnblogs.com/Ow ...

  2. UIScrollView 原理详解

    转载此文章原因:web页面在ipad的app中总是有橡皮筋效果,使用iscroll虽然能解决橡皮筋想过,但是滚动层内的元素事件都无法触发.故同安卓和ios一样使用后台解决...红色的为解决方案.. S ...

  3. iOS - UIScrollView

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIScrollView : UIView <NSCoding> @available(iOS 2.0, ...

  4. iOS开发UIScrollView的底层实现

    起始 做开发也有一段时间了,经历了第一次完成项目的激动,也经历了天天调用系统的API的枯燥,于是就有了探索底层实现的想法. 关于scrollView的思考 在iOS开发中我们会大量用到scrollVi ...

  5. 第二、UIScrollView的使用大全

    UIScrollView UIPageControl 的使用 2011-11-19 16:48 4690人阅读 评论(0) 收藏 举报 imagescrollspringiphone // //    ...

  6. [转]IOS 学习笔记(8) 滚动视图(UIScrollView)的使用方法

    下面介绍pageControl结合ScrollView实现连续滑动翻页的效果,ScrollView我们在应用开发中经常用到,以g这种翻页效果还是很好看的,如下图所示: 通过这个例子,我们重点学习UIS ...

  7. iOS基础 - UIScrollView

    一.UIScrollView使用引导思路. 1.之前我们所学的显示图片是用UIImageView 2.将UIImageView添加到根视图中,不显示的原因:没有设置位置 3.当图片比屏幕大时,直接放在 ...

  8. 【iOS系列】-UIScrollView的介绍及结合UIPageControl实现图片播放的实例

    [iOS系列]-UIScrollView的介绍及结合UIPageControl实现图片播放的实例 第一:UIScrollView的常用属性 //表示UIScrollView内容的尺寸,滚动范围 @pr ...

  9. 学习笔记之-------UIScrollView 基本用法 代理使用

    //contentSize.contentInset和contentOffset 是 scrollView三个基本的属性. // 滚动 self.ScrollView.contentSize =sel ...

随机推荐

  1. 大数据统计分析平台之一、Kafka单机搭建

    1.zookeeper搭建 Kafka集群依赖zookeeper,需要提前搭建好zookeeper 单机模式(7步)(集群模式进阶请移步:http://blog.51cto.com/nileader/ ...

  2. oracle中 connect by prior 递归查询

    Oracle中start with...connect by prior子句用法 connect by 是结构化查询中用到的,其基本语法是: select ... from tablename sta ...

  3. IntelliJ IDEA快捷键:Esc

    the Esc key in any tool window moves the focus to the editor. 任何工具窗口都会将焦点移动到编辑器.

  4. 【LOJ】#2513. 「BJOI2018」治疗之雨

    题解 具体就是列一个未知数方程\(dp[i]\)表示有\(i\)滴血的时候期望多少轮 \(dp[i] = 1 + \sum_{j = 1}^{i + 1} a_{i,j}dp[j]\) \(dp[n] ...

  5. php 结合redis实现高并发下的抢购、秒杀功能

    抢购.秒杀是如今很常见的一个应用场景,主要需要解决的问题有两个:1 高并发对数据库产生的压力2 竞争状态下如何解决库存的正确减少("超卖"问题)对于第一个问题,已经很容易想到用缓存 ...

  6. [Java/Python] java调用python脚本问题记录

    Java调用Python的的两种方式 1.Runtime private static String call_python(String input_argv) { String python_py ...

  7. P1244 青蛙过河

    P1244 青蛙过河NOI2000主要思想:数学归纳法 递推 压位高精度 化归 理解能力和找规律的能力题意再述:1.青蛙从上到下必须连续递增或者下面是石墩 而不能是1 12 33 4而且每时每刻都要满 ...

  8. JSP(Servlet)中从连接池获取连接

    1) 建立连接. 2) 执行SQL. 3) 处理结果. 4) 释放资源. Connection pool:连接池 DataSource: LDAP ( Light directory access p ...

  9. 蓝牙设备探测工具blueranger

    蓝牙设备探测工具blueranger   blueranger是Kali Linux预安装的一款蓝牙探测工具.该工具通过向指定设备发送蓝牙L2CAP协议的ping包,创建连接.由于大部分蓝牙设备对pi ...

  10. BZOJ.2134.[国家集训队]单选错位(概率 递推)

    题目链接 如题目中的公式,我们只要把做对每个题的概率加起来就可以了(乘个1就是期望). 做对第i道题的概率 \[P_i=\frac{1}{max(a_{i-1},a_i)}\] 原式是 \(P_i=\ ...