【问题标题】:Set image size to scrollview's content size after a double tap双击后将图像大小设置为滚动视图内容大小
【发布时间】:2012-06-13 13:10:54
【问题描述】:

双击图像视图后,我想将图像大小设置为与滚动视图的内容大小相同。

我在我的应用程序和它的 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 的最大缩放比例设置为 2.0

当我双击 myimageview 时,我的图像的原始尺寸为 1280x853,绑定模具高度为 640,滚动视图的内容尺寸高度为 960(uiscreen.size.height * maximumzoomscale)

但我想要的是图像视图的高度和内容大小的高度应该相同,因此用户不能滚动到图像之外

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

谢谢

【问题讨论】:

    标签: iphone objective-c ios ios5 uiscrollview


    【解决方案1】:

    也许不是最好的解决方案,但我用下面的代码块解决了我的问题:

    - (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)];
    
    
    
    
    
      }
     }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多