ios开发中经常需要让图片上下左右移动,这时候就需要用到UIScrollView这个对象,使用非常简单,案例如下。
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//创建scrollview
UIScrollView *scroll = [[UIScrollView alloc] init];
scroll.frame = self.view.bounds;
scroll.backgroundColor = [UIColor orangeColor];
//把scrollview添加到屏幕
[self.view addSubview:scroll];
//创建图片框子控件
UIImage *image = [UIImage imageNamed:@"timg2.jpg"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[scroll addSubview:imageView];
//告诉scrollview图片大小有多大,这样图片才会有滚动效果
//scroll.contentSize = imageView.image.size;
//如果只想水平滚动,则可以告诉srcollview图片的高度为0,上下滚动则宽度为0
scroll.contentSize = CGSizeMake(imageView.image.size.width, 0);
//设置滚动条样式,如白色
scroll.indicatorStyle = UIScrollViewIndicatorStyleWhite;
//是否显示水平滚动条
scroll.showsHorizontalScrollIndicator = NO;
//是否显示垂直滚动条
scroll.showsVerticalScrollIndicator = NO;
}
@end