//设置代理
_scrollView.delegate = self;
UIScrollView基本属性测试##
- (void)testBaseAttributes
{
//设置能否滚动,默认为YES
//_scrollView.scrollEnabled = NO;
//设置边界回弹效果
//_scrollView.bounces = NO;
/*滚动指示条风格
UIScrollViewIndicatorStyleDefault 灰白
UIScrollViewIndicatorStyleBlack 灰色
UIScrollViewIndicatorStyleWhite 白色
*/
_scrollView.indicatorStyle = UIScrollViewIndicatorStyleDefault;
//是否显示水平滚动指示条
_scrollView.showsHorizontalScrollIndicator = NO;
//是否显示垂直滚动指示条
_scrollView.showsVerticalScrollIndicator = NO;
//无论内容尺寸多大,都可一直滚动,测试时bounces必须为YES
//水平方向一直可以滚动
_scrollView.alwaysBounceHorizontal = YES;
//垂直方向一直可以滚动
_scrollView.alwaysBounceVertical = YES;
/*设置松手后减速的加速度
UIScrollViewDecelerationRateNormal 正常
UIScrollViewDecelerationRateFast 快速
*/
//_scrollView.decelerationRate = UIScrollViewDecelerationRateFast;
//点击状态栏自动滚动到顶部,默认为YES
//_scrollView.scrollsToTop = NO;
//设置内容偏移量,非常重要
//无动画版本
_scrollView.contentOffset = CGPointMake(300, 300);
//延时5s,调用动画设置偏移量到(0,0)
[self performSelector:@selector(setContentOffsetToZero) withObject:nil afterDelay:5];
//设置是否按页滚动,默认为NO
_scrollView.pagingEnabled = YES;
//缩放相关,必须再指定缩放的视图才能缩放,设置代理方法
_scrollView.minimumZoomScale = 0.5;
_scrollView.maximumZoomScale = 2.0;
//添加双击缩放手势
UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(zoomHandle:)];
tgr.numberOfTapsRequired = 2;
[_scrollView addGestureRecognizer:tgr];
[tgr release];
}
//UIScrollview缩放属性
//双击缩放触发的方法
- (void)zoomHandle:(UITapGestureRecognizer *)tgr
{
CGFloat maximumScale = _scrollView.maximumZoomScale;
CGFloat currentScale = _scrollView.zoomScale;
if (currentScale != 1.0) {
[_scrollView setZoomScale:1.0 animated:YES];
} else {
[_scrollView setZoomScale:maximumScale animated:YES];
}
}
UIScrollView代理相关方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//只要有滚动就会一直调用,不能做耗时操作
//NSLog(@"滚动...");
}
//使用代码方式,动画版本的修改偏移量,结束时回调
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
NSLog(@"代码方式,动画滚动结束");
}
//指定缩放的视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return scrollView.subviews[0];
}
页码指示器基本属性
- (void)setPageControl
{
_pageControl = [[UIPageControl alloc] init];
CGSize size = self.view.frame.size;
_pageControl.frame = CGRectMake(0, size.height-50, size.width, 50);
//_pageControl.backgroundColor = [UIColor grayColor];
//设置页码指示小圆点的颜色
_pageControl.pageIndicatorTintColor = [UIColor greenColor];
//设置当前页码指示小圆点的颜色
_pageControl.currentPageIndicatorTintColor = [UIColor blueColor];
//设置总页数
_pageControl.numberOfPages = _scrollView.contentSize.width/_scrollView.frame.size.width;
//添加事件
[_pageControl addTarget:self action:@selector(changeHandle:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:_pageControl];
}
隐藏状态栏
#pragma mark - 状态栏相关
- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleLightContent;
}
- (BOOL)prefersStatusBarHidden
{
return _isHiddenStatusBar;
}
- (void)tapGestureHandle
{
_isHiddenStatusBar = !_isHiddenStatusBar;
//必须要刷新状态栏才会改变
[self setNeedsStatusBarAppearanceUpdate];
}
UIScrollview图片复用
说明:三个UIImageView,中间一个UIimageView加在scrollView用于缩放操作,复用的关键在于UIImageView的tag值和image的tag值之间的关联
关键代码如下:
//滑动结束后操作
- (void)autoRefresh
{
char flag = 0;
CGSize size = _scrollView.frame.size;
if (_scrollView.contentOffset.x == 0) {
//向右滑
flag = -1;
} else if (_scrollView.contentOffset.x == 2*size.width) {
//向左滑
flag = 1;
} else {
return;
}
for (UIImageView *view in _imageViews) {
//重新设置imageView的tag值非常重要
view.tag = (view.tag+flag+_imgaeNames.count)%_imgaeNames.count;
[self setImageToImageView:view];
}
_scrollView.contentOffset = CGPointMake(size.width, 0);
//找到中间视图的父视图(小的滚动视图)
UIScrollView *sv = (UIScrollView *)[_imageViews[1] superview];
//恢复其缩放比例1.0
sv.zoomScale = 1.0;
[self refreshTitle];
}