이 코드를 얻었습니다. 원하는 코드는 화면에 UIViews 몇 개를 만들고 그 위에 버튼이있는 것입니다. UIView의 상단에있는 버튼을 클릭하십시오. UIView는 자체적으로 애니메이션을 수행하고 다른 쪽으로 넘기고 UIView의 반대편에있는 버튼을 클릭하면 다시 뒤집습니다.UIButton에 의해 호출 된 다른 함수에서 동적 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 *) viewWithTag : (NSInteger) 태그를 첫 번째보기에서 사용하여 좋은 것을 얻을 수 있습니다. – Martin
어떻게 사용할 수 있습니까? 어떻게하면 '두 번째'및 '세 번째'UIView에 도달 할 수 있습니까? 첫 번째가 바뀌기 때문에 "- (void) showOtherView"의 'second'및 'third'에서만 사용하기 때문에 거기에있는 2 개의 UIView에 어떻게 도달 할 수 있습니까? –