UIScrollView 常用知识点


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


#import "ZoomViewController.h"


@interface ZoomViewController ()<UIScrollViewDelegate>


@end


@implementation ZoomViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

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

    aScrollView.backgroundColor  =[UIColor orangeColor];

    //设置最小缩放比例

    aScrollView.minimumZoomScale = 0.5;

    //设置最大缩放比例

    aScrollView.maximumZoomScale = 3;

    //设置代理的对象

    aScrollView.delegate = self;

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

    CGFloat width = 280;

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

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

    //中心点与scrollView对其

    imageView.center = aScrollView.center;

    imageView.image  = image;

    imageView.tag = 100;

    //添加在scrollView上显示

    [aScrollView addSubview:imageView];


    [self.view addSubview: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);

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值