Andy Ibanez
Andy Ibanez

Reputation: 12254

UIView Animation Block Not Doing Anything

I have a very small animation I want to do by moving a view's frame using blocks (rather than the animation headers).

My view hierarchy is quite big so I will explain everything in regards of the specific view I want to animate.

The biggest view is the one that takes the whole screen, so it is 320x480 points. Inside this view, I have another view called "keypadContainerView" that is 320x200 points, located on 0, 261 and it appears at the bottom of the screen. This keypadContainerView has a subview called keypadView, which has a bunch of small buttons as subviews.

Biggest View -> KeypadContainerView -> keypadView -> UIbuttons.

An screenshot:

enter image description here

Although this could go without saying, the keypad there is the keypadContainerView, with is't keypadView and buttons. I need this hierarchy because I'm aiming for eye candy here, and each layer has a different background.

The way I create and deal with that view is here:

    keypadViewContainer = [[[UIView alloc] initWithFrame:CGRectMake(0, 261, 320, 200)] autorelease]; //(Original is 0, 261, 320, 200)
    keypadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
    [keypadViewContainer addSubview:keypadView];

//Add the buttons

[self.view addSubview:keypadViewContainer];

And this is how I'm trying to animate it:

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [UIView animateWithDuration:5.0 animations:^{
        self.keypadViewContainer.frame = CGRectMake(0, 0, 320, 200);
    } completion:NULL];
}

What is funny is that if I NSLog something in the animations: parameter, I actually get some output so the method is definitely getting called. It's just not animating anything at all.

The whole controller code is here, in case you need it:

//UnlockKeyboardViewController
//------------------------------------------------------------------------
@interface UnlockKeyboardViewController : UIViewController
{
    NSArray *buttons;
    NSArray *passcodeFields;

    UIImage *buttonBackgroundImage;
    UIImage *buttonBackgroundHighlightedImage;
    UIImage *middleButtonBackgroundImage;
    UIImage *middleButtonBackgroundImageHighlighted;
    UIImage *screenBackgroundImage;

    UIImage *infoViewContainerImage;
    UIImage *keypadViewContainerImage;
    UIImage *passcodeFieldsContainerImage;

    UIImage *infoViewImage;
    UIImage *passcodeViewImage;

    UIView *infoViewContainer;
    UIView *keypadViewContainer;
    UIView *passcodeFieldsContainer;

    UIView *infoView;
    UIView *keypadView;
    UIView *passcodeFieldsView;

    UIView *infoToDisplayView; //The view the programmer passes to show in infoView.
}

@property(nonatomic, retain)UIImage *buttonBackgroundImage;
@property(nonatomic, retain)UIImage *buttonBackgroundHighlightedImage;
@property(nonatomic, retain)UIImage *screenBackgroundImage;
@property(nonatomic, retain)UIImage *keypadViewBackgroundImage;
@property(nonatomic, retain)UIImage *infoViewContainerImage;
@property(nonatomic, retain)UIImage *keypadViewContainerImage;
@property(nonatomic, retain)UIImage *passcodeFieldsContainerImage;
@property(nonatomic, retain)UIImage *infoViewImage;
@property(nonatomic, retain)UIImage *passcodeViewImage;
@property(nonatomic, retain)UIView *infoToDisplayView;

//Properties for container views.
@property(nonatomic, retain)UIView *infoViewContainer;
@property(nonatomic, retain)UIView *keypadViewContainer;
@property(nonatomic, retain)UIView *passcodeFieldsContainer;
@end

@implementation UnlockKeyboardViewController
@synthesize buttonBackgroundImage = _buttonBackgroundImage;
@synthesize buttonBackgroundHighlightedImage = _buttonBackgroundHighlightedImage;
@synthesize screenBackgroundImage = _screenBackgroundImage;
@synthesize keypadViewBackgroundImage = _keypadViewBackgroundImage;
@synthesize infoViewContainerImage = _infoViewContainerImage;
@synthesize keypadViewContainerImage = _keypadViewContainerImage;
@synthesize passcodeFieldsContainerImage = _passcodeFieldsContainerImage;
@synthesize infoViewImage = _infoViewImage;
@synthesize passcodeViewImage = _passcodeViewImage;
@synthesize infoToDisplayView = _infoToDisplayView;

//Synthesizers for container views.
@synthesize infoViewContainer = _infoViewContainer;
@synthesize keypadViewContainer = _keypadViewContainer;
@synthesize passcodeFieldsContainer = _passcodeFieldsContainer;

