2009-11-05 18 views
7

में एनीमेशन लागू करें मैं कोको और आईफोन प्रोग्रामिंग के लिए नया हूं और मैं UIButton में एक एनीमेशन लागू करना चाहता हूं। उदाहरण के लिए, मैं एक स्क्वायर छवि के साथ एक कस्टम UIButton बनाते हैं। फिर जब मैं उस UIButton दबाता हूं, तो स्क्वायर छवि फ़्लिप हो जाएगी। ध्यान दें कि वर्ग छवि UIButton की छवि है।UIButton

[UIButton setImage:[UIImage imageNamed:@"square.png"]]; 

उत्तर

6

नीचे दबाए जाने पर बटन (पृष्ठभूमि छवि के साथ) फ़्लिप करने के लिए पूरा कोड नीचे दिया गया है। असल में आपको दो बटन और एक कंटेनर दृश्य की आवश्यकता है।

///// .H file code.... 


    //Container views used for flipping the bars to show whose turn it is 

    UIButton* btn1; 
    UIButton* btn2; 
UIView *BarContainerView; 

///// .M file code.... 

- (void)viewWillAppear:(BOOL)animated 
{ 

    BarContainerView = [[UIView alloc] initWithFrame:CGRectMake(20, 30, 103, 150)]; 

btn1 = [[UIButton buttonWithType:UIButtonTypeRoundedRect]retain]; 
[btn1 addTarget:self action:@selector(btn1_click) forControlEvents:UIControlEventTouchUpInside]; 
[btn1 setBackgroundImage:[UIImage imageNamed:@"image1.png"] forState:UIControlStateNormal]; 
btn1.frame = CGRectMake(0, 0, 103, 150); 

btn2 = [[UIButton buttonWithType:UIButtonTypeRoundedRect]retain]; 
[btn2 addTarget:self action:@selector(btn2_click) forControlEvents:UIControlEventTouchUpInside]; 
[btn2 setBackgroundImage:[UIImage imageNamed:@"image2.png"] forState:UIControlStateNormal]; 
btn2.frame = CGRectMake(0, 0, 103, 150); 

[BarContainerView addSubview:btn1]; 
[self.view addSubview:BarContainerView]; 
[self.view bringSubviewToFront:BarContainerView]; 

}

- (void) btn1_click 
{ 

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:0.75]; 
[UIView setAnimationDelegate:self]; 

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:BarContainerView cache:YES]; 

[btn1 removeFromSuperview]; 
[BarContainerView addSubview:btn2]; 
[UIView commitAnimations]; 

} 

- (void) btn2_click 
{ 

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:0.75]; 
[UIView setAnimationDelegate:self]; 

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:BarContainerView cache:YES]; 

[btn2 removeFromSuperview]; 
[BarContainerView addSubview:btn1]; 
[UIView commitAnimations]; 

} 
9

मैं एक ही समस्या थी और मैं एक बहुत ही समान तरीके से इसे हल। मैं सिर्फ UIButton subclassed और ऐसे ही एक विधि लागू:

- (void) flipBackgroundImage:(UIImage*) image 
{ 
    [UIView beginAnimations:@"flipbutton" context:NULL]; 
    [UIView setAnimationDuration:0.4]; 
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self cache:YES]; 

    [self setBackgroundImage:image forState:UIControlStateNormal]; 

    [UIView commitAnimations]; 
} 

वर्क्स एक आकर्षण की तरह।

चीयर्स, अंका