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是容器中 文本行 与 文本行 ...
随机推荐
- MATLAB的GUI
% 常使用的对象查看和设置函数 % .get.set函数 ) % 获得句柄值为0的对象的属性,即显示器对象属性 plot([:]); % 绘制一幅图 title('示例'); % 增加text对象 % ...
- 踢爆IT劣书出版黑幕——由清华大学出版社之《C语言入门很简单》想到的(1)
1.前言与作者 首先声明,我是由于非常偶然的机会获得<C语言入门很简单>这本书的,绝对不是买的.买这种书实在丢不起那人. 去年这书刚出版时,在CU论坛举行试读推广,我当时随口说了几句(没说 ...
- ios webView 放大网页解决/input 获得焦点focus 网页放大 解决
新手遇到的问题: 终于找到原因,各种HTML viewport 都试过 setScalePageToFit 也试过,webViewDidFinishLoad加JS代码,动态算webView.scrol ...
- yield 用法分析
yield 关键字向编译器指示它所在的方法是迭代器块.编译器生成一个类来实现迭代器块中表示的行为.在迭代器块中,yield 关键字与 return 关键字结合使用,向枚举器对象提供值.这是一个返回值, ...
- MessageReceiver
class MessageReceiver { private RelayEngine<MessageCollection> _MessageRelayEngine; private st ...
- ftp相关资料
一.ftp状态码 110 重新启动标记应答.在这种情况下文本是确定的,它必须是:MARK yyyy=mmmm,其中yyyy是用户进程数据流标记,mmmm是服务器标记. 120 ...
- HTML5 History 模式
vue-router 默认 hash 模式 -- 使用 URL 的 hash 来模拟一个完整的 URL,于是当 URL 改变时,页面不会重新加载. 如果不想要很丑的 hash,我们可以用路由的 his ...
- html(二)
1无序列表 ul是没有前后顺序的信息列表. <ul> <li></li> <li></li> ...... </ul> ul在网 ...
- 模板——RMQ
就是模板 #include <cstdio> #include <cstring> #include <iostream> using namespace std; ...
- android Json Gson FastJson 解析
一 Json xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...