-(void)loadView
{
    [super loadView];
    self.view.frame = CGRectMake(0, 0, 320, 480);
    _unlockKeyboardBundle = [NSBundle bundleForClass:[self class]];
    buttonBackgroundImage = [UIImage imageWithContentsOfFile:[_unlockKeyboardBundle pathForResource:@"button" ofType:@"png"]];
    middleButtonBackgroundImage = [UIImage imageWithContentsOfFile:[_unlockKeyboardBundle pathForResource:@"middleButton" ofType:@"png"]];
    buttonBackgroundHighlightedImage = [UIImage imageWithContentsOfFile:[_unlockKeyboardBundle pathForResource:@"pushedButton" ofType:@"png"]];
    middleButtonBackgroundImageHighlighted = [UIImage imageWithContentsOfFile:[_unlockKeyboardBundle pathForResource:@"pushedButtonMiddle" ofType:@"png"]];

    infoViewContainerImage = [UIImage imageWithContentsOfFile:[_unlockKeyboardBundle pathForResource:@"infoViewContainerImage" ofType:@"png"]];
    keypadViewContainerImage = [UIImage imageWithContentsOfFile:[_unlockKeyboardBundle pathForResource:@"keypadViewContainerImage" ofType:@"png"]];
    passcodeFieldsContainerImage = [UIImage imageWithContentsOfFile:[_unlockKeyboardBundle pathForResource:@"passcodeViewContainerImage" ofType:@"png"]];

    infoViewImage = [UIImage imageWithContentsOfFile:[_unlockKeyboardBundle pathForResource:@"infobackground" ofType:@"png"]];
    passcodeViewImage = [UIImage imageWithContentsOfFile:[_unlockKeyboardBundle pathForResource:@"passcodescreen" ofType:@"png"]];

    //self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[_unlockKeyboardBundle pathForResource:@"lockbackground" ofType:@"png"]]];

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    keypadViewContainer = [[[UIView alloc] initWithFrame:CGRectMake(0, 261, 320, 200)] autorelease]; //(Original is 0, 261, 320, 200)
    keypadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 200)];
    [keypadViewContainer addSubview:keypadView];
    keypadViewContainer.backgroundColor = [UIColor colorWithPatternImage:keypadViewContainerImage];

    NSMutableArray *tempButtons = [NSMutableArray array];
    self.view.opaque = YES;

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button1 setTitle:@"1" forState:UIControlStateNormal];
    button1.frame = CGRectMake(0, 0, 106, 50);

    UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button4 setTitle:@"4" forState:UIControlStateNormal];
    button4.frame = CGRectMake(0, 50, 106, 50);

    UIButton *button7 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button7 setTitle:@"7" forState:UIControlStateNormal];
    button7.frame = CGRectMake(0, 100, 106, 50);

    UIButton *cancel = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancel setTitle:NSLocalizedString(@"cancel", @"Cancel string") forState:UIControlStateNormal];
    cancel.frame = CGRectMake(0, 150, 106, 50);

    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button2 setTitle:@"2" forState:UIControlStateNormal];
    button2.frame = CGRectMake(106, 0, 108, 50);

    UIButton *button5 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button5 setTitle:@"5" forState:UIControlStateNormal];
    button5.frame = CGRectMake(106, 50, 108, 50);

    UIButton *button8 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button8 setTitle:@"8" forState:UIControlStateNormal];
    button8.frame = CGRectMake(106, 100, 108, 50);

    UIButton *button0 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button0 setTitle:@"0" forState:UIControlStateNormal];
    button0.frame = CGRectMake(106, 150, 108, 50);

    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button3 setTitle:@"3" forState:UIControlStateNormal];
    button3.frame = CGRectMake(214, 0, 106, 50);

    UIButton *button6 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button6 setTitle:@"6" forState:UIControlStateNormal];
    button6.frame = CGRectMake(214, 50, 106, 50);

    UIButton *button9 = [UIButton buttonWithType:UIButtonTypeCustom];
    [button9 setTitle:@"9" forState:UIControlStateNormal];
    button9.frame = CGRectMake(214, 100, 106, 50);

    UIButton *cancelOrDelete = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelOrDelete setTitle:@"<-" forState:UIControlStateNormal];
    cancelOrDelete.frame = CGRectMake(214, 150, 108, 50);

    [tempButtons addObject:button1];
    [tempButtons addObject:button2];
    [tempButtons addObject:button3];
    [tempButtons addObject:button4];
    [tempButtons addObject:button5];
    [tempButtons addObject:button6];
    [tempButtons addObject:button7];
    [tempButtons addObject:button8];
    [tempButtons addObject:button9];
    [tempButtons addObject:button0];
    [tempButtons addObject:cancel];
    [tempButtons addObject:cancelOrDelete];

    buttons = [[NSArray arrayWithArray:tempButtons] retain];

    for(UIButton *theButton in buttons)
    {
        if([theButton.currentTitle isEqualToString:@"2"] || [theButton.currentTitle isEqualToString:@"5"] || [theButton.currentTitle isEqualToString:@"8"] || [theButton.currentTitle isEqualToString:@"0"])
        {
            [theButton setBackgroundImage:middleButtonBackgroundImage forState:UIControlStateNormal];
            [theButton setBackgroundImage:middleButtonBackgroundImageHighlighted forState:UIControlStateHighlighted];
        }else
        {
            [theButton setBackgroundImage:buttonBackgroundImage forState:UIControlStateNormal];
            [theButton setBackgroundImage:middleButtonBackgroundImageHighlighted forState:UIControlStateHighlighted];
        }
        [keypadView addSubview:theButton];
    }
    [self.view addSubview:keypadViewContainer];

    passcodeFieldsView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0,320, 110)] autorelease];
    passcodeFieldsContainer = [[[UIView alloc] initWithFrame:CGRectMake(0, 151, 320, 110)] autorelease];
    passcodeFieldsContainer.backgroundColor = [UIColor colorWithPatternImage:passcodeFieldsContainerImage];
    passcodeFieldsView.backgroundColor = [UIColor colorWithPatternImage:passcodeViewImage];

    NSMutableArray *passTemps = [NSMutableArray array];

    UITextField *tf1 = [[[UITextField alloc] initWithFrame:CGRectMake(24, 27, 50, 50)] autorelease];
    UITextField *tf2 = [[[UITextField alloc] initWithFrame:CGRectMake(98, 27, 50, 50)] autorelease];
    UITextField *tf3 = [[[UITextField alloc] initWithFrame:CGRectMake(172, 27, 50, 50)] autorelease];
    UITextField *tf4 = [[[UITextField alloc] initWithFrame:CGRectMake(246, 27, 50, 50)] autorelease];

    [passTemps addObject:tf1];
    [passTemps addObject:tf2];
    [passTemps addObject:tf3];
    [passTemps addObject:tf4];

    passcodeFields = [[NSArray arrayWithArray:passTemps] retain];

    for(UITextField *theTf in passcodeFields)
    {
        theTf.borderStyle = UITextBorderStyleBezel;
        theTf.backgroundColor = [UIColor whiteColor];
        theTf.enabled = NO;
        theTf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        theTf.textAlignment = UITextAlignmentCenter;
        theTf.adjustsFontSizeToFitWidth = YES;
        theTf.alpha = 0.7;
        theTf.secureTextEntry = YES;
        [passcodeFieldsView addSubview:theTf];
    }

    [passcodeFieldsContainer addSubview:passcodeFieldsView];

    [self.view addSubview:passcodeFieldsContainer];

    infoView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 151)] autorelease];
    infoView.backgroundColor = [UIColor colorWithPatternImage:infoViewImage];
    infoViewContainer = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 151)] autorelease];
    infoViewContainer.backgroundColor = [UIColor colorWithPatternImage:infoViewContainerImage];

    [infoViewContainer addSubview:infoView];

    [self.view addSubview:infoViewContainer];

    [pool drain];
}
-(void)viewDidLoad
{
    [super viewDidLoad];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [UIView animateWithDuration:5.0 animations:^{
        self.keypadViewContainer.frame = CGRectMake(0, 0, 320, 200);
    } completion:NULL];
}

