1

(アニメーション ヘッダーではなく) ブロックを使用してビューのフレームを移動することによって実行したい非常に小さなアニメーションがあります。

私のビュー階層は非常に大きいので、アニメーション化したい特定のビューに関してすべてを説明します。

最大のビューは画面全体を占めるビューなので、320x480 ポイントです。このビュー内には、「keypadContainerView」という別のビューがあり、これは 320x200 ポイントで、0, 261 にあり、画面の下部に表示されます。この keypadContainerView には keypadView というサブビューがあり、サブビューとして小さなボタンがたくさんあります。

最大のビュー -> KeypadContainerView -> keypadView -> UIbuttons。

スクリーンショット:

ここに画像の説明を入力

これは言うまでもありませんが、キーパッドには keypadContainerView があり、keypadView とボタンはありません。ここで目を楽しませるためにこの階層が必要であり、各レイヤーには異なる背景があります。

そのビューを作成して処理する方法は次のとおりです。

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

面白いのは、animations: パラメータで何かを 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

私のビューの扱い方に何か関係があるのではないかと少し感じているので、ソースコード全体があります。

これに関する任意のヘルプは非常に高く評価されます。

4

1 に答える 1

1

それらの代わりに:

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

使用する

Self.keypadViewContainer = .....

@synthesize が間違っています。アンダースコアなしで iVar を明示的に宣言しますが、合成を使用してアンダースコア付きの iVar を作成します。

したがって、プロパティは _keypadViewContainer に対応しますが、loadView では keypadViewController に割り当てます

最も簡単な修正: synthesize = ... の代わりに @synthesize を使用するだけです (これは、Xcode 4.4 を使用していない場合のみです!)

最良の修正: 明示的な iVar 宣言を取り除き、dealloc のように iVar に直接アクセスする場合は、_keypadViewContainer などを使用してください。

理想的な修正: ARC を使用し、IB を使用してキーパッド インターフェースを構築します (なぜそうしないのですか?)

于 2012-07-29T21:40:35.470 に答える