开发者

Scaling image anchored at cetain point

Is it possible to scale an image by using a specifie开发者_运维知识库d point as the anchor, i.e. the image "grows" out from this point?


Scaling isn't based on a point. What you want to do is move it so that the the corresponding point on the new and original images is at the same point. To do this, just adjust the (x,y) position of the image. Use the proportional distance to the edge multiplied by the difference in size.


You could do something like this (based on solution using UIPinchGesureRecognize, but you can get the idea...). This is the selector called for the gestureRecognizer:

CGPoint newDistanceFromCenter;
CGPoint distanceFromCenter;

- (void) scale:(id)sender
{
    UIPinchGestureRecognizer *recognizer = (UIPinchGestureRecognizer*)sender;

    if(recognizer.state == UIGestureRecognizerStateBegan)
    {
        CGPoint pinchPoint = [recognizer locationInView:self];
        distanceFromCenter.x = self.center.x - pinchPoint.x;
        distanceFromCenter.y = self.center.y - pinchPoint.y;
    }
    else if(recognizer.state == UIGestureRecognizerStateChanged) 
    {
        CGAffineTransform currentTransform = self.transform;
        CGFloat scale = recognizer.scale;

        newDistanceFromCenter.x = (distanceFromCenter.x * scale);
        newDistanceFromCenter.y = (distanceFromCenter.y * scale);

        CGPoint center = scalingImage_.center;
        center.x -= (distanceFromCenter.x - newDistanceFromCenter.x);
        center.y -= (distanceFromCenter.y - newDistanceFromCenter.y);
        self.center = center;

        distanceFromCenter = newDistanceFromCenter;

        self.transform = CGAffineTransformScale(currentTransform, scale, scale);

        recognizer.scale = 1;
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