Please note, this blog entry is from a previous course. You might want to check out the current one.
The complete code of the mandatory tasks of assignment #3 can be found at github.
Addendum Task #7
The previously shown code for the pinch: method would zoom using the origin of the axis as center point thus moving a graph out of view if the currently shown area is not close to the origin. To provide a proper zoom function where the zoom happens centered to the touching point the origin has to adjusted as well as the scale:
- (CGPoint)pointOfTouch:(CGPoint)touch
{
CGPoint point;
point.x = (touch.x - self.dataSource.origin.x) / self.dataSource.scale;
point.y = (touch.y - self.dataSource.origin.y) / self.dataSource.scale;
return point;
}
- (void)setOriginFor:(CGPoint)point forTouch:(CGPoint)touch
{
CGPoint origin;
origin.x = touch.x - point.x * self.dataSource.scale;
origin.y = touch.y - point.y * self.dataSource.scale;
self.dataSource.origin = origin;
}
- (void)pinch:(UIPinchGestureRecognizer *)gesture
{
if ((gesture.state != UIGestureRecognizerStateChanged) &&
(gesture.state != UIGestureRecognizerStateEnded)) return;
CGPoint touch = [gesture locationInView:self];
CGPoint point = [self pointOfTouch:touch];
self.dataSource.scale *= gesture.scale;
gesture.scale = 1;
[self setOriginFor:point forTouch:touch];
}