前些日子一直在研究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循环播放加缩放的更多相关文章

  1. iOS - 视频循环播放

    录制完视频后,我们想在录制视频的预览层上无限循环播放我们的小视频,是不是很炫酷,这时候我们就有三中选择了:1.MPMoviePlayerController2.AVPlayer3.AVAssetRea ...

  2. [ios]scrollView实现移动与缩放

    实现滑动 1.在viewDidLoad中对scrollview的contentSize属性赋值 告诉他滑动范围. 实现缩放 1.在storyboard的scrollview的attribute标签中设 ...

  3. iOS 用Swipe手势和动画实现循环播放图片

    主要想法 添加3个ImageView展示图片,实现图片的无限循环. 使用Swipe手势识别用户向右或向左滑动图片. 使用CATransition给ImageView.layer添加动画,展示图片更换的 ...

  4. UI 06 ScrollView 的手动循环播放 与 自己主动循环播放

    假设想要循环播放的话, scrollView的照片前要加上最后一张图片, 最后要加上第一张图片. - (void)viewDidLoad { [super viewDidLoad]; // Do an ...

  5. iOS audio不支持循环播放

    解决办法:监听播放完成事件(注意点,audio标签不能设置循环播放,去除标签 loop="loop"或者 loop="false",不然不走播放完成事件) $( ...

  6. iOS开发----音频播放、录音、视频播放、拍照、视频录制

    随着移动互联网的发展,如今的手机早已不是打电话.发短信那么简单了,播放音乐.视频.录音.拍照等都是很常用的功能.在iOS中对于多媒体的支持是非常强大的,无论是音视频播放.录制,还是对麦克风.摄像头的操 ...

  7. iOS - AVAudioPlayer 音频播放

    前言 NS_CLASS_AVAILABLE(10_7, 2_2) @interface AVAudioPlayer : NSObject @available(iOS 2.2, *) public c ...

  8. 【iOS系列】-UIWebView加载网页禁止左右滑动

    [iOS系列]-UIWebView加载网页禁止左右滑动 问题: 做项目时候,用UIWebView加载网页的时候,要求是和微信网页中打开的网页的效果一样,也即是只能上下滑动,不能左右滑动,也不能缩放. ...

  9. 用jquery写循环播放div的相关笔记 珍贵的总结 -1

    用jquery写循环播放div line-height应用的元素的 层次? line-heig字ht, 叫行高, 仅仅是指 文/文本, 而不管图片. line-height是容器中 文本行 与 文本行 ...

随机推荐

  1. Delphi Json

    superobject.pas单元对json的解析非常方便, 力荐 下面演示对如下json的解析 { ", "memo": "S.H.E 004" } ...

  2. soj 1015 Jill's Tour Paths 解题报告

    题目描述: 1015. Jill's Tour Paths Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Every ...

  3. 使用Python从Markdown文档中自动生成标题导航

    概述 知识与思路 代码实现 概述 Markdown 很适合于技术写作,因为技术写作并不需要花哨的排版和内容, 只要内容生动而严谨,文笔朴实而优美. 为了编写对读者更友好的文章,有必要生成文章的标题导航 ...

  4. Linux下面配置文件~/.bash_profile

    ~/.的意义是什么? ~ 代表你的/home/用户名目录 假设你的用户名是x,那么~/就是/home/x/ . 是代表此目录本身,但是一般可以不写 所以cd ~/. 和cd ~ 和cd ~/效果是一样 ...

  5. ElasticSearch学习记录

    中文api 什么是集群? 集群(cluster) >由一个或多个节点组织在一起. >由一个唯一的名字标识,默认为"elasticsearch". 节点(node) &g ...

  6. 主机和虚拟机不能ping通问题

    VMware的三种网络模式附上: 在vmw中,默认有3个虚拟交换机 分别是VMnet0(桥接).VMnet1(主机网络)以及VMnet8(NAT) 还可以根据需要添加VMnet2~VMnet7和VMn ...

  7. BeanUtils.copyProperties()方法和PropertyUtils.copyProperties()的区别

    首先两者来源于同一个包: import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.Prop ...

  8. 《编写可维护的JavaScript》——JavaScript编码规范(四)

    注释 单行注释 单行注释以两个斜线开始,以行尾结束 单行注释有三种使用方法: 独占一行的注释,用来解释下一行代码.这行注释前总是有一个空行,且缩进层级和下一行代码保持一致. 在行尾的注释.代码结束到注 ...

  9. GoLang语言

    1 Go语言基础 1.1 语法详解 1.1.1 注释 /* regexp 包为正则表达式实现了一个简单的库. 该库接受的正则表达式语法为: 正则表达式: 串联 { '|' 串联 } 串联: { 闭包 ...

  10. android 时间 格式

    http://blog.csdn.net/yudajun/article/details/7939552