根据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. IntelliJ Idea中创建package的问题

    在idea中创建package时,会一直往后面累加 想在com.huayang下面再新建一个service,普通的做法是选中了huayang.dao--->然后右键--->新建了一个pac ...

  2. Angular快速学习笔记(3) -- 组件与模板

    1. 显示数据 在 Angular 中最典型的数据显示方式,就是把 HTML 模板中的控件绑定到 Angular 组件的属性. 使用插值表达式显示组件属性 要显示组件的属性,最简单的方式就是通过插值表 ...

  3. Ubuntu 18.04及Snap体验——让Linux入门更简单(转))

    https://www.linuxidc.com/Linux/2018-06/152993.htm 初次听说过Linux的时候,是大一计算机课时候老师介绍说除了Windows还有Linux.Unix操 ...

  4. Node.js包的依赖及版本号(转)

    原文:  http://www.cnphp6.com/archives/64130 Node.js最重要的一个文件就是package.json,其中的配置参数决定了功能.例如下面就是一个例子 { &q ...

  5. 我的Python之旅第五天---迭代器和生成器

    h3,#nv_portal .vw .d .h3 {display: block; font-weight: 500; background-image: linear-gradient(to rig ...

  6. 关于Android4.X的Alertdialog对话框

    最近在做Android4.0的开发,发现AlertDialog相比较以前有了较大变化,就是在触摸对话框边缘外部,对话框消失 于是研究其父类发现,可以设置这么一条属性,当然必须先AlertDialog. ...

  7. zoj 3204 最小生成树,输出字典序最小的解

    注意排序即可 #include<cstdio> #include<iostream> #include<algorithm> #include<cstring ...

  8. 如何让PictureBox背景色透明

    winform程序中的PictureBox载入了一张带有透明度的PNG图片,悬浮于其他控件之上,但是它的背景不是透明的,即使把它的BackColor设置为Color.Transparent,或者是0x ...

  9. Mui --- app与服务器之间的交互原理、mui ajax使用

    1.APP与服务器之间的交互原理 app端(客户端)与服务端的交互其实理解起来和容易,客户端想服务器端发送请求,服务器端进行数据运算后返回最终结果.结果可以是多种格式: 1.text 文本格式 2.x ...

  10. [Go] 第一个单词首字母变大写:Ucfirst(),第一个单词首字母变小写:Lcfirst()

    import ( "unicode" ) func Ucfirst(str string) string { for i, v := range str { return stri ...