-(void)dealloc
{
    [buttons release];
    [passcodeFields release];
    [buttonBackgroundImage release];
    [buttonBackgroundHighlightedImage release];
    [screenBackgroundImage release];
    [infoView release];
    [keypadView release];
    [passcodeFieldsView release];
    [infoViewContainerImage release];
    [keypadViewContainerImage release];
    [passcodeFieldsContainerImage release];
    [infoViewImage release];
    [passcodeViewImage release];
    [infoToDisplayView release];
    [super dealloc];
}
@end

I have a small feeling it may have something to do with the way I'm dealing with my views, so there's the whole source code.

Any help with this will be highly appreciated.

Upvotes: 1

Views: 181

Answers (1)

Mario
Mario

Reputation: 4520

Instead of those :

keypadViewContainer = [[[UIView alloc] initWithFrame:CGRectMake(0, 261, 320, 200)] autorelease]; 

Use

Self.keypadViewContainer = .....

and your @synthesize are wrong. You declare iVars explicitly without underscore, But use synthesize to create underscored iVars.

So your properties correspond to _keypadViewContainer but in your loadView you assign to keypadViewController

Quickest fix: just use @synthesize instead of synthesize = ... (that is only if you don't use Xcode 4.4!)

Best fix: get rid of the explicit iVar declaration and Make sure to use _keypadViewContainer etc. when you access iVar directly like in dealloc.

Ideal fix: use ARC and use IB to build your keypad interface (why don't you btw?)

Upvotes: 1

Related Questions