iOS scrollview循环播放加缩放
前些日子一直在研究3d的框架没有时间写博客,不过最后需求改了,也没研究出个啥。这段时间出了新的需求,需要循环播放图片,并且滑动的时候中间的图片有缩放的效果。刚开始想在网上搜索,不过并没有找到合适的demo,没办法只能写个了。
首先说下思路,做这个效果需要解决三个问题。
第一个问题,如何控制每次滑动的距离。iOS中好像并没有设置scrollview每次滑动的距离吧。设置其画框的大小和pageenable的时候已经决定了其每次滑动的距离。但是需求要显示三张图片啊,中间大图,两边的图片只显示一部分。也是想了个恶心的办法,将画框的大小设置成图片的宽度➕一个图片之间的距离(这个距离可根据实际情况调整),将masktobounds设置为no,即可。不过这样做有一个缺点,左右两边的图片是不能响应滑动事件的,以后有时间再解决这个问题吧。
第二个问题,循环播放,这个估计都会,不解释了,可以看代码。
第三个问题,也是难点,如何在滑动的时候改变图片的大小。我们可以根据图片在scrollview上的位置得到其centerx,然后根据其与contentoffset.x+width/2.0(图片的宽度一半)+gap/2.0(图片间距的一半)之间的差作为x,构建一次线性方程式,其实很简单,看看代码就懂了。最后设置transform。
可能说的不清楚,直接上代码吧。
//图片的个数
#define ARRAYCOUNT 3
//缩放的比例
#define SCALE 0.369565
#import "ALNewKeeperScrollView.h"
#import "ALNewKeeperModelView.h" @interface ALNewKeeperScrollView ()<UIScrollViewDelegate>
{
float rate;
float gap;
float whRate;
float width ;
float height;
BOOL ifFirstScroll;
}
@property (nonatomic, strong) UIScrollView *scrollView;
@end @implementation ALNewKeeperScrollView - (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame])
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setClipsToBoundsYes) name:@"setClipsToBoundsYes" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setClipsToBoundsNo) name:@"setClipsToBoundsNo" object:nil];
rate = /SCREEN_WIDTH;
gap = (*)/rate;
whRate = (float)/;
height = /rate;
width = height/whRate; self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(, , width+gap, self.height)];
// self.scrollView.backgroundColor = [UIColor cyanColor];
self.scrollView.centerX = self.width*0.5;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.scrollsToTop = NO;
self.scrollView.delegate = self;
self.scrollView.clipsToBounds = NO;
[self addSubview:self.scrollView]; ifFirstScroll = YES;
}
return self;
}
- (void)setClipsToBoundsYes
{
self.scrollView.clipsToBounds = YES;
}
- (void)setClipsToBoundsNo
{
self.scrollView.clipsToBounds = NO;
}
- (void)config:(NSArray *)array andOffSet:(NSInteger)index
{
int arrayCount = ARRAYCOUNT;
int count = arrayCount+;
for (int i=; i<count; i++)
{
//3 4 0 1 2 3 4 0 1
ALNewKeeperModelView *view = [[ALNewKeeperModelView alloc] initWithFrame:CGRectMake(gap/2.0 + i*(width+gap), /rate, width, height)];
view.centerY = self.height * 0.5;
if (i<)
{
[view config:[NSString stringWithFormat:@"%i", i+]];
}
else if (i<count-)
{
[view config:[NSString stringWithFormat:@"%i", i-]];
}
else
{
[view config:[NSString stringWithFormat:@"%i", i-(count-)]];
}
[self.scrollView addSubview:view];
}
self.scrollView.pagingEnabled = YES;
// self.scrollView.bounces = NO;
[self.scrollView setContentSize:CGSizeMake(count*(gap+width)+gap, )];
float offsetx = (index+)*(width+gap);
[self.scrollView setContentOffset:CGPointMake(offsetx, )];
} - (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat startX = width+gap;
CGFloat endX = (ARRAYCOUNT + )*(width+gap);
CGFloat offsetX = scrollView.contentOffset.x;
if ((NSInteger)offsetX <= (NSInteger)startX )
{
[scrollView setContentOffset:CGPointMake(endX-startX, )];
}
if ((NSInteger)offsetX >= (NSInteger)endX)
{
[scrollView setContentOffset:CGPointMake(startX+startX, )];
}
//滑动的时候缩放图片大小
float contentOffsetX = scrollView.contentOffset.x;
float centerX = contentOffsetX+gap/2.0+width/2.0;
for (id view in [scrollView subviews])
{
if ([view isKindOfClass:[ALNewKeeperModelView class]])
{
ALNewKeeperModelView *modelView = (ALNewKeeperModelView *)view;
float x = modelView.centerX-centerX;
float scale = 1.0;
if (x >= && x <= width)
{
scale += -SCALE/width*x+SCALE;
}
else if (x < && x > - width)
{
scale += SCALE/width*x+SCALE;
}
NSLog(@"%f", scale);
[self setShadow:modelView andScale:scale];
modelView.transform = CGAffineTransformMakeScale(scale, scale);
}
}
} - (void)setShadow:(UIView *)view andScale:(float)scale
{
view.layer.shadowColor = [UIColor blackColor].CGColor;
view.layer.shadowOffset = CGSizeMake(, );
view.layer.shadowRadius = ;
view.layer.shadowOpacity = (float)(scale-1.0)*;
} @end
上述是主要的类,还有几个类就不用写了。可以将该类中没有的类自定义一个view进行替换。
iOS scrollview循环播放加缩放的更多相关文章
- iOS - 视频循环播放
录制完视频后,我们想在录制视频的预览层上无限循环播放我们的小视频,是不是很炫酷,这时候我们就有三中选择了:1.MPMoviePlayerController2.AVPlayer3.AVAssetRea ...
- [ios]scrollView实现移动与缩放
实现滑动 1.在viewDidLoad中对scrollview的contentSize属性赋值 告诉他滑动范围. 实现缩放 1.在storyboard的scrollview的attribute标签中设 ...
- iOS 用Swipe手势和动画实现循环播放图片
主要想法 添加3个ImageView展示图片,实现图片的无限循环. 使用Swipe手势识别用户向右或向左滑动图片. 使用CATransition给ImageView.layer添加动画,展示图片更换的 ...
- UI 06 ScrollView 的手动循环播放 与 自己主动循环播放
假设想要循环播放的话, scrollView的照片前要加上最后一张图片, 最后要加上第一张图片. - (void)viewDidLoad { [super viewDidLoad]; // Do an ...
- iOS audio不支持循环播放
解决办法:监听播放完成事件(注意点,audio标签不能设置循环播放,去除标签 loop="loop"或者 loop="false",不然不走播放完成事件) $( ...
- iOS开发----音频播放、录音、视频播放、拍照、视频录制
随着移动互联网的发展,如今的手机早已不是打电话.发短信那么简单了,播放音乐.视频.录音.拍照等都是很常用的功能.在iOS中对于多媒体的支持是非常强大的,无论是音视频播放.录制,还是对麦克风.摄像头的操 ...
- iOS - AVAudioPlayer 音频播放
前言 NS_CLASS_AVAILABLE(10_7, 2_2) @interface AVAudioPlayer : NSObject @available(iOS 2.2, *) public c ...
- 【iOS系列】-UIWebView加载网页禁止左右滑动
[iOS系列]-UIWebView加载网页禁止左右滑动 问题: 做项目时候,用UIWebView加载网页的时候,要求是和微信网页中打开的网页的效果一样,也即是只能上下滑动,不能左右滑动,也不能缩放. ...
- 用jquery写循环播放div的相关笔记 珍贵的总结 -1
用jquery写循环播放div line-height应用的元素的 层次? line-heig字ht, 叫行高, 仅仅是指 文/文本, 而不管图片. line-height是容器中 文本行 与 文本行 ...
随机推荐
- 【转】CentOS系统中常用查看日志命令
来源:http://www.centoscn.com/CentOS/help/2014/0310/2540.html Linux IDE RedHat 防火墙活动 .cat tail -f 日 志 文 ...
- web应用安全防御100技 好书再次阅读, 变的只是表象,被概念迷惑的时候还是静下心来回顾本质
如何进行web应用安全防御,是每个web安全从业者都会被问到的问题,非常不好回答,容易过于肤浅或流于理论,要阐明清楚,答案就是一本书的长度.而本文要介绍一本能很好回答这个问题的优秀书籍——<we ...
- SQL SERVER 数据库备份的三种策略及语句
1.全量数据备份 备份整个数据库,恢复时恢复所有.优点是简单,缺点是数据量太大,非常耗时 全数据库备份因为容易实施,被许多系统优先采用.在一天或一周中预定的时间进行全数据库备份使你不用动什么脑筋 ...
- django关系对象映射(Object Relational Mapping,简称ORM)
Model 创建数据库,设计表结构和字段 django中遵循 Code Frist 的原则,即:根据代码中定义的类来自动生成数据库表 from django.db import models clas ...
- CSS ::before 和 ::after 伪元素另类用法
原文地址:http://justcoding.iteye.com/blog/2032627 CSS 有两个说不上常用的伪类 :before 和 :after,偶尔会被人用来添加些自定义格式什么的,但是 ...
- Intellij IDEA 配置最简单的maven-struts2环境的web项目
在idea里搭建maven项目 看着网上大神发的各种博客,然后自己搭建出来一个最简单的maven-strtus2项目,供初学者学习 新建project
- 2017年1月3日 星期二 --出埃及记 Exodus 21:29
2017年1月3日 星期二 --出埃及记 Exodus 21:29 If, however, the bull has had the habit of goring and the owner ha ...
- C++ 中注意,零扩展和符号位扩展
版权声明:本文为博主原创文章,未经博主允许不得转载. 首先,介绍一下两种扩展的定义 转 http://blog.csdn.net/jaylong35/article/details/6160736 符 ...
- FastReport使用DataSet作数据源
1.打开FastReport的设计器, 2.选择[File]->[New] 新建FastReport模板. 3.选择[View]->[Data],显示如下,导出Dictionary,保存. ...
- 6.如何使用官方提供的nuget包实现cookie登陆
"Microsoft.AspNetCore.Authentication.Cookies": "1.0.0", 这里需要用到的是这个nuget包 public ...