2012-05-15 1 views
0

이 코드를 얻었습니다. 원하는 코드는 화면에 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; 
    } 
} 

나는 스스로를 잘 설명하기를 바랍니다. 그렇지 않다면 나에게 묻습니다. 나는 대답 할 것입니다.

고마워요!

답변

1

귀하의 버튼은 도달하고자하는 UIView의 하위보기입니다. 그렇지 않습니까?

는 그렇다면, 그것은 매우 간단하다 : 당신의 버튼에 선택과 목표를 설정,

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

이 함수를 선언,

-(void)flipView:(UIButton*)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]; 
} 

이이 문제에 해당합니까?

+0

감사합니다. 애니메이션이 작동하고보기가 바뀌었지만 다른 문제가 있습니다. 특정 '두 번째'및 '세 번째'보기를 숨기거나 표시하도록 설정하려면 어떻게해야합니까? 그것은 '첫 번째'견해입니다. –

+2

태그를 사용하고있는 것으로 보입니다. 그렇다면 - (UIView *) viewWithTag : (NSInteger) 태그를 첫 번째보기에서 사용하여 좋은 것을 얻을 수 있습니다. – Martin

+0

어떻게 사용할 수 있습니까? 어떻게하면 '두 번째'및 '세 번째'UIView에 도달 할 수 있습니까? 첫 번째가 바뀌기 때문에 "- (void) showOtherView"의 'second'및 'third'에서만 사용하기 때문에 거기에있는 2 개의 UIView에 어떻게 도달 할 수 있습니까? –

1

속성 UIView를 선언하십시오.

UIView *flippedVew; 
//set property. 

버튼이보기에 클릭

, 는 재산을 설정하고 (당신이 애니메이션을 수행) 귀하의 버튼 동작은 해당 뷰에 애니메이션을한다. 마찬가지로 다른보기의 단추를 클릭하면 해당보기로 속성을 설정합니다.

관련 문제