【问题标题】:Bounds and anchoring of programmatic UIScrollView with zooming编程 UIScrollView 的边界和锚定与缩放
【发布时间】:2011-01-28 22:26:18
【问题描述】:

所以,我设法以编程方式创建了一个带有缩放方法的 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);

}

谢谢!

【问题讨论】:

    标签: iphone xcode ipad gesture zooming


    【解决方案1】:

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

    【讨论】:

    • 即使我没有使用界面生成器?
    • 好的,我有:mapScrollView.minimumZoomScale = .5; mapScrollView.maximumZoomScale = 1.5;我有 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{ return mapImageView; } ...什么也没发生?
    • @user586006 甚至被称为viewForZoomingInScrollView?设置断点或在其中放置NSLog()。如果没有被调用,你可能忘记设置滚动视图的委托了。
    • 我添加了 [mapScrollView setDelegate:(id)self];它有效。我真的很爱你。谢谢!!!!!!!!!!!!!!!!!!!
    猜你喜欢
    • 2019-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    • 2016-05-19
    • 1970-01-01
    • 2011-02-14
    • 2010-09-15
    相关资源
    最近更新 更多