下面直接用实例的方式对UIScrollView的用法进行总结:



 



#import"ViewController.h"
 
 
//动态获取屏幕的大小,以适用于不同大小的屏幕 
  
#define WIDTH [[UIScreen mainScreen] bounds].size.width 
  
 #define HEIGHT [[UIScreen mainScreen] bounds].size.height 
  

@interface ViewController () 
  

@end 
  

@implementation ViewController 
  

 - (void)viewDidLoad { 
  
     [super viewDidLoad]; 
  
     
  
     UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 20, WIDTH-20, HEIGHT - 30)]; 
  
     scroll.backgroundColor = [UIColor whiteColor]; 
  
     
  
     scroll.contentSize = CGSizeMake((WIDTH-20)*2, HEIGHT+30);//设置滚动范围,如果想要左右滑动,则必须保证此宽大于scroll的frame对应的宽,如果想要上下滑动必须保证此高大于scroll的frame对应的高 
  
     
  
     scroll.scrollEnabled = NO;//是否允许滚动(默认允许) 
  
     scroll.bounces = NO;      //是否有弹簧效果(默认有) 
  
     
  
      scroll.contentOffset = CGPointMake(20, 40);//设置scrollview滚动到某个位置 
  
     
  
     NSLog(@"%@",NSStringFromCGPoint(scroll.contentOffset));//获取scrollview当前滚动的位置 
  
     
  
       scroll.pagingEnabled = YES;//是否允许整页滚动,如果想要左右整页滑动,要保证contentsize的宽是scrollview frame宽的整数倍,如果要上下整页滑动,要保证contentsize 的高是frame高的整数倍,当下一页露出范围小于整页的一半时,滚回到当前页,当超出一半时,滚动到下一页 
  
     
  
     scroll.showsHorizontalScrollIndicator = NO;//是否显示水平滚动条 
  
       scroll.showsVerticalScrollIndicator = NO;//是否显示垂直滚动条 
  
     
  
     scroll.scrollsToTop = YES;//点状态栏时,是否允许scorllView滚动到顶部(默认允许) 
  
     
  
      scroll.zooming = YES;//是否允许缩放(代理方法) 
  
      scroll.zoomScale = 2;//缩放比例 
  
     
  
     [self.view addSubview:scroll]; 
  
     
  
     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, HEIGHT, 200, 30)]; 
  
     label.backgroundColor = [UIColor redColor]; 
  
     [scroll addSubview:label]; 
  
 } 
  
 - (void)didReceiveMemoryWarning { 
  
     [super didReceiveMemoryWarning]; 
  
     // Dispose of any resources that can be recreated. 
  
 } 
   
 
@end



 



UIScrollView与UISegmentedControl的结合使用



 



UIScrollView 的代理方法:



#import "ViewController.h" 
  

@interface ViewController ()<UIScrollViewDelegate> 
  
 { 
  
     UISegmentedControl *segment; 
  
     UIScrollView *scrollView; 
  
 } 
  
@end 
  

@implementation ViewController 
  

 - (void)viewDidLoad { 
  
     [super viewDidLoad]; 
  

     scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 30, 300, 400)]; 
  
     scrollView.backgroundColor = [UIColor whiteColor]; 
  
     scrollView.contentSize = CGSizeMake(900, 400);//设置滚动的范围 
  
     scrollView.delegate = self; 
  
     scrollView.pagingEnabled = YES;//是否允许整页滑动 
 
    scrollView.scrollEnabled = NO;//是否允许滚动(默认允许)
 
 
    [self.view addSubview:scrollView]; 
  
     
  
     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 400)]; 
  
     view.backgroundColor = [UIColor grayColor]; 
  
     [scrollView addSubview:view]; 
  
     
  
     UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(300, 0, 300, 400)]; 
  
     view1.backgroundColor = [UIColor brownColor]; 
  
     [scrollView addSubview:view1]; 
  

     UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(600, 0, 300, 400)]; 
  
     view2.backgroundColor = [UIColor purpleColor]; 
  
     [scrollView addSubview:view2]; 
  

     
  
     segment = [[UISegmentedControl alloc] initWithItems:@[@"1",@"2",@"3"]]; 
  
     segment.frame = CGRectMake(10, 450,200, 40); 
  
     segment.selectedSegmentIndex = 0; 
  
     [segment addTarget:self action:@selector(segmentChange) forControlEvents:UIControlEventValueChanged]; 
  
     [self.view addSubview:segment]; 
  
     
  
 } 
  
 -(void)segmentChange{ 
  
     CGPoint p = {segment.selectedSegmentIndex*300,0}; 
  
     [scrollView setContentOffset:p animated:YES]; 
 
}
 
 
 
 
 
//滑动过程中调用的方法
 
 
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{ 
  
    // NSLog(@"滑动时调用"); 
 
}
 
 
 
 
 
