【UIScrollView】
//内容区域
@property(nonatomic) CGSize contentSize;
//偏移
@property(nonatomic) CGPoint contentOffset;
//弹簧效果
@property(nonatomic) BOOL bounces;
//分页效果
@property(nonatomic,getter=isPagingEnabled) BOOL pagingEnabled;
//允许滑动视图本身,如果设为no只能通过函数滑动,不能直接用手滑动
@property(nonatomic,getter=isScrollEnabled) BOOL scrollEnabled;
//显示水平进度条
@property(nonatomic) BOOL showsHorizontalScrollIndicator;
//显示垂直进度条
@property(nonatomic) BOOL showsVerticalScrollIndicator;
//放大和缩小的倍数(需要实现缩放的代理方法)
@property(nonatomic) CGFloat minimumZoomScale;
@property(nonatomic) CGFloat maximumZoomScale;
//设置偏移
- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;
【UIScrollViewDelegate】
//代理方法
//滑动过程中
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
//开始拖动
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;
//停止拖动
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
//开始滑动
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView;
//停止滑动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
//允许缩放的视图(一个scrollview中只能有一个可以缩放且必须设置可以缩放的位数)
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;
【UIPageControl】
//小点点
@property(nonatomic) NSInteger numberOfPages;
@property(nonatomic) NSInteger currentPage;
//小点点的颜色
@property(nonatomic,retain) UIColor *pageIndicatorTintColor;
@property(nonatomic,retain) UIColor *currentPageIndicatorTintColor;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//滚动视图
UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(30, 70, 240, 300)];
sv.backgroundColor = [UIColor redColor];
sv.tag = 1;
[self.view addSubview:sv];
//设置可以展示的大小(尺寸)
sv.contentSize = CGSizeMake(480, 600);
//设置弹簧效果
sv.bounces = YES;
//翻页效果
sv.pagingEnabled = NO;
//水平和垂直方向进度条的展示状态
sv.showsHorizontalScrollIndicator = YES;
sv.showsVerticalScrollIndicator = NO;
//不允许用户拿手指头滑动
// sv.scrollEnabled = NO;
UIView *blueView = [[UIView alloc] initWithFrame:CGRectMake(260, 320, 50, 50)];
blueView.backgroundColor = [UIColor blueColor];
[sv addSubview:blueView];
UIView *yellowView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 230, 290)];
yellowView.backgroundColor = [UIColor yellowColor];
[sv addSubview:yellowView];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UIScrollView *sv = (UIScrollView *)[self.view viewWithTag:1];
//将偏移转成字符串,并打印
NSLog(@"%@",NSStringFromCGPoint(sv.contentOffset));
//直接设置偏移
// sv.contentOffset = CGPointMake(240, 300);
//带动画设置偏移
[sv setContentOffset:CGPointMake(100, 100) animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor grayColor];
UIScrollView *sv = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 40, 300, 300)];
sv.backgroundColor = [UIColor yellowColor];
[self.view addSubview:sv];
UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"king2"]];
iv.tag = 1;
[sv addSubview:iv];
// sv.contentSize = CGSizeMake(1200, 300);
sv.delegate = self;
sv.pagingEnabled = YES;
//设置sv里子视图可以缩放的倍数
sv.maximumZoomScale = 3;
sv.minimumZoomScale = 0.5;
//页码控制器
UIPageControl *pc = [[UIPageControl alloc] initWithFrame:CGRectMake(100, 440, 120, 10)];
pc.tag = 100;
//总页数
pc.numberOfPages = 4;
[self.view addSubview:pc];
//设置小点点的颜色
pc.pageIndicatorTintColor = [UIColor redColor];
pc.currentPageIndicatorTintColor = [UIColor greenColor];
//不接收用户触摸事件
pc.userInteractionEnabled = NO;
}
#pragma mark - scrollViewDelegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{//设置sv中允许被缩放的子视图
return [scrollView viewWithTag:1];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// NSLog(@"1=%@",NSStringFromCGPoint(scrollView.contentOffset));
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
NSLog(@"2=%@",NSStringFromCGPoint(scrollView.contentOffset));
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
NSLog(@"3=%@%d",NSStringFromCGPoint(scrollView.contentOffset),decelerate);
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
NSLog(@"4=%@",NSStringFromCGPoint(scrollView.contentOffset));
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
int page = scrollView.contentOffset.x/300;
NSLog(@"===========%d",page);
UIPageControl *pc = (UIPageControl *)[self.view viewWithTag:100];
//设置页码控制的当前页(从0开始的)
pc.currentPage = page;
}