1、基本知識orm
一、初始化 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)];
二、設置屬性
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;
三、 [self.view addSubview:scroll];
四、 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];
}
2、例子
#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;
}