//点击状态栏调用的方法(前提:scrolltotop = yes)
 
 
-(void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{ 
  
     //点击状态栏调用(scrolltotop = yes) 
  
    // NSLog(@"到顶了"); 
  
 } 
 
  //手指放到scrollview上开始滑动时调用的方法
 
 
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ 
  
     //手指放到scrollview上开始滑动时调用 
  
    // NSLog(@"调用"); 
  
 } 
 
  //停止拖拽后调用的方法
 
 
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ 
  
     //停止拖拽 
  
    // NSLog(@"--->停止"); 
 
}
 
 
 
 
 
 
 
 
//停止减速--scrollview不动了之后调用的方法
 
 
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{ 
  
     //停止减速--scrollview不动了 
  
   //  NSLog(@"不动了"); 
  
     
  
     CGPoint p = scrollView.contentOffset; 
  
     float w = p.x; 
  

     int index = w/scrollView.frame.size.width; 
  
     NSLog(@"当前:%d页",index); 
  
     
  
     segment.selectedSegmentIndex = index; 
  
     
  
 } 
  

 - (void)didReceiveMemoryWarning { 
  
     [super didReceiveMemoryWarning]; 
  
     // Dispose of any resources that can be recreated. 
  
 } 
   
 
@end
 
 
 
 
 
视图的缩放
 
 
 
 
 
#import "ViewController.h" 
  

@interface ViewController ()<UIScrollViewDelegate>{ 
  
     UIScrollView *_scrollView; 
  
     UIImageView  *_imgView; 
  
 } 
  

@end 
  

@implementation ViewController 
  

 - (void)viewDidLoad { 
  
     [super viewDidLoad]; 
  

     _scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds]; 
  
     _scrollView.delegate = self; 
  
     _scrollView.minimumZoomScale = 1;//缩放的最小比例 
  
     _scrollView.maximumZoomScale = 3;//缩放的最大比例 
  
     [self.view addSubview:_scrollView]; 
  
     
  
     _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 400)]; 
  
     _imgView.image = [UIImage imageNamed:@"1.jpg"]; 
  
     [_scrollView addSubview:_imgView]; 
  
 } 
  

//告诉scrollView,是哪个子视图要进行缩放 
  
 -(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{ 
  
     
  
     return _imgView; 
  
 } 
  
//停止缩放调用的方法 
  
 -(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{ 
  
     //停止缩放 
  
     NSLog(@"调用"); 
  
 } 
  

 -(void)scrollViewDidZoom:(UIScrollView *)scrollView{ 
  
     NSLog(@"---"); 
  
 } 
  

 - (void)didReceiveMemoryWarning { 
  
     [super didReceiveMemoryWarning]; 
  
     // Dispose of any resources that can be recreated. 
  
 } 
   
 
@end


 


作者: fly52035


实现UIScrollView的缩放,必须使maximumZoomScale(默认1.0)和minimumZoomScale(默认1.0)不同 ,并且需要在delegate中的viewForZoomingInScrollView: 方法中返回需要所放的view。实现以上即可进行缩放。



要修改缩放过程中的一些行为,可以修该delegate中的scrollViewWillBeginZooming:withView:  和scrollViewDidZoom:(此方法为每次拖动时随时调用)和scrollViewDidEndZooming:withView:atScale:(此方法为每次放缩完毕时调用)的函数内容。



注意:



viewForZoomingInScrollView方法返回的view是scrollview确定contentsize的view。contentsize的大小与该view的frame.size相同;在放缩的同时scrollview会自动设定zoomscale属性的大小,与每次放缩结束的scrollViewDidEndZooming:withView:atScale 中的scale相同。每次放缩过程中,所放缩的view的bound不会改变而frame会改变(这同修改view的transform属性的效果相同)放缩会改变frame,改变view在父视图的位置,而不会改变bound大小。推测放缩也是通过affinetransform进行改变。所以对需要固定位置的view,需要在每次scrollViewDidZoom中修改view的位置,例如如下代码实现固定ImageView始终在整个content的居中位置:



-(void)scrollViewDidZoom:(UIScrollView *)scrollView{
    CGFloat xcenter = scrollView.center.x , ycenter = scrollView.center.y; 
//目前contentsize的width是否大于原scrollview的contentsize,如果大于,设置imageview中心x点为contentsize的一半,以固定imageview在该contentsize中心。如果不大于说明图像的宽还没有超出屏幕范围,可继续让中心x点为屏幕中点,此种情况确保图像在屏幕中心。
    xcenter = scrollView.contentSize.width > scrollView.frame.size.width ? 
                                                scrollView.contentSize.width/2 : xcenter;
//同上,此处修改y值
    ycenter = scrollView.contentSize.height > scrollView.frame.size.height ? 
                                                scrollView.contentSize.height/2 : ycenter;
    [[scrollView viewWithTag:imageview] setCenter:CGPointMake(xcenter, ycenter)];
}

设置scrollview的bouncesZoom属性可以确保view的放缩比例超出设置比例范围时自动进行反弹。


使图片自适应屏幕大小,以确保图片在屏幕正中的方法:

float x_scale = scrollView.frame.size.width/selectedImage.size.width;
    float y_scale = scrollView.frame.size.height/selectedImage.size.height;
    CGFloat scale = x_scale < y_scale ? x_scale : y_scale;
    imageView.transform = CGAffineTransformMakeScale(scale, scale);