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 的子视图,不是吗?

如果是这样,这很简单:为您的按钮设置一个选择器和目标,

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

声明你的功能,

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

单击的按钮将在 sender 参数中。所以 :

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

编辑 :

在您的描述中,您需要两个按钮:每侧一个。第一面是视图A,第二面是视图B。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 回答
1

声明一个属性 UIView。

UIView *flippedVew;
//set property.

在视图上单击按钮时,设置属性和按钮操作(执行动画的位置)在该视图上执行动画。同样,当单击另一个视图上的按钮时,将属性设置为该视图。

于 2012-05-15T12:30:35.097 回答