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

时间:2011-01-28 22:26:18

标签: iphone xcode ipad gesture zooming

所以,我已经成功地以编程方式创建了一个带有缩放方法的UIScrollView,但是我很难解决如何解决缩放问题:

  1. 当我放大或缩小它展开/缩回的位置时,我不会发生捏合手势,它发生在拐角处。

  2. 放大或缩小后,会在边界外留出额外的空间,而且我无法将图像滚动超过宽度的一半。图像的高度。

  3. 除此之外,我非常接近100%的工作。我试过玩achorpoints,但看起来滚动视图和图像视图没有响应。

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

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

    谢谢!

1 个答案:

答案 0 :(得分:2)

UIScrollView具有内置的缩放支持功能。您只需设置minimumZoomScalemaximumZoomScale属性,然后使用viewForZoomingInScrollView返回用于缩放的视图。