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,
Declare your function,
The clicked button will be in the sender parameter. So :
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.
now in flip view :
Does it correspond to your problem ?