views:

665

answers:

0

I'm creating an application where I want to let the user move (not pan) an UIImageView around by dragging it on the screen. Additionally, I want the user to be able to zoom the UIImageView in and out.

As such I've been using a custom UIScrollView that forwards single touches to the 'contentView':

@implementation JM_UIScrollView

- (BOOL)touchesShouldBegin:(NSSet *)touches withEvent:(UIEvent *)event inContentView:(UIView *)view
{
    NSSet *allTouches = [event allTouches];
    NSLog(@"Checking for touches: %d", [allTouches count]);

    if ([allTouches count] == 1) {
     return YES;
    }

    return NO; 
}

@end

Along with a custom UIImageView that implements touchesBegan and touchesMoved to determine where to move the UIImageView:

@implementation JM_UIImageView
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSSet *allTouches = [event allTouches];

    if ([allTouches count] == 0)
     return;

    UITouch *firstTouch = [[allTouches allObjects] objectAtIndex: 0];

    CGPoint touchLoc = [firstTouch locationInView: [self superview]];

    touchOffset= CGPointMake(touchLoc.x-self.center.x,touchLoc.y-self.center.y);
    NSLog(@"Currently at: %3.3f x %3.3f", touchLoc.x, touchLoc.y);

}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    NSSet *allTouches = [event allTouches];

    if ([allTouches count] == 0)
     return;

    UITouch *firstTouch = [[allTouches allObjects] objectAtIndex: 0];
    CGPoint touchLoc = [firstTouch locationInView: [self superview]];

    if ([allTouches count] == 1)
    {   
     if ([firstTouch view] == self)
     {

      touchLoc.x -= touchOffset.x;
      touchLoc.y -= touchOffset.y;

      NSLog(@"Moved to: %3.3f x %3.3f", touchLoc.x, touchLoc.y);
      self.center = touchLoc;
     }  
    }   
}

@end

This is then all glued together:

scrollView = [[JM_UIScrollView alloc] initWithFrame: [[UIScreen mainScreen] bounds]];   
scrollView.delegate = self;
scrollView.bouncesZoom = YES;
scrollView.scrollEnabled = NO;
scrollView.backgroundColor = [UIColor redColor];

scrollView.clipsToBounds = YES;     

UIImage *image = [UIImage imageNamed:@"berg.jpg"];
imageView = [[JM_UIImageView alloc] initWithFrame: CGRectMake(0, 0, 140, 230)];
imageView.image = image;
imageView.center = CGPointMake(200,300);
[imageView setUserInteractionEnabled: YES];

[scrollView addSubview: imageView];

scrollView.contentSize = CGSizeMake(140,230);

scrollView.minimumZoomScale = 0.2;
scrollView.maximumZoomScale = 1.1;

[window addSubview: scrollView];
// Override point for customization after application launch
[window makeKeyAndVisible];

The problem:

When I start the application, I can move the UIImageView just fine and fluently. I can zoom-in and still be able to move it around.

However, it seems that whenever I zoom back out to the maximum level I seem then unable to move the UIImageView around anymore. It will jump at times, but by a maximum of 10 pixels. Use of NSLog() shows the touchesBegan/touchesMoved methods on the JM_UIImageView are no longer called.

Does anyone have any idea on what I might be missing here?

EDIT:

Would also accept an answer for wether or not this is the only way of implementing pinch-zooming with the zoombounce animation.