UIView动画块没有做任何事情

时间:2012-07-29 21:29:58

标签: objective-c cocoa-touch uiview core-animation objective-c-blocks

我想通过使用块(而不是动画标题)移动视图的帧来制作一个非常小的动画。

我的视图层次结构非常大,所以我将解释有关我想要制作动画的特定视图的所有内容。

最大的观点是占据整个屏幕的一个,所以它是320x480点。在这个视图中,我有一个名为“keypadContainerView”的视图,它是320x200点,位于0,261,它出现在屏幕的底部。这个keypadContainerView有一个名为keypadView的子视图,它有一堆小按钮作为子视图。

最大视图 - > KeypadContainerView - > keypadView - > UIbuttons。

截图:

enter image description here

虽然这可以不言而喻,但键盘上有KeyboardContainerView,不是键盘视图和按钮。我需要这种层次结构,因为我的目标是眼睛糖果,每一层都有不同的背景。

我创建和处理该视图的方式如下:

    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];

这就是我试图制作动画的方式:

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

有趣的是,如果我在动画中使用NSLog:参数,我实际上得到了一些输出,所以这个方法肯定会被调用。它根本不是任何动画。

如果您需要,整个控制器代码就在这里:

//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

我有一种小小的感觉,它可能与我处理我的观点的方式有关,所以有完整的源代码。

对此的任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:1)

而不是那些:

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

使用

Self.keypadViewContainer = .....

你的@synthesize错了。 您明确声明iVars没有下划线, 但是使用synthesize来创建强调的iVars。

所以你的属性对应 _keypadViewContainer 但是在你的loadView中你分配给keypadViewController

最快的修复:只使用@synthesize而不是synthesize = ... (仅当您不使用Xcode 4.4时!)

最佳修复:摆脱明确的iVar声明和 当您像dealloc一样直接访问iVar时,请确保使用_keypadViewContainer等。

理想的修复:使用ARC并使用IB构建您的键盘界面(为什么不顺便?)