前些日子一直在研究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. js 简体中文拼音对应表

    https://github.com/silaLi/pinyin js 拼音对象,包涵大部分文字

  2. ruby调试/练习时的小技巧

    必备工具 irb 查祖先 1.9.3-p545 :023 > String.ancestors => [String, Comparable, Object, Kernel, BasicO ...

  3. windows下jenkins常见问题填坑

    没有什么高深的东西,1 2天的时间大多数人都能自己摸索出来,这里将自己遇到过的问题分享出来避免其他同学再一次挖坑. 目录 1. 主从节点 2. Nuget自动包还原 3. powershell部署 4 ...

  4. var ball0=new Ball("executing") 是怎样被执行的?

    function Ball(message){ alert(message); }; var ball0=new Ball("executing"); //var ball0=ne ...

  5. Nginx:轻量级高性能的Web服务器

    Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由Igor Sysoev为俄罗斯访问量第二的R ...

  6. gerrit session expired 怎么解决,搜索未果

    gerrit session expired,怎么解决,搜索未果

  7. poj 1626

    传送门:http://poj.org/problem?id=1636 题意:有两个监狱,每个监狱有n个人,有m种关系,表示A监狱第i个人不能跟B监狱第j个人在一个监狱,问你最多能换几组人(从A,B监狱 ...

  8. Handler 、 Looper 、Message

    分析: Looper:prepare和loop public static final void prepare() { if (sThreadLocal.get() != null) { throw ...

  9. [TCPIP] DNS Note

    TCPIP DNS  域名系统 DNS 是一个应用于TCP/IP应用程序的分布式数据库,它提供主机名字和IP地址之间的转换及有关电子邮件的选路信息. 对DNS的访问是通过一个地址解析器来完成的,在Un ...

  10. libevent源码分析:listener

    listener是libevent封装的一个方便生成监听者的一组结构和函数,其中包括: /* * Copyright (c) 2000-2007 Niels Provos <provos@cit ...