首页 > 代码库 > UI:UIScrollView滚动视图
UI:UIScrollView滚动视图
1.初始化
UIScrollView *scrollView = [[UIScrollViewalloc]init];
2.设置尺寸
(1)按照屏幕尺寸
scrollView.frame =self.view.bounds;
(2)自定义尺寸
scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
3.设置背景色
scrollView.backgroundColor = [UIColorredColor];
4.添加到View上
[self.viewaddSubview:scrollView];
5.在UIScrollView上添加image
//1.先初始化,再设置图片和尺寸
UIImageView *imageView = [[UIImageView alloc] init];
UIImage *image = [UIImage imageNamed:@"a11.jpg"];
imageView.image = image;
imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
[scrollView addSubview:imageView];
//2.先得到图片,通过图片去创建UIImageView
UIImage *image = [UIImageimageNamed:@"a9.jpg"];
_imageView = [[UIImageViewalloc]initWithImage:image];
[scrollViewaddSubview:_imageView];
其他属性设置:
//设置滚动区域 如果宽设为0 ,就不能水平滚动
scrollView.contentSize =CGSizeMake(image.size.width, image.size.height);
//设置当前滚动的位置(左上角的坐标)
scrollView.contentOffset =CGPointMake(500,500);
//在四周添加滚动范围
scrollView.contentInset =UIEdgeInsetsMake(0,0,0, 0);
//设置是否显示显示滚动条(水平,垂直)
// scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator =NO;
//设置滚动条样式(三种)
scrollView.indicatorStyle =UIScrollViewIndicatorStyleBlack;
// scrollView.indicatorStyle = UIScrollViewIndicatorStyleDefault;
// scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
//设置是否可以滚动
scrollView.scrollEnabled =YES;
//设置弹性效果
// scrollView.bounces = NO;
//即使图片小于滚动范围,也可以用弹性效果
scrollView.alwaysBounceVertical =YES;
scrollView.alwaysBounceHorizontal =YES;
@interface ViewController () <UIScrollViewDelegate>
{
UIImageView *_imageView;
}
@end
//设置代理为self
scrollView.delegate =self;
scrollView.maximumZoomScale =2;
#pragma mark 先确定要缩放的控件是谁
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
return_imageView;
}
scrollView.minimumZoomScale =0.5;
UI:UIScrollView滚动视图