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.
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
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.
Try using an affine transform instead:
view.transform = CGAffineTransformMakeScale(scaleX, 1)
And then setting the center.
Related
I am building a custom UIView that you can rotate and resize. I can resize the UIView by dragging the corners of the UIView. I calculate how much I have dragged then change the frame of the UIView accordingly.
However, I am running into problems once I added a rotation gesture recognizer to the view. If I rotate or apply a transform to the view, I no longer know how to calculate drag distance and change the frame of the view. How could I calculate the width and height change between my new view and the original view when things are put at an added angle or if they have some other transform, like a translation transform?
I thought of possibilities to set the view's transform back to .identity, change the size of the view, then re-apply its transform, but I'm not sure how to actually go about implementing this.
After applying transform you can not use frame
You have two options
1) First Calculate everything using center of your view
2) As you know apply identity and change frame
for point 2 I have added example that might helpful to you
let transform = imageView.transform
imageView.transform = CGAffineTransform.identity
var rect: CGRect = imageView.frame
rect = // Change Rect here
imageView.frame = rect // Assign it
imageView.transform = transform // Apply Transform
I want to fix a point (left top point) and scale (zoom out) the UIView according to the point
like this image
Now I use self.transform = CGAffineTransformMakeScale( 0.7 , 0.7); to scale the UIView , But it only can scale the UIView according to the center point
Edit
I try to use set anchorPoint with (0,0) but the view position be wrong
Use anchorPoint property:
self.layer.anchorPoint = CGPointMake(0.0, 0.0);
Try to set anchorPoint of layer
container!.layer.anchorPoint = CGPoint(x: 0, y: 0)
I have a unique requirement to set the coordinate origin of a UIView to be the center of the view. To clarify, the origin needs to be centered vertically and horizontally so that moving to the right is a positive X value, moving left is negative. For Y, moving above the center mark is positive, below is negative. This is essentially the same as geographic coordinates, using the prime meridian's intersection with the equator as the origin.
I am not even sure where to start with this. Can anyone offer up a hint? Thanks, V
Set the origin of the views bounds property to be the middle of the view, something like:
CGRect bounds = view.frame;
bounds.origin.x = bounds.size.width / -2.;
bounds.origin.y = bounds.size.height / -2.;
view.bounds = bounds;
You need functions to transform normal coordinates to strange and vice versa.
For example method in your UIView subclass
- (CGPoint)normalToStrange:(CGPoint)normal
{
return CGPointMake(normal.x - self.width / 2, -normal.y + self.height / 2);
}
I'm developing an animated column bar chart and i having some trouble.
The bars show up with an scale animation. At the beginning all columns have 1 point size and will scale till a predefined height.
The point is: I want to draw a gradient into the columns (They are subclasses of UIView) and want the scale animation to recalculate the gradient.
Until now, i have drawn the gradient, but when the column scale, the gradient does not scale with it.
As the view have 1 point height, the gradient is show as 1 color only.
Can someone give me a north to solve this issue?
Solved the problem!
i was using scale instead of changing the view's bounds..
Was
view.transform = CGAffineTransformMakeScale(1.0, scaleY.floatValue);
Must be
view.bounds = CGRectMake(view.bounds.origin.x, view.bounds.origin.y * scaleY.floatValue, view.bounds.size.width, view.bounds.size.height * scaleY.floatValue);
Where scaleY was calculated before and is a NSNumber that contains the Y scale factor.
It's not necessary to set
view.contentMode = UIViewContentModeRedraw;
Thank you DarkDusk for the help.
Try setting the contentMode:
myView.contentMode = UIViewContentModeRedraw;
This will force a redraw on every size change.
I have a round image that I want to "squish" vertically so that it looks more like a horizontal line, then expand it back to the original shape. I thought this would work by setting the layer's anchor point to the center and then animating the frame via UIViewAnimation with the height of the frame = 1.
[self.imageToSquish.layer setAnchorPoint:CGPointMake(0.5, 0.5)];
CGRect newFrame = CGRectMake(self.imageToSquish.frame.origin.x, self.imageToSquish.frame.origin.y, self.imageToSquish.frame.size.width, 1 );
[UIView animateWithDuration:3
animations:^{self.imageToSquish.frame = newFrame;}
completion:nil];
But the image shrinks toward the top instead of around the center.
You’re giving it a frame that has its origin—at the top left—in the same position as it started. You probably want to do something more like this, adding half the image’s height:
CGRect newFrame = CGRectMake(self.imageToSquish.frame.origin.x, self.imageToSquish.frame.origin.y + self.imageToSquish.frame.size.height / 2, self.imageToSquish.frame.size.width, 1);
Alternatively—and more efficiently—you could set the image view’s transform property to, say, CGAffineTransformMakeScale(1, 0.01) instead of messing with its frame. That’ll be centered on the middle of the image, and you can easily undo it by setting the transform to CGAffineTransformIdentity.