//总共图片数量
#define kIMGCOUNT 4
#define VIEW_WIDTH self.view.frame.size.width
#define VIEW_HEIGHT self.view.bounds.size.height
@interface HomeViewController : UIViewController<UIScrollViewDelegate>
{
UIImageView *imageView;
@private
UIScrollView *_scrollView;
}
@implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self initViewController];
}
- (void)initViewController
{
//设置scrollview
_scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 20,VIEW_WIDTH , 261)]; //屏幕宽度
// scrollView.backgroundColor = [UIColor redColor];
[self.view addSubview:_scrollView];
//设置内容大小
_scrollView.contentSize = CGSizeMake(VIEW_WIDTH*kIMGCOUNT,VIEW_HEIGHT*5);
// _scrollView.pagingEnabled = YES; //分页效果
//隐藏滚动指示条
// _scrollView.showsHorizontalScrollIndicator = YES;
// scrollView.showsVerticalScrollIndicator = YES;
//设置图片
for (int i = 0; i<kIMGCOUNT; i++)
{
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(self.view.frame.size.width*i, 0, self.view.frame.size.width, 261)];
//设置图片
NSString *name = [NSString stringWithFormat:@"background%d",i+1];
imageView.tag = i;
imageView.image=[UIImage imageNamed:name];
[_scrollView addSubview:imageView];
}
//设置代理
_scrollView.delegate=self;
//图片缩放的最大最小值设置
// _scrollView.minimumZoomScale = 0.3;
// _scrollView.maximumZoomScale = 3;
// 用来记录scrollview滚动的位置
// scrollView.contentOffset = ;
//是否反弹效果,默认为反弹
// _scrollView.bounces = NO;
//是否滚动,默认可以滚动
// _scrollView.scrollEnabled = NO;
//设置indication滚动条风格 默认/黑/白,三种风格
// _scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
//提示用户,滚动条闪一下
// [_scrollView flashScrollIndicators];
// 增加额外的滚动区域(逆时针,上、左、下、右)
// top left bottom right 设置内容和滚动条的边缘
// _scrollView.contentInset = UIEdgeInsetsMake(20, 20, 20, 20);
// _scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 50, 0);
//滚动条是否可以同时运动
// _scrollView.directionalLockEnabled = YES;
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(VIEW_WIDTH, 210, VIEW_WIDTH, 40)];
label.text = @"学习scrollView";
label.backgroundColor = [UIColor yellowColor];
[_scrollView addSubview:label];
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(VIEW_WIDTH/2-50, 350, 100, 40)];
[btn setTitle:@"滚动" forState:UIControlStateNormal];
btn.backgroundColor = [UIColor purpleColor];
[btn addTarget:self action:@selector(test) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (void)test{
//将内容移动到指定位置
[_scrollView setContentOffset:CGPointMake(VIEW_WIDTH, 0) animated:YES]; //只改变了x,y的值
// [_scrollView scrollRectToVisible:CGRectMake(VIEW_WIDTH, 0, VIEW_WIDTH, 500) animated:YES];
}