欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

scrollview放大imageview从点击的地方开始放大

程序员文章站 2022-07-09 09:31:12
...

-(void)tapAction:(UITapGestureRecognizer *)tap{

    

    CGFloat offsetX = (self.myScrollView.bounds.size.width > self.myScrollView.contentSize.width)?(self.myScrollView.bounds.size.width - self.myScrollView.contentSize.width)/2 : 0.0;

    CGFloat offsetY = (self.myScrollView.bounds.size.height > self.myScrollView.contentSize.height)?(self.myScrollView.bounds.size.height - self.myScrollView.contentSize.height)/2 : 0.0;

    

    CGPoint touchPoint = [tap locationInView:self.myScrollView];

 

if ((self.myScrollView.zoomScale >= metroScale) && (self.myScrollView.zoomScale < metroScale*2)) {

        ///x轴的逻辑是正确的,但是y轴不是从0开始的,,上边有导航栏呢。。。。。

[self.myScrollView zoomToRect:CGRectMake(((touchPoint.x-offsetX)/metroScale), ((touchPoint.y-offsetY)/metroScale)-kNavigationBarHeight-StateBarHeight, metroScale*2, metroScale*2) animated:YES];

        [self.myScrollViewsetZoomScale:metroScale*2animated:YES];

 

} elseif((self.myScrollView.zoomScale >= metroScale*2)&&(self.myScrollView.zoomScale < metroScale*3)){

        ///x轴的逻辑是正确的,但是y轴不是从0开始的,,上边有导航栏呢。。。。。

[self.myScrollView zoomToRect:CGRectMake(((touchPoint.x-offsetX)/(metroScale*2)), ((touchPoint.y-offsetY)/(metroScale*2))-kNavigationBarHeight-StateBarHeight, metroScale*4, metroScale*4) animated:YES];

        [self.myScrollViewsetZoomScale:metroScale*4animated:YES];

}else{

        [self.myScrollViewsetZoomScale:metroScaleanimated:YES];

    }

    

 

}

 

///下面的一个方法,可以实现从点击的地方开始放大,效果不错。。。

-(void)tapAction:(UITapGestureRecognizer *)tap{

-(void)tapAction:(UITapGestureRecognizer *)tap{

    

    CGFloat offsetX = (self.myScrollView.bounds.size.width > self.myScrollView.contentSize.width)?(self.myScrollView.bounds.size.width - self.myScrollView.contentSize.width)/2 : 0.0;

    CGFloat offsetY = (self.myScrollView.bounds.size.height > self.myScrollView.contentSize.height)?(self.myScrollView.bounds.size.height - self.myScrollView.contentSize.height)/2 : 0.0;

    

    CGPoint touchPoint = [tap locationInView:self.myScrollView];

 

if (self.myScrollView.zoomScale != metroScale) {

[self.myScrollViewsetZoomScale:metroScaleanimated:YES];

} else {

        ///x轴的逻辑是正确的,但是y轴不是从0开始的,,上边有导航栏呢。。。。。

[self.myScrollView zoomToRect:CGRectMake(((touchPoint.x-offsetX)/metroScale), ((touchPoint.y-offsetY)/metroScale)-kNavigationBarHeight-StateBarHeight, metroScale*3, metroScale*3) animated:YES];

        [self.myScrollViewsetZoomScale:metroScale*3animated:YES];

}

    

 

}

    

 

}

 

 

 

 

///下面是核心代码

 ///添加touch事件

    UITapGestureRecognizer *myTapGesture = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(tapAction:)];

   

    myTapGesture.numberOfTapsRequired=2;//默认为1.表示点击的次数。

    [self.myScrollView addGestureRecognizer:myTapGesture];

    

}

 

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

 

{

    returnself.myImageV;

}

-(void)tapAction:(UITapGestureRecognizer *)tap{

    

    CGFloat offsetX = (self.myScrollView.bounds.size.width > self.myScrollView.contentSize.width)?(self.myScrollView.bounds.size.width - self.myScrollView.contentSize.width)/2 : 0.0;

    CGFloat offsetY = (self.myScrollView.bounds.size.height > self.myScrollView.contentSize.height)?(self.myScrollView.bounds.size.height - self.myScrollView.contentSize.height)/2 : 0.0;

    

    CGPoint touchPoint = [tap locationInView:self.myScrollView];

    kNSLog(@"双击metroScale == %f,,%f,,%f",metroScale,offsetX,offsetY);

if (self.myScrollView.zoomScale != metroScale) {

[self.myScrollViewsetZoomScale:metroScaleanimated:YES];

} else {

        ///x轴的逻辑是正确的,但是y轴不是从0开始的,,上边有导航栏呢。。。。。

[self.myScrollView zoomToRect:CGRectMake(((touchPoint.x-offsetX)/metroScale), ((touchPoint.y-offsetY)/metroScale)-kNavigationBarHeight-StateBarHeight, 1, 1) animated:YES];

}

    

 

}

 

- (void)scrollViewDidZoom:(UIScrollView *)scrollView

{

    float minzoomx = self.myScrollView.frame.size.width / self.myImageV.size.width;

    float minzoomy = self.myScrollView.frame.size.height / self.myImageV.size.height;

    

    if(scrollView.zoomScale > MIN(minzoomx, minzoomy))

    {

//        [self.myImageV setCenter:CGPointMake((scrollView.frame.size.width / 2)/minzoomx, (scrollView.frame.size.height / 2)/minzoomy)];

        NSLog(@"上面。。。。。。。。。。。。。。。");

    }else{

         NSLog(@"下面。");

        if (minzoomy>minzoomx) {

            [self.myImageV setCenter:CGPointMake((scrollView.frame.size.width / 2)/MIN(minzoomx, minzoomy), (scrollView.frame.size.height / 2))];

        }else{

            [self.myImageV setCenter:CGPointMake((scrollView.frame.size.width / 2), (scrollView.frame.size.height / 2)/MIN(minzoomx, minzoomy))];

        }

        

    }

    

}

 

///上面是核心代码

 

 

 

 

 

 

 

 

 

 

///前面的只是表面上好的。。。在最后才真正是对的。。。。。

self.myScrollView.delegate = self;

    float minzoomx = self.myScrollView.frame.size.width / self.myImageV.size.width;

    float minzoomy = self.myScrollView.frame.size.height / self.myImageV.size.height;

    self.myScrollView.minimumZoomScale = MIN(minzoomx, minzoomy);  //最小缩放到当前ScrollView的大小比例

    self.myScrollView.maximumZoomScale = 3.0f; //最大缩放到图片的3

    ///设置地铁图的初始化位置。。。

 

    self.myScrollView.zoomScale = MIN(minzoomx, minzoomy);

 ///这个也不能少

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

 

{

    returnself.myImageV;

    

}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView

{

    if(scrollView.zoomScale < 1.0)

    {

        UIImageView *imageView = [scrollView.subviews lastObject];

        [self.myImageV setCenter:CGPointMake(scrollView.frame.size.width / 2, scrollView.frame.size.height / 2)];

    }

 

}

 

 

////应该用下面的才行。。。

- (void)scrollViewDidZoom:(UIScrollView *)scrollView

{

    float minzoomx = self.myScrollView.frame.size.width / self.myImageV.size.width;

    float minzoomy = self.myScrollView.frame.size.height / self.myImageV.size.height;

    if(scrollView.zoomScale > 1.0)

    {

        [self.myImageV setCenter:CGPointMake((scrollView.frame.size.width / 2)/minzoomx, (scrollView.frame.size.height / 2)/minzoomy)];

    

    }else{

        [self.myImageV setCenter:CGPointMake((scrollView.frame.size.width / 2), (scrollView.frame.size.height / 2))];

    }

    

    

    

 

}

 

 

////真正行的。。。。。。。

//

//  ShowPDFViewController.m

//  iTotemFramework

//

//  Created by apple on 14-3-5.

//  Copyright (c) 2014 iTotemStudio. All rights reserved.

//

 

#import "ShowPDFViewController.h"

 

#import "UIImage+PDF.h"

 

@interfaceShowPDFViewController ()

 

@end

 

@implementation ShowPDFViewController

 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

        self.myData = [NSMutableData data];

    }

    returnself;

}

 

- (void)viewDidLoad

{

    [superviewDidLoad];

    // Do any additional setup after loading the view from its nib.

    [self.navigationItemsetNewTitle:self.myName];

    [self.navigationItemsetBackItemWithTarget:selfaction:@selector(back) title:nil];

    [self.navigationItemsetRightItemWithTarget:selfaction:@selector(rightAction:) image:@"DT_share.png"];

    

}

///分享的方法。。。。。

