具有缩放功能的程序化 UIScrollView 的边界和锚定

发布于 2024-10-14 17:32:35 字数 2434 浏览 0 评论 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);

}

谢谢!

So, I've managed to create a UIScrollView with zooming method programmatically, but I'm kind of stuck how to solve an issue I'm having with zooming:

  1. When I zoom in or out the point at where it expands/retracts does not happen where I do the pinch gesture, it happens on a corner.

  2. After zooming in or out, it will leave extra space outside the bounds, and I cannot scroll the image more than half the width & height of the image.

Other than this, I'm so close to getting it working 100%. I've tried playing around with achorpoints, but it looks like the scroll view and image view does not respond to this.

Here is the important stuff in the code listing:

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);

}

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

双马尾 2024-10-21 17:32:35

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

UIScrollView has built-in zooming support. All you need to do is set the minimumZoomScale and maximumZoomScale properties, and return a view to be used for zooming using viewForZoomingInScrollView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文