iOS scrollview循环播放加缩放

时间:2023-03-09 07:05:43
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进行替换。