简体   繁体   中英

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

I've got this code, what I want it to do, is to create a few UIViews on the screen, with a button on them, and when you click on the button that is located on-top of the UIView, the UIView it self will perform animation and will flip to the other side, when you click on the button that is on the UIView's other side, again, it will flip back.

So i've created my UIView's dynamically, but I have a problem, the button is performing the action, but the view wont performing the animation because I dont know how to reach out to the specific UIView.

How can I do it?

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

I hope I explained myself well, If not, please ask me and I will answer.

Thanks alot!

Your button is subview of the UIView you want to reach, isn't it ?

If so, it s very simple : set a selector and target to your button,

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

Declare your function,

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

The clicked button will be in the sender parameter. So :

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

EDIT :

In your description, you want two buttons: one on each side. 1st side is viewA and 2nd side is viewB. viewA & viewB belong to 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];

now in flip view :

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

Does it correspond to your problem ?

declare a property UIView.

UIView *flippedVew;
//set property.

When a button is clicked on a view, set the property and your button action(where you perform the animation) do the animation on that view. similarly, when button on another view is clicked , set the property to that view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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