UIScrollView的相关方法设置
(1)简单设置ScrollView
scrollview.directionalLockEnabled = YES; //只能一个方向滑动
scrollview.pagingEnabled = NO; //是否翻页
scrollview.showsVerticalScrollIndicator =YES; //垂直方向的滚动指示
scrollview.indicatorStyle = UIScrollViewIndicatorStyleWhite;//滚动指示的风格
scrollview.showsHorizontalScrollIndicator = NO;//水平方向的滚动指示
(2)三种去掉scrollView滚动条的方法:
<1>self.serviceScrollView.showsVerticalScrollIndicator = NO;
<2>self.serviceScrollView.showsHorizontalScrollIndicator = NO;
<3>self.serviceScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
(3)UIScrollViewDelegate方法中,监听滑动方向:
-(void)scrollViewWillEndDragging:(UIScrollView
*)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout
CGPoint *)targetConte ntOffset{ if(velocity.y>0){
[self.navigationController setNavigationBarHidden:YES
animated:YES]; }else{
[self.navigationController setNavigationBarHidden:NO animated:YES];
} }
// 触摸屏幕来滚动画面还是其他的方法使得画面滚动,皆触发该函数
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
NSLog(@"Scrolling...");
}
// 触摸屏幕并拖拽画面,再松开,最后停止时,触发该函数
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
NSLog(@"scrollViewDidEndDragging - End of Scrolling.");
}
// 滚动停止时,触发该函数
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSLog(@"scrollViewDidEndDecelerating - End of Scrolling.");
}
// 调用以下函数,来自动滚动到想要的位置,此过程中设置有动画效果,停止时,触发该函数
// UIScrollView的setContentOffset:animated:
// UIScrollView的scrollRectToVisible:animated:
// UITableView的scrollToRowAtIndexPath:atScrollPosition:animated:
// UITableView的selectRowAtIndexPath:animated:scrollPosition:
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
NSLog(@"scrollViewDidEndScrollingAnimation - End of Scrolling.");
}
(3)scrollView属性总结
属性 作用
CGPoint contentOffSet 监控目前滚动的位置
CGSize contentSize 滚动范围的大小
UIEdgeInsets contentInset 视图在scrollView中的位置
id delegate 设置协议
BOOL directionalLockEnabled 指定控件是否只能在一个方向上滚动
BOOL bounces 控制控件遇到边框是否反弹
BOOL alwaysBounceVertical 控制垂直方向遇到边框是否反弹
BOOL alwaysBounceHorizontal 控制水平方向遇到边框是否反弹
BOOL pagingEnabled 控制控件是否整页翻动
BOOL scrollEnabled 控制控件是否能滚动
BOOL showsHorizontalScrollIndicator 控制是否显示水平方向的滚动条
BOOL showsVerticalScrollIndicator 控制是否显示垂直方向的滚动条
UIEdgeInsets scrollIndicatorInsets 指定滚动条在scrollerView中的位置
UIScrollViewIndicatorStyleindicatorStyle 设定滚动条的样式
float decelerationRate 改变scrollerView的减速点位置
BOOL tracking 监控当前目标是否正在被跟踪
BOOL dragging 监控当前目标是否正在被拖拽
BOOL decelerating 监控当前目标是否正在减速
BOOL delaysContentTouches 控制视图是否延时调用开始滚动的方法
BOOL canCancelContentTouches 控制控件是否接触取消touch的事件
float minimumZoomScale 缩小的最小比例
float maximumZoomScale 放大的最大比例
float zoomScale 设置变化比例
BOOL bouncesZoom 控制缩放的时候是否会反弹
BOOL zooming 判断控件的大小是否正在改变
BOOL zoomBouncing 判断是否正在进行缩放反弹
BOOL scrollsToTop 控制控件滚动到顶部
注意:
1、使scrollview不要上下滑动,可以设置
scrollview.contentSize = CGSizeMake(你要的长度, 0);
(4)
#pragma mark UIScrollViewDelegate
//只要滚动了就会触发
- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
{
// NSLog(@" scrollViewDidScroll");
NSLog(@"ContentOffset x is %f,yis %f",scrollView.contentOffset.x,scrollView.contentOffset.y);
}
//开始拖拽视图
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ }
//完成拖拽- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
{
NSLog(@"scrollViewDidEndDragging");
}
//将开始降速时
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView;
{
NSLog(@"scrollViewWillBeginDecelerating");
}
//减速停止了时执行,手触摸时执行执行
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
{
NSLog(@"scrollViewDidEndDecelerating");
}
//滚动动画停止时执行,代码改变时出发,也就是setContentOffset改变时
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView;
{
NSLog(@"scrollViewDidEndScrollingAnimation");
}
//设置放大缩小的视图,要是uiscrollview的subview
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;
{
NSLog(@"viewForZoomingInScrollView");
return viewA;
}
//完成放大缩小时调用
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale;
{
viewA.frame=CGRectMake(50,0,100,400);
NSLog(@"scale between minimum and maximum. called after any 'bounce' animations");
}// scale between minimum and maximum. called after any 'bounce' animations
//如果你不是完全滚动到滚轴视图的顶部,你可以轻点状态栏,那个可视的滚轴视图会一直滚动到顶部,那是默认行为,你可以通过该方法返回NO来关闭它
- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;
{
NSLog(@"scrollViewShouldScrollToTop");
returnYES;
}
- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView;
{
NSLog(@"scrollViewDidScrollToTop");
}