标签:
一、基本知识
1、初始化 UIScrollView
#import "ViewController.h"
#define WIDTH[[UIScreen mainScreen]bounds].size.width
#define HEIGHT[[UIScreen mainScreen]bounds].size.height
@interface ViewController ()<UIScrollViewDelegate>
@end
UIScrollView *scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(10, 20, WIDTH-20, HEIGHT-30)];
2、设置属性
UIScrollView *scroll = [[UIScrollViewalloc]initWithFrame:CGRectMake(10, 20, WIDTH-20, HEIGHT-30)];
scroll.backgroundColor = [UIColor greenColor];
scroll.contentSize = CGSizeMake((WIDTH-20)*10, HEIGHT+30);;//设置滚动范围,如果想要左右滑动,则必须保证此宽大于scroll的frame对应的宽,如果想要上下滑动必须保证此高大于scroll的frame对应的高
//scroll.scrollEnabled = YES;//是否允许滚动
scroll.bounces = YES;//是否有弹簧效果
//scroll.contentOffset =CGPointMake(40, 80);//设置scrollview滚动到某个位置
NSLog(@"%@",NSStringFromCGPoint(scroll.contentOffset));//获取scrollview当前滚动的位置
scroll.pagingEnabled = YES;//是否允许整页滚动,如果想要左右整页滑动,要保证contentsize的宽是scrollview frame宽的整数倍,如果要上下整页滑动,要保证contentsize 的高是frame高的整数倍,当下一页露出范围小于整页的一半时,滚回到当前页,当超出一半时,滚动到下一页
//scroll.showsHorizontalScrollIndicator = NO;//是否显示水平滚动条
//scroll.showsVerticalScrollIndicator = NO;//是否显示垂直滚动条
//scroll.scrollsToTop = YES;//点状态栏时,是否允许scorllView滚动到顶部
//scroll.zooming = YES;//是否允许缩放
scroll.zoomScale = 2;
3、 [self.view addSubview:scroll];
4、 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 60, 30)];
label.backgroundColor = [UIColor redColor];
[scroll addSubview:label];
for (int i=1; i<4; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i]];
// float w = image.size.width;
// float h =image.size.height;
UIImageView *imageview = [[UIImageView alloc]initWithImage:image];
imageview.frame = CGRectMake(30+i*375, 60, 300, 550);
[scroll addSubview:imageview];
}
二、例子
#import "ViewController.h"
@interface ViewController ()<UIScrollViewDelegate>
{
UISegmentedControl *segment;
UIScrollView *scrollView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 30, 300, 400)];
scrollView.backgroundColor = [UIColor whiteColor];
scrollView.contentSize = CGSizeMake(900, 400);
scrollView.delegate = self;
scrollView.pagingEnabled = YES;
scrollView.scrollEnabled = NO;
[self.view addSubview:scrollView];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 400)];
view.backgroundColor = [UIColor grayColor];
[scrollView addSubview:view];
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(300, 0, 300, 400)];
view1.backgroundColor = [UIColor brownColor];
[scrollView addSubview:view1];
UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(600, 0, 300, 400)];
view2.backgroundColor = [UIColor purpleColor];
[scrollView addSubview:view2];
segment = [[UISegmentedControl alloc] initWithItems:@[@"1",@"2",@"3"]];
segment.frame = CGRectMake(10, 450,200, 40);
segment.selectedSegmentIndex = 0;
[segment addTarget:self action:@selector(segmentChange) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:segment];
}
-(void)segmentChange{
CGPoint p = {segment.selectedSegmentIndex*300,0};
[scrollView setContentOffset:p animated:YES];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
// NSLog(@"滑动时调用");
}
-(void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{
//点击状态栏调用(scrolltotop = yes)
// NSLog(@"到顶了");
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//手指放到scrollview上开始滑动时调用
// NSLog(@"调用");
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//停止拖拽
// NSLog(@"--->停止");
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//停止减速--scrollview不动了
// NSLog(@"不动了");
CGPoint p = scrollView.contentOffset;
float w = p.x;
int index = w/scrollView.frame.size.width;
NSLog(@"当前:%d页",index);
segment.selectedSegmentIndex = index;
}
UIScrollView滚动视图
标签:
原文地址:http://www.cnblogs.com/wxzboke/p/4978397.html