0

所以,我设法以编程方式创建了一个带有缩放方法的 UIScrollView,但我有点卡住了如何解决我在缩放时遇到的问题:

  1. 当我放大或缩小它展开/缩回的点时,它不会发生在我做捏合手势的地方,它发生在角落。

  2. 放大或缩小后,它会在边界之外留下额外的空间,并且我不能滚动图像超过图像宽度和高度的一半。

除此之外,我非常接近让它 100% 工作。我尝试过使用锚点,但看起来滚动视图和图像视图对此没有响应。

以下是代码清单中的重要内容:

UIScrollView *mapScrollView;

UIImageView *mapImageView;

CGFloat lastScale = 0;

NSMutableArray *map_List;


- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {

  mainMenuAppDelegate *del = (mainMenuAppDelegate *)[[UIApplication sharedApplication] delegate];

  map_List = [[NSMutableArray alloc] init];
  [map_List addObject:@"Pacific_Map_8bit.png"];
  [map_List addObject:@"Atlantic_Map_8bit.png"];


  CGRect mapScrollViewFrame = CGRectMake(0, 0, 1024, 768);

  mapScrollView = [[UIScrollView alloc]   initWithFrame:mapScrollViewFrame];

  mapScrollView.contentSize = CGSizeMake(2437, 1536);


  UIImage *mapImage = [UIImage imageNamed:[map_List objectAtIndex:mapNum]];

  mapImageView = [[UIImageView alloc] initWithImage: mapImage];

  mapScrollView.bounces = NO;

  [mapImage release];

  [mapScrollView addSubview:mapImageView];
  [self addSubview:mapScrollView];

  mapImageView.userInteractionEnabled = YES;


  UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
  [mapImageView addGestureRecognizer:pinchRecognizer];

  [pinchRecognizer release];
    }
    return self;

} 
// the scale method thats triggered to zoom when pinching
-(void)scale:(id)sender {

 if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) {

  lastScale = 1.0;
  return;
 }

 CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);

 NSLog(@"map scale %f", scale);

 CGFloat mapWidth = mapImageView.frame.size.width;
 CGFloat mapHeight = mapImageView.frame.size.height;
 NSLog(@"map width %f", mapWidth);

 if(scale>=1 & mapWidth<=4000 || scale<1 & mapWidth>=1234){

  CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
  CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
  [[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];

  lastScale = [(UIPinchGestureRecognizer*)sender scale];

 }

 mapScrollView.contentSize = CGSizeMake(mapWidth, mapHeight);

}

谢谢!

4

1 回答 1

2

UIScrollView具有内置的缩放支持。您需要做的就是设置minimumZoomScalemaximumZoomScale属性,并返回一个用于缩放的视图viewForZoomingInScrollView

于 2011-01-28T23:18:49.857 回答