0

私はこのコードを持っています。私がやりたいことは、画面上にいくつかのUIViewを作成し、それらにボタンを付けて、UIViewの上にあるボタンをクリックすると、UIViewがself はアニメーションを実行し、反対側に反転します。UIView の反対側にあるボタンをクリックすると、再び反転します。

UIView を動的に作成しましたが、問題があります。ボタンはアクションを実行していますが、特定の UIView に到達する方法がわからないため、ビューはアニメーションを実行しません。

どうすればいいですか?

-(void)viewDidLoad
{
    arrImages = [NSMutableArray arrayWithObjects:[UIImage imageNamed:@"image001.jpg"],[UIImage imageNamed:@"image002.jpg"],[UIImage imageNamed:@"image003.jpg"],[UIImage imageNamed:@"image004.png"],[UIImage imageNamed:@"image005.JPG"],[UIImage imageNamed:@"image006.jpg"],[UIImage imageNamed:@"image007.jpeg"],[UIImage imageNamed:@"image008.jpg"],[UIImage imageNamed:@"image009.jpg"],[UIImage imageNamed:@"image010.png"],[UIImage imageNamed:@"image001.jpg"],[UIImage imageNamed:@"image002.jpg"],[UIImage imageNamed:@"image003.jpg"],[UIImage imageNamed:@"image004.png"],[UIImage imageNamed:@"image005.JPG"],[UIImage imageNamed:@"image006.jpg"],[UIImage imageNamed:@"image007.jpeg"],[UIImage imageNamed:@"image008.jpg"],[UIImage imageNamed:@"image009.jpg"],[UIImage imageNamed:@"image010.png"], nil];

    x = 0;
    btnFrameX = 18;
    btnFrameY = 20;

    for (int i = 0; i < [arrImages count]; i++)
    {
        if (btnFrameX <= 237)
        {
            //  Create views of cards

            UIView * first = [[UIView alloc]initWithFrame:CGRectMake(btnFrameX, btnFrameY, 65, 65)];
            UIView * secod = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 65, 65)];
            UIView * third = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 65, 65)];

            first.tag = [[NSString stringWithFormat:@"1%i",i]intValue];
            secod.tag = [[NSString stringWithFormat:@"2%i",i]intValue];
            third.tag = [[NSString stringWithFormat:@"3%i",i]intValue];

            NSLog(@"1%i",i);
            NSLog(@"2%i",i);
            NSLog(@"3%i",i);

            UILabel * cardLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 65, 65)];
            cardLabel.text = [NSString stringWithFormat:@"%i",i+1];

            UIButton * cardBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            [cardBtn setFrame:CGRectMake(0, 0, 65, 65)];
            [cardBtn setTitle:[NSString stringWithFormat:@"%i",i] forState:UIControlStateNormal];
            [cardBtn setImage:[arrImages objectAtIndex:i] forState:UIControlStateNormal];

            [cardBtn addTarget:self action:@selector(flipView:) forControlEvents:UIControlEventTouchUpInside];

            [secod addSubview:cardLabel];
            [third addSubview:cardBtn];
            [first addSubview:secod];
            [first addSubview:third];
            [self.view addSubview:first];

            btnFrameX = btnFrameX + 73;
        }
        if (btnFrameX > 237)
        {
            btnFrameX = 18;
            btnFrameY = btnFrameY + 73;
        }
    }


-(IBAction) flipView:(id)sender
{
    [UIView beginAnimations:nil context:nil];
    // Should hide the one that you clicked on and show the other one.
    [self showOtherView];
    // flipping the mainview (first), he's the one that the other 2 UIViews (second and third) attached on.
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:first cache:YES];
    [UIView setAnimationDuration:1.0f];
    [UIView setAnimationDelegate:self];
    [UIView commitAnimations];
}

-(void) showOtherView
{    
    if (x == 0)
    {
        x = 1;
        second.hidden = NO;
        third.hidden = YES;
    }
    else
    {
        x = 0;
        second.hidden = YES;
        third.hidden = NO;
    }
}

うまく説明できれば幸いです。そうでない場合は、お尋ねください。お答えします。

どうもありがとう!

4

2 に答える 2

1

プロパティ UIView を宣言します。

UIView *flippedVew;
//set property.

ビューでボタンがクリックされると、プロパティを設定し、ボタン アクション (アニメーションを実行する場所) がそのビューでアニメーションを実行します。同様に、別のビューのボタンをクリックすると、そのビューにプロパティを設定します。

于 2012-05-15T12:30:35.097 に答える
1

あなたのボタンは、到達したい UIView のサブビューですよね?

もしそうなら、それは非常に簡単です: セレクターとターゲットをボタンに設定し、

[cardBtn addTarget:self action:@selector(flipView:) forControlEvents:UIControlEventTouchUpInside];

関数を宣言し、

-(void)flipView:(UIButton*)sender;

クリックされたボタンは、送信者パラメーターになります。そう :

-(void)flipView:(UIButton*)sender {
    UIView * reachedView = [sender superview];
    /* make what you want with the reached view ! */
}

編集 :

説明では、2 つのボタン (両側に 1 つずつ) が必要です。1面がviewA、2面がviewBです。viewA と viewB は mainView に属します。

UIButton btA = ...
UIButton btB = ...
[btA addTarget:self action:@selector(flipView:) forControlEvents:UIControlEventTouchUpInside];
[btB addTarget:self action:@selector(flipView:) forControlEvents:UIControlEventTouchUpInside];
[viewA addSubview:btA];
[viewB addSubview:btB];
[mainView addSubview:viewA];
[mainView addSubview:viewB];

フリップビューになりました:

-(void)flipView:(UIButton*)sender {
    UIView * mainView = [[sender superview] superview]; // the first for viewA or B, the second for mainView...
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.75];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:mainView cache:YES];
    [mainView exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
    [UIView commitAnimations];
}

それはあなたの問題に対応していますか?

于 2012-05-15T12:12:01.237 に答える