2

I'm trying to scale a view Horizontally but i'm not getting the results i expected.

view.bounds = CGRectMake(view.bounds.origin.x, view.bounds.origin.y , view.bounds.size.width * scaleX, view.bounds.size.height );
view.center = CGPointMake(view.bounds.origin.x, view.center.y);

As you can see at the code above, i want to scale the view just to the right side. This is why i changed the center of the view.

The problem is that the view stills scaling to both sides! I did the same logic to scale another UIView vertically and got the result i expected.

Felipe Conde
  • 2,024
  • 23
  • 26

3 Answers3

2

Just found the problem.

when setting certer, the new point must be relative to frame, not bounds. so, i get the code:

view.center = CGPointMake(-view.frame.origin.x, view.center.y);

Hope it helps somebody..

Thx for help

Felipe Conde
  • 2,024
  • 23
  • 26
1

If the origin of your view's frame is in the top-left (which is the default), increasing the width of your view should scale it to the right only.

This should work:

view.frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, view.frame.size.width * scaleX, view.frame.size.height);

You do not need to adjust the origin/center point to scale only to the right.

Alex Coplan
  • 13,211
  • 19
  • 77
  • 138
  • 1
    Yes, i need to re-center the view. doing what you said, the view grows to both sides. – Felipe Conde Feb 17 '12 at 17:20
  • @FelipeConde really?? I just made a completely new project with a view and a button, hooked the button up to a method with that code in, and it only scaled to the right... I don't understand why it doesn't do that for you... have you checked where the origin of your view is? - If you still can't get it to work I'll upload some sample code somewhere which you can download to test it with – Alex Coplan Feb 17 '12 at 18:01
  • I just pasted your code and got the view growing to both sides. I found a solution, you can see it above. I`ve just answer my own question. Thx for you help ;) – Felipe Conde Feb 17 '12 at 18:06
  • @FelipeConde I know you've found a solution, but you shouldn't need to reset the center. Can you check where your view's origin is? – Alex Coplan Feb 17 '12 at 18:07
  • The view origin getting it thought bounds was 0,0 as it should be. – Felipe Conde Feb 18 '12 at 21:46
0

Try using an affine transform instead:

view.transform = CGAffineTransformMakeScale(scaleX, 1)

And then setting the center.

EmilioPelaez
  • 18,758
  • 6
  • 46
  • 50