iosobjective-cuitableviewcgaffinetransformscale

iOS CGAffineTransformScale keep left anchor


I am using CGAffineTransformScale to scale down my UITableView, it is scaling it down correctly but i can't seem to set the anchor point correctly.

self.myTableView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.8, 0.8);

If I don't set the anchor point it scales it down from the center but as my requirement I need to keep the left top corner point fixed. For this I've used this

self.myTableView.layer.anchorPoint = CGPointMake(0, 0);
self.myTableView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.8, 0.8);

But this doesn't set the anchor correctly.. One more thing I haven't used AutoLayout

Please can anyone help me out on this…? thanks in advance..


Solution

  • Try this

    [self setAnchorPoint:CGPointMake(0, 0.5) forView:recognizer.view]; // for left anchor point
    

    and here setAnchorPoint

    -(void)setAnchorPoint:(CGPoint)anchorPoint forView:(UIView *)view
    {
    CGPoint newPoint = CGPointMake(view.bounds.size.width * anchorPoint.x,
                                   view.bounds.size.height * anchorPoint.y);
    CGPoint oldPoint = CGPointMake(view.bounds.size.width * view.layer.anchorPoint.x,
                                   view.bounds.size.height * view.layer.anchorPoint.y);
    
    newPoint = CGPointApplyAffineTransform(newPoint, view.transform);
    oldPoint = CGPointApplyAffineTransform(oldPoint, view.transform);
    
    CGPoint position = view.layer.position;
    
    position.x -= oldPoint.x;
    position.x += newPoint.x;
    
    position.y -= oldPoint.y;
    position.y += newPoint.y;
    
    view.layer.position = position;
    view.layer.anchorPoint = anchorPoint;
    }