开发者

Bounds and anchoring of programmatic UIScrollView with zooming

开发者 https://www.devze.com 2023-02-07 21:31 出处:网络
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:

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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号