UIView stretching side instead of adjusting width & origin
This is probably an easy question, whether its possible or not i have no idea; but easy none the less.
Screen = 0x 0y 1000w 1000h
UIView Start = 500x 0y 500w 1000h
开发者_如何转开发UIView End = 0x 0y 1000w 1000h
I have a UIView lets say covering the right side of the screen with the dimensions and coordinates above. Right now i know to get to the end point i have to do
CGRectMake(start.origin.x - 500, start.origin.y, start.size.width + 500, start.size.height)
Is it possible to just do something like
[uiview stretchLeftSide:500];
that way i do not need to touch the origin or width directly. it causes some issues as the view contains controls where i cannot manage there anchoring so they just jump to the specified location, then the view adjusts (this is all animated BTW).
As far as i know relocating & resizing a UIView
this way - CGRectMake(start.origin.x - 500, start.origin.y, start.size.width + 500, start.size.height)
is the way to do it.
It's ugly but that's what I used to do. But you could inherit UIView
and add a method stretchLeftSide
and implement what you want. In longer running projects this makes absolute sense and should be done as a practice...
First, I'm assuming your UIView
(let's just call it view) is a subview of Screen
(i.e. you did something like [Screen addSubview:view]
).
If you want to do the animation so the left side stretches the way you want, basically, all you have to do is set the origin of view
to (0,0)
and the size to be the same as Screen
. For this, you can use Screen.bounds
which is exactly what you want the final frame of view
to be. The bounds
property of a view will always have an origin of (0,0)
and a size equal to the size of the view.
If you have a reference to Screen
, you can use Screen.bounds
as I mentioned above, but if you don't, and you know that view
is a subView of Screen
, then you can use view.superview.bounds
.
Now the animation. Because I have to write apps that support iOS versions as old as 3.0, I can't use block animations. Instead, I use UIView
class methods like this:
// Here you can enter an identifier to identify your animation
// and an object that will be passed to your delegate.
// These 2 parameters are only useful if you set an animation delegate
// using [UIView setAnimationDelegate:delegate];
[UIView beginAnimations:nil context:nil];
// Duration in seconds
[UIView setAnimationDuration:0.5];
// Perform you changes here.
view.frame = view.superview.bounds;
// Commit the changes
[UIView commitAnimations];
精彩评论