一 ScrollView的常用属性及介绍:
@property(nonatomic)CGPoint contentOffset;相对于起始位置的偏移量x和y,默认以左上角为原点是(0,0)
// default CGPointZero
@property(nonatomic)CGSize contentSize; 定义滚动视图内容的尺寸大小
// default CGSizeZero
@property(nonatomic)UIEdgeInsets contentInset;设置滚动边距
// default UIEdgeInsetsZero. add additional scroll area around content
@property(nonatomic,assign)id<UIScrollViewDelegate> delegate;代理
// default nil. weak reference
@property(nonatomic,getter=isDirectionalLockEnabled)BOOLdirectionalLockEnabled;指定ScrollView是否只能在一个方向上滚动,默认是NO
// default NO. if YES, try to lock vertical or horizontal scrolling while dragging
@property(nonatomic)BOOL bounces;内容遇到边框是否反弹,默认是YES
// default YES. if YES, bounces past edge of content and back again
@property(nonatomic)BOOLalwaysBounceVertical;垂直方向内容遇到边框是否返回,默认是YES
// default NO. if YES and bounces is YES, even if content is smaller than bounds, allow drag vertically
@property(nonatomic)BOOL alwaysBounceHorizontal;水平方向内容遇到边框是否返回,默认是YES
// default NO. if YES and bounces is YES, even if content is smaller than bounds, allow drag horizontally
@property(nonatomic,getter=isPagingEnabled)BOOL pagingEnabled;是否分页,默认是NO
// default NO. if YES, stop on multiples of view bounds
@property(nonatomic,getter=isScrollEnabled)BOOL scrollEnabled;是否允许滚动,默认是YES
// default YES. turn off any dragging temporarily
@property(nonatomic)BOOLshowsHorizontalScrollIndicator;是否显示水平方向滚动条,默认是YES
// default YES. show indicator while we are tracking. fades out after tracking
@property(nonatomic)BOOLshowsVerticalScrollIndicator;是否显示垂直方向滚动条,默认是YES
// default YES. show indicator while we are tracking. fades out after tracking
@property(nonatomic)UIEdgeInsets scrollIndicatorInsets;滚动条在视图中的位置,若未设置偏移,默认是在视图中央
// default is UIEdgeInsetsZero. adjust indicators inside of insets
@property(nonatomic UIScrollViewIndicatorStyle indicatorStyle;滚动条的样式,默认是UIScrollViewIndicatorStyleDefault
// default is UIScrollViewIndicatorStyleDefault
@property(nonatomic)CGFloat decelerationRateNS_AVAILABLE_IOS(3_0);滚动速率
部分代码演示:
#import "ViewController.h"
@interface ViewController ()<UIScrollViewDelegate>
{
UIScrollView *scv;
UIImageView *imgView;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
scv = [[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
self.view = scv;
scv.delegate = self;
imgView = [[UIImageView alloc]init];
imgView.image = [UIImage imageNamed:@"2_5"];
[scv addSubview:imgView];
// 让视图根据图像自己调整大小
[imgView sizeToFit];
// 定义滚动视图内容的尺寸大小
scv.contentSize = imgView.image.size;
// 设置滚动边距
scv.contentInset = UIEdgeInsetsMake(100, 100, 100, 100);
// 设置偏移位置
scv.contentOffset = CGPointMake(0, 0);
// 隐藏水平滚动标示
// scv.showsHorizontalScrollIndicator = NO;
// 隐藏垂直滚动标示
// scv.showsVerticalScrollIndicator = NO;
scv.directionalLockEnabled = YES;
// NO时滚动时不会超出可见范围
scv.bounces = YES;
scv.pagingEnabled = YES;
// 滚动条在视图中的位置
scv.scrollIndicatorInsets = UIEdgeInsetsMake(0 , 0, 0, 0);
// 设置最大最小缩放比例
scv.minimumZoomScale = 0.5;
scv.maximumZoomScale = 2.0;
}
二 ScrollView的常用代理方法:
// scrollView 已经滑动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@"--------scrollViewDidScroll方法被调用了--------");
NSLog(@"ContentOffset x is %f,y is %f",scv.contentOffset.x,scv.contentOffset.y);
}
// scrollView 将要开始拖动
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
NSLog(@"--------scrollViewWillBeginDragging方法被调用了--------");
}
// scrollView 将要停止拖动
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_AVAILABLE_IOS(5_0)
{
NSLog(@"--------scrollViewWillEndDragging方法被调用了--------");
}
// scrollView 停止拖动
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
NSLog(@"--------scrollViewDidEndDragging方法被调用了--------");
}
// scrollView 将要开始减速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
NSLog(@"--------scrollViewWillBeginDecelerating方法被调用了--------");
}
// scrollView 停止减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@"--------scrollViewDidEndDecelerating方法被调用了--------");
}
// 滚动动画停止时执行,代码改变时触发,也就是setContentOffset改变时
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
NSLog(@"--------scrollViewDidEndScrollingAnimation方法被调用了--------");
}
// 返回一个放大或者缩小的视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imgView;
}
// 开始放大或者缩小
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view NS_AVAILABLE_IOS(3_2)
{
NSLog(@"--------scrollViewWillBeginZooming方法被调用了--------");
}
// 缩放进行中
- (void)scrollViewDidZoom:(UIScrollView *)scrollView NS_AVAILABLE_IOS(3_2)
{
NSLog(@"--------scrollViewDidZoom方法被调用了--------");
}
// 缩放结束时
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
NSLog(@"--------scrollViewDidEndZooming方法被调用了--------");
}
// 是否支持滑动至顶部
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView
{
NSLog(@"--------scrollViewShouldScrollToTop方法被调用了--------");
return YES;
}
// 滑动到顶部时调用该方法
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
{
NSLog(@"--------scrollViewDidScrollToTop方法被调用了--------");
}