繁体   English   中英

如何从UIButton调用的另一个函数到达动态UIView

[英]How to reach a dynamic UIView from another function that called by a UIButton

我有这个代码,我想要它做的是,在屏幕上创建一些UIViews,上面有一个按钮,当你点击位于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;
    }
}

我希望我能很好地解释自己,如果没有,请问我,我会回答。

非常感谢!

你的按钮是你要达到的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 ! */
}

编辑:

在您的描述中,您需要两个按钮:每侧一个。 第一侧是viewA,第二侧是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];
}

它符合您的问题吗?

声明属性UIView。

UIView *flippedVew;
//set property.

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM