双击后将图像大小设置为scrollview内容大小

时间:2012-06-13 13:10:55

标签: iphone objective-c ios ios5 uiscrollview

我想在双击图像视图后将imagesize设置为与scrollview的内容大小相同。

我在我的应用程序和ktphotoview对象中使用ktphotobrowser(它是一个uiscrollview对象)我有以下代码双击

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
UITouch *touch = [touches anyObject];

if ([touch view] == self) {
  if ([touch tapCount] == 2) {
     [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(toggleChromeDisplay) object:nil];
     //[self zoomToRect:[self zoomRectForScale:[self maximumZoomScale] withCenter:[touch locationInView:self]] animated:YES];

      //[self setContentSize:imageView_.bounds.size];
      //[self zoomToLocation:[touch locationInView:self]];

       CGPoint pointInView = [touch locationInView:self];

       // Get a zoom scale that's zoomed in slightly, capped at the maximum zoom scale specified by the scroll view
       CGFloat newZoomScale = self.zoomScale * 2.0;
       NSLog(@"zoomscale %f",newZoomScale);
       newZoomScale = MIN(newZoomScale, self.maximumZoomScale);

       // Figure out the rect we want to zoom to, then zoom to it
       CGSize scrollViewSize = self.bounds.size;

       CGFloat w = scrollViewSize.width / newZoomScale;
       CGFloat h = scrollViewSize.height / newZoomScale;
       CGFloat x = pointInView.x - (w / 2.0f);
       CGFloat y = pointInView.y - (h / 2.0f);

      CGRect rectToZoomTo = CGRectMake(x, y, w, h);

       [self zoomToRect:rectToZoomTo animated:YES];


  }
}
}

scrollview的maximumzoomscale设置为2.0

我的图像的原始大小是1280x853,当我双击myimageview时,绑定的模具高度将为640,而scrollview的内容大小的高度将为960(uiscreen.size.height * maximumzoomscale)

但我想要的是图像视图的高度和内容大小的高度应该相同,这样用户就无法滚动到图像外部

我在这个视图中如何以及在哪里进行这些计算和设置,在哪里可以设置它们的高度和宽度?

感谢

1 个答案:

答案 0 :(得分:0)

可能不是最好的解决方案,但我用以下代码块解决了我的问题:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
UITouch *touch = [touches anyObject];

if ([touch view] == self) {
  if ([touch tapCount] == 2) {


     [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(toggleChromeDisplay) object:nil];
      [self zoomToRect:[self zoomRectForScale:[self maximumZoomScale] withCenter:[touch locationInView:self]] animated:YES];
      float abc=(self.contentSize.height-(imageView_.image.size.height*320/imageView_.image.size.width*self.maximumZoomScale))/2 *-1;

      [self setContentInset:UIEdgeInsetsMake(abc, 0, abc, 0)];





  }
 }
}