-(void)rightAction:(id)sender{

    NSString *tempText = [NSStringstringWithFormat:@"这是一张高清%@地铁图,分享自世界邦 - 全球地铁图APP。你也快来试试吧。应用下载地址:%@",self.myName,kAPPURL];

    [UMSocialSnsServicepresentSnsIconSheetView:self

                                         appKey:kUMappKey

                                      shareText:tempText

                                     shareImage:self.myShareImage

                                shareToSnsNames:[NSArrayarrayWithObjects:UMShareToSina,UMShareToWechatSession,UMShareToWechatTimeline,nil]

                                       delegate:nil];

    

    ///微信,朋友圈分享。。

    //设置微信分享应用类型,用户点击消息将跳转到应用,或者到下载页面

    //UMSocialWXMessageTypeImage 为纯图片类型

    [UMSocialDatadefaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb;

//    [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeImage;

    //分享图文样式到微信朋友圈显示字数比较少,只显示分享标题

    [UMSocialDatadefaultData].extConfig.title = @"朋友圈分享内容";

    //设置微信好友或者朋友圈的分享url,下面是微信好友,微信朋友圈对应wechatTimelineData

    [UMSocialDatadefaultData].extConfig.wechatSessionData.url = @"http://www.shijiebang.com/topic/mobile/railway/";

    [UMSocialDatadefaultData].extConfig.wechatTimelineData.url = @"http://www.shijiebang.com/topic/mobile/railway/";

}

-(void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:animated];

    [MobClickendLogPageView:@"显示地铁图页面"];

}

///页面出来后。。。

-(void)viewDidAppear:(BOOL)animated{

    [super viewDidAppear:animated];

    if ([kUserDefault objectForKey:self.myURL]!=NULL) {

        [self showSubway];

    }

    else{

        if (![ViewControllerFactoryisnetworkOK]) {

            //            [self.view.window showHUDWithText:@"该地铁图还没有下载,请检查联网!" Type:ShowLoading Enabled:YES];

            [ViewControllerFactoryshowMessageAlert:@"该地铁图还没有下载,请检查联网!"];

        }else{

            NSURL *url = [NSURL URLWithString:self.myURL];

            NSURLRequest *request = [NSURLRequest requestWithURL:url];

            //发起请求,定义代理

            [NSURLConnectionconnectionWithRequest:request delegate:self];

            flag = 0;

            ///初始化一个imageView

            self.tempView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 80, 90)];

            self.tempView.backgroundColor = [UIColorclearColor];

            self.tempView.center = CGPointMake(kScreenWidth/2, (kScreenHeight-kNavigationBarHeight-kStateBarHeight)/2);

            

            self.tempImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 0, 60, 55)];

            self.tempLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 55, 80, 30)];

            self.tempLabel.textAlignment = NSTextAlignmentCenter;

            self.tempLabel.font = [UIFont fontWithName:@"Arial" size:13];

            self.tempLabel.backgroundColor = [UIColorclearColor];

            

            [self.tempViewaddSubview:self.tempImageView];

            [self.tempView addSubview:self.tempLabel];

            [self.view addSubview:self.tempView];

            [self.view bringSubviewToFront:self.tempView];

        }

    }

 

}

///页面出来前

-(void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    [MobClickendLogPageView:@"显示地铁图页面"];

}

 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;

    if(httpResponse && [httpResponse respondsToSelector:@selector(allHeaderFields)]){

        NSDictionary *httpResponseHeaderFields = [httpResponse allHeaderFields];

        total_ = [[httpResponseHeaderFields objectForKey:@"Content-Length"] longLongValue];

    }

}

// 分批返回数据

- (void)connection:(NSURLConnection *) connection didReceiveData:(NSData *)data {

 

    [self.myData appendData:data];

    double a = [self.myData length];

    float jindu = a/total_;

    int imageFlag = (int)(jindu*24);

    NSString *imageString = [NSString stringWithFormat:@"%d",imageFlag];

    self.tempLabel.text = [NSString stringWithFormat:@"加载中%d%%",(int)(jindu*100)];

    self.tempImageView.image = kImageNamed(imageString);

}

