views:

18

answers:

2

Hi,

I have a view that I want to extend on the left side using an animation. All borders but the left one should remain the same, so the x position and the width of the view are changing.

I use this code:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:5.0];
self.frame = CGRectMake(self.frame.origin.x-100,
                        self.frame.origin.y,
                        self.frame.size.width+100,
                        self.frame.size.height);
[UIView commitAnimations];

If I run this code, the width of the view is set to the new value immediately and then the view is moved to the new x point, but why? How can I change this behaviour?

Thanks for your ideas!

A: 

Your code runs fine for me. How are you running it? Calling -setFrame is the right choice for what you're doing. Here is what my custom view looks like:

#import "TestView.h"

@implementation TestView

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
    }
    return self;
}

- (void)dealloc {
    [super dealloc];
}

- (void)go;
{
  [UIView beginAnimations:nil context:nil];
  [UIView setAnimationDuration:5.0];
  self.frame = CGRectMake(self.frame.origin.x-100,
                                 self.frame.origin.y,
                                 self.frame.size.width+100,
                                 self.frame.size.height);
  [UIView commitAnimations];
}

@end

I create an outlet from my view controller of type TestView and connect it and set its type in IB. Then I create a button whose handler calls [customView go]; Seems to work fine.

Matt Long
I am starting my code after button is pressed, so all view initializations should have been done.
Heinrich
I have now tried to only change the size of the view using the frame property, but no animation happens.If I try to change the size using the bounds property, the view moves ... isn't that a bit strange?
Heinrich
Here's a demo project I did. See if you can find the difference between what you're doing and what I have: http://www.cimgf.com/files/GrowViewLeft.zip
Matt Long
A: 

Okay, I made it woring now. The problem was that my view was a UIButton and I set the title and background with the setBackground:forState: and setTitle:forState:. It seems that the background of the view is not animatable. I use the work around that I add my own background with a UIImageView.

Thanks for your replies!

Heinrich