UIScrollView 常用知识点

来源:互联网 发布:骑士数据 编辑:程序博客网 时间:2024/05/21 11:27


这是一个scrollView的工程,里面包含了一些常用的知识点,都是很基础的这是,适合初学者


#import "ZoomViewController.h"


@interface ZoomViewController ()<UIScrollViewDelegate>


@end


@implementation ZoomViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    UIScrollView *aScrollView = [[UIScrollViewalloc]initWithFrame:self.view.bounds];

    aScrollView.backgroundColor  =[UIColororangeColor];

    //设置最小缩放比例

    aScrollView.minimumZoomScale = 0.5;

    //设置最大缩放比例

    aScrollView.maximumZoomScale = 3;

    //设置代理的对象

    aScrollView.delegate = self;

    UIImage *image =  [UIImageimageNamed:@"image"];

    CGFloat width = 280;

    CGFloat height = width * image.size.height / image.size.width;

    UIImageView *imageView = [[UIImageViewalloc]initWithFrame:CGRectMake(0,0, width, height)];

    //中心点与scrollView对其

    imageView.center = aScrollView.center;

    imageView.image  = image;

    imageView.tag = 100;

    //添加在scrollView上显示

    [aScrollView addSubview:imageView];


    [self.viewaddSubview:aScrollView];

    [aScrollView release];

}





- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView

{

    //通过协议方法让代理对象为scrollView指定要被缩放的视图对象(该视图已经是scrollView上已经存在的子视图)

    return [scrollView viewWithTag:100];

}

    //scrollView缩放指定视图时,scrollView会跟随缩放来实时的修改contentSize的大小,为了保证被缩放的视图一直处于中心位置,可以通过实现协议,以如下xy轴增量补偿的的形式修改.

- (void)scrollViewDidZoom:(UIScrollView *)scrollView

{

    UIView *imageView = [scrollView viewWithTag:100];

//    imageView.center = scrollView.center;

    CGFloat content_w = scrollView.contentSize.width;

    CGFloat content_h = scrollView.contentSize.height;

    CGFloat width  = scrollView.bounds.size.width;

    CGFloat height = scrollView.bounds.size.height;

    

    CGFloat deleat_x = width > content_w ? (width - content_w) /2 : 0;

    CGFloat deleat_y  = height > content_h ? (height - content_h)  /2 : 0;

    imageView.center = CGPointMake(content_w/2 +deleat_x, content_h/2 +deleat_y);

    NSLog(@"%f %f",content_w,content_h);

}



0 0
原创粉丝点击