1.scrollView的介绍
scrollView是UI中的基础视图,他有着至关重要的作用,也是我们在UI中常用的控件。他的代理有很多我们需要用,这里我们就不再一一介绍了。
2.简单scrollView的使用
(1)scrollView的使用方法非常简单,首先先声明scrollView对象,定义对象的坐标和大小,需要用到代理的时候就声明一下代理scrollView.delegate = self;然后将其加入视图中。
(2)在将需要在scrollView中需要先向的控件声明,定义坐标及大小。然后加入scroll中
(3)假如我们scroll中的控件在一个屏幕中不够显现,这是后句需要扩展scroll。用多少扩展多少
(4)scroll在Masonry(第三方库)使用约束时候需要注意,滚动的方向的两边约束不能对比scroll,否则scrollView滚动不了
3.重用scrollView
(1)当我们放入scrollView中的控件所占内存比较大时,容易造成手机卡死。那么我们就需要减少scrollView中的内存。所以我们想到了重用scrollView。
(2)步骤
【1】重用scroll我们一般需要三个屏幕宽度,当前显示在中间,两边各有一张备用屏幕(先不考虑最左边和最右边)。
【2】使用scrollView中的一个代理:作用是滑动之后会调用这个代理。
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
假设我们的滚动视图为横向滚动,使用:
int page = (scrollView.contentOffset.x)/scrollView.frame.size.width;的到当前的是第几页
然后重用scrollView,将当前页数设为scrollView三页中的最中间的页数
【3】还要考虑最左边和最右边,判断一下是不是在最左边或者是最右边。如果是则
假如是最左边
我们就不要左边的一张图,并且在代理个函数中写当为第一页时,不支持像左边滑动
假如是最右边
我们就不要右边的一张图,且不支持向右滑动
4.代码DEMO
int n = ;
int m = ;
if (self.withimage == self.allarr.count - || self.withimage == ) {
n = ;
}
//如果只有一张图片
if(self.allarr.count == )
{
self.addscroll = [[addTapandImageGToScroll alloc] initWithFrame:CGRectMake(WIDTH, , WIDTH, HEIGHT)];
UIImage * image = self.allarr[self.withimage];
self.addscroll.iamgeV.frame = self.frame;
NSLog(@"%@",NSStringFromCGRect(self.frame));
[self.Scroller addSubview:self.addscroll];
[UIView animateWithDuration: animations:^{
CGSize imagesize = image.size;
double pror = WIDTH/imagesize.width;
self.addscroll.iamgeV.frame = CGRectMake(, (HEIGHT - pror * HEIGHT)/, WIDTH, pror * HEIGHT);
}];
}
for (int i = ; i < n; i ++)
{
self.addscroll = [[addTapandImageGToScroll alloc] initWithFrame:CGRectMake(WIDTH * i, , WIDTH, HEIGHT)];
UIImage * image ;
if (self.withimage == ) {
image = self.allarr[self.withimage + i];
}
else
{
image = self.allarr[self.withimage - + i];
} NSLog(@"++++++++++++++++++++++%d",self.withimage - + i);
self.addscroll.iamgeV.tag = i;
self.addscroll.iamgeV.image = image;
int s;
if (self.withimage == ) {
s = ;
}
else
{
s = ;
} if (i == s && firstShowAnimateimage == ) {
self.addscroll.iamgeV.frame = self.frame;
NSLog(@"%@",NSStringFromCGRect(self.frame));
[self.Scroller addSubview:self.addscroll];
[UIView animateWithDuration: animations:^{
CGSize imagesize = image.size;
double pror = WIDTH/imagesize.width;
self.addscroll.iamgeV.frame = CGRectMake(, (HEIGHT - pror * HEIGHT)/, WIDTH, pror * HEIGHT);
}];
firstShowAnimateimage = firstShowAnimateimage + ;
}
else
{
[self.Scroller addSubview:self.addscroll];
CGSize imagesize = image.size;
double pror = WIDTH/imagesize.width;
self.addscroll.iamgeV.frame = CGRectMake(, (HEIGHT - pror * HEIGHT)/, WIDTH, pror * HEIGHT);
} //设置下面的属性
self.addscroll.userInteractionEnabled = YES;
self.addscroll.iamgeV.userInteractionEnabled = YES;
//设置点击一次事件
self.addscroll.block = ^(addTapandImageGToScroll *add)
{
if (tapnumber == )
{
weakself.viewBack.hidden = NO;
tapnumber = ;
}
else
{
weakself.viewBack.hidden = YES;
tapnumber = ;
}
};
} self.Scroller.contentSize = CGSizeMake(WIDTH * (n - m), );
NSLog(@"%d",self.index);
if (self.withimage == ) {
self.Scroller.contentOffset = CGPointMake(, );
}
else
{
self.Scroller.contentOffset = CGPointMake(WIDTH * , );
} self.nowimagedex = ;
saveimage = self.allarr[self.withimage];
[self addSome]; - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
//关于手势
tapnumber = ; if (self.allarr.count == ) {
return;
} int page = (scrollView.contentOffset.x)/scrollView.frame.size.width;
NSLog(@"%d",page); if (self.withimage == && page == ) {
return;
} self.nowimagedex = page;
self.labeltitle.text = self.allname[ page];
[self.labeltext setDefaultStyleBy:self.alltext[page]];
CGFloat height = [self.labeltext heightForStringByWidth:WIDTH];
self.scrollText.contentSize = CGSizeMake(, height);
if (height+>HEIGHT*0.5){
self.viewBack.frame = CGRectMake(, HEIGHT*0.5, WIDTH, HEIGHT*0.5);
}else
{
self.viewBack.frame = CGRectMake(, HEIGHT-height-, WIDTH, height+);
}
saveimage = self.allarr[page]; if (page == )
{
self.withimage = self.withimage - ;
[self showImageWithScroll];
}
if (page == )
{
self.withimage = self.withimage + ;
[self showImageWithScroll];
}
if (page == && self.withimage == ) {
self.withimage = self.withimage + ;
[self showImageWithScroll]; }
}