UIScrollView使用
(1)拖动UIScrollView到中间,在内部再添加子View
(2)设置UIScrollView内容大小
self.UIScrollView引用.contentSize = self.UIScrollView子View引用.frame.size;
让UIScrollView滚动到指定位置
CGPoint point = self.UIScrollView引用.contentOffset;
point.x = point.x + 偏移量;
point.y = point.y + 偏移量;
self.UIScrollView引用.contentOffset = point; //无动画效果,直接移过去
//point可以用CGPointMake(x, y)创建一个新的
[self.UIScrollView引用 setContentOffset:point animated:YES]; //通过动画方式滚动过去
隐藏UIScrollView滚动条
self.UIScrollView引用.showsHorizontalScrollIndicator = NO; //水平滚动条
self.UIScrollView引用.showsVerticalScrollIndicator = NO; //垂直滚动条
设置UIScrollView内边距(类似android padding)
self.UIScrollView引用.contentInset = UIEdgeInsetsMake(left, top, right, bottom);
通过代理监听UIScrollView滚动事件
(1)self.UIScrollView引用.delegate = self;
(2)@interface ViewController () <UIScrollViewDelegate> //使用代理(类似java中的implements)
(3)在重写UIScrollView几个方法
//开始拖动时触发
- (void)scrollViewWillBeginDragging:(UIScrollView *) scrollView{
//处理开始拖动的代码
}
// 拖动和滚动时触发
- (void)scrollViewDidScroll:(UIScrollView *) scrollView{
//获取当前滚动位置,值为{x, y}
NSString *curOffset = NSStringFromCGPoint(scrollView.contentOffset);
}
//拖动结束时触发
- (void)scrollViewDidEndDragging:(UIScrollView *) scrollView willDecelerate:(BOOL) decelerate{
//处理拖动结束的代码
}
处理UIScrollView缩放事件
(1)返回用于缩放的UIScrollView子控件
- (UIView *) viewForZoomingInScrollView:(UIScrollView *scrollView {
return self.子控件引用;
}
(2)在viewDidLoad初始化时设置最大最小缩放比
-(void)viewDidLoad {
[super viewDidLoad];
self.UIScrollView引用.maximumZoomScale = 3.5;
self.UIScrollView引用.minimumZoomScale = 0.5;
}
(3)其他事件
//开始缩放触发
- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view{
}
//缩放中触发
- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
}
//缩放完成触发
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)viewatScale:(CGFloat)scale{
}
UIScrollView设置分页效果(多张图片滚动时,拖动到一半时自动控制滚到下一页或上一页)
self.UIScrollView引用.pagingEnabled = YES; //内部根据contentSize大小实现分页
iOS:UIScrollView使用
最新推荐文章于 2024-08-11 11:27:03 发布