我有这段代码,我想要它做的是在屏幕上创建几个 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;
}
}
我希望我能很好地解释自己,如果没有,请问我,我会回答。
非常感谢!