// 数据完全返回完毕

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    ///下面的两句其实已经给imageView赋值了。。。。。

    UIImage *tempImage = [UIImageoriginalSizeImageWithPDFData:self.myData];

    self.myImageV.image = tempImage;

    [self.tempViewremoveFromSuperview];

    

    tempWidth = tempImage.size.width;

    tempHeight = tempImage.size.height;

    self.myImageV.size = CGSizeMake(tempWidth, tempHeight);///关键是这句话。。。。。

    self.myScrollView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-kNavigationBarHeight-StateBarHeight);

    self.myScrollView.contentSize = CGSizeMake(self.myImageV.size.width, self.myImageV.size.height);

    

    self.myScrollView.delegate = self;

    float minzoomx = self.myScrollView.frame.size.width / tempWidth;

    float minzoomy = self.myScrollView.frame.size.height / tempHeight;

    self.myScrollView.minimumZoomScale = MIN(minzoomx, minzoomy);  //最小缩放到当前ScrollView的大小比例

    self.myScrollView.maximumZoomScale = 3.0f; //最大缩放到图片的3

    

    ///设置地铁图的初始化位置。。。

    self.myScrollView.zoomScale = MIN(minzoomx, minzoomy);

    

    

    [kUserDefaultsetObject:self.myDataforKey:self.myURL];

    [kUserDefaultsynchronize];

    

    self.myShareImage = [ViewControllerFactoryyasuoCameraImage:tempImage];///这是为了分享用的。。。。。。

    

    ///添加touch事件

    UITapGestureRecognizer *myTapGesture = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(tapAction:)];

    self.myImageV.userInteractionEnabled = YES;

    myTapGesture.numberOfTapsRequired=2;//默认为1.表示点击的次数。

    [self.myImageV addGestureRecognizer:myTapGesture];

}

 

-(void)showSubway{

 

    

    UIImage *tempImage = [UIImage originalSizeImageWithPDFData:[kUserDefault objectForKey:self.myURL]];

    self.myImageV.image = tempImage;

    

    tempWidth = tempImage.size.width;

    tempHeight = tempImage.size.height;

    self.myImageV.size = CGSizeMake(tempWidth, tempHeight);///关键是这句话。。。。。

    self.myScrollView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-kNavigationBarHeight-StateBarHeight);

    self.myScrollView.delegate = self;

    float minzoomx = self.myScrollView.frame.size.width / tempWidth;

    float minzoomy = self.myScrollView.frame.size.height / tempHeight;

    self.myScrollView.minimumZoomScale = MIN(minzoomx, minzoomy);  //最小缩放到当前ScrollView的大小比例

    self.myScrollView.maximumZoomScale = 3.0f; //最大缩放到图片的3

    ///设置地铁图的初始化位置。。。

    self.myScrollView.zoomScale = MIN(minzoomx, minzoomy);

    self.myShareImage = [ViewControllerFactoryyasuoCameraImage:tempImage];///这是为了分享用的。。。。。。

    

    

    ///添加touch事件

    UITapGestureRecognizer *myTapGesture = [[UITapGestureRecognizeralloc] initWithTarget:selfaction:@selector(tapAction:)];

    self.myImageV.userInteractionEnabled = YES;

    myTapGesture.numberOfTapsRequired=2;//默认为1.表示点击的次数。

    [self.myImageV addGestureRecognizer:myTapGesture];

    

}

 

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

 

{

    returnself.myImageV;

}

-(void)tapAction:(id)sender{

    NSLog(@"轻拍,,,双击");

    float minzoomx = self.myScrollView.frame.size.width / tempWidth;

    float minzoomy = self.myScrollView.frame.size.height / tempHeight;

    

    CGFloat zs = self.myScrollView.zoomScale;

    zs = (zs == MIN(minzoomx, minzoomy)) ? 1.0 : MIN(minzoomx, minzoomy);

    

    [UIViewbeginAnimations:nilcontext:NULL];

    [UIViewsetAnimationDuration:0.6];

    self.myScrollView.zoomScale = zs;

    [UIViewcommitAnimations];

 

}

 

- (void)scrollViewDidZoom:(UIScrollView *)scrollView

{

    float minzoomx = self.myScrollView.frame.size.width / self.myImageV.size.width;

    float minzoomy = self.myScrollView.frame.size.height / self.myImageV.size.height;

    

    if(scrollView.zoomScale > MIN(minzoomx, minzoomy))

    {

//        [self.myImageV setCenter:CGPointMake((scrollView.frame.size.width / 2)/minzoomx, (scrollView.frame.size.height / 2)/minzoomy)];

        NSLog(@"上面。。。。。。。。。。。。。。。");

    }else{

         NSLog(@"下面。");

        if (minzoomy>minzoomx) {

            [self.myImageV setCenter:CGPointMake((scrollView.frame.size.width / 2)/MIN(minzoomx, minzoomy), (scrollView.frame.size.height / 2))];

        }else{

            [self.myImageV setCenter:CGPointMake((scrollView.frame.size.width / 2), (scrollView.frame.size.height / 2)/MIN(minzoomx, minzoomy))];

        }

        

    }

    

}

 

- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

 

@end

 

 

@end