2010-02-25 16 views
49

मेरे पास मुख्य UIView है जहां मैं अलग-अलग डेटा प्रदर्शित करता हूं। तब मैं एक बटन है, जो इस तरह subview प्रदर्शित करता है डाल:addSubview एनीमेशन

- (IBAction) buttonClicked:(id)sender 
{ 
    UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(25,25,50,20)]; 
    UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(25,25,50,25)]; 
    newLabel.text = @"some text"; 
    [newView addSubview:newLabel]; 

    [self.view addSubview:newView]; 
    [newLabel release]; 
    [newView release]; 
} 

newview ठीक प्रतीत होता है, लेकिन यह अपने आप किसी भी तरह से चेतन नहीं है, यह सिर्फ तुरंत दिखाई देता है। जब नया दृश्य दिखाई देता है तो मैं कुछ प्रकार की एनीमेशन कैसे जोड़ सकता हूं? ज़ूम या स्लाइड या उस तरह कुछ की तरह। क्या कोई साधारण कोड है?

उत्तर

76

हाय आप UIView एनिमेशन इस्तेमाल कर सकते हैं:

[newView setFrame:CGRectMake(0.0f, 480.0f, 320.0f, 480.0f)]; //notice this is OFF screen! 
[UIView beginAnimations:@"animateTableView" context:nil]; 
[UIView setAnimationDuration:0.4]; 
[newView setFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)]; //notice this is ON screen! 
[UIView commitAnimations]; 

एसडीके अब आप के लिए यह पता लगाने और पदों आप इसे दिया दृश्य चेतन होगा। यह UIViews के सबसे संपत्तियों के लिए काम करता है: अल्फा, पैमाने, सीमा, फ्रेम, आदि

वहाँ भी फ्लिप और कर्ल के रूप में एनिमेशन में निर्माण कर रहे हैं:

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:1.0]; 
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight 
          forView:newView 
          cache:YES]; 

[self.navigationController.view addSubview:settingsView.view]; 
[UIView commitAnimations]; 

आशा इस हो रही आप आरंभ :) बाहर में मदद करता है QuarzCore ढांचे के खिलाफ

+0

शांत, धन्यवाद, कि काम ठीक – Mike

+0

जबकि पहले उदाहरण तकनीकी रूप से मान्य यह खराब फार्म है आप तो मैं या तो एक दूसरे या @ adedoy के जवाब की सिफारिश करेंगे आज का समर्थन करने की जरूरत है और स्क्रीन आकार की बड़ी मात्रा पर विचार है – Sirens

22

लिंक

#import <QuartzCore/QuartzCore.h> 

CATransition *transition = [CATransition animation]; 
transition.duration = 1.0; 
transition.type = kCATransitionPush; //choose your animation 
[newView.layer addAnimation:transition forKey:nil]; 
[self.view addSubview:newView]; 
+1

+1 आप संक्रमण को और अनुकूलित करने के लिए संक्रमण उपप्रकारों का भी उपयोग कर सकते हैं। Http://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CATransition_class/#//apple_ref/doc/constant_group/Common_Transition_Subtypes – anoop4real

79

इस एक सिफारिश के उपयोग के लिए है:

[UIView transitionWithView:containerView duration:0.5 
     options:UIViewAnimationOptionTransitionCurlUp //change to whatever animation you like 
     animations:^ { [containerView addSubview:subview]; } 
     completion:nil]; 
+1

अरे @adedoy फ्लिप संभव के बिना सही एनीमेशन का उपयोग कैसे करें ?? जैसे पुशव्यू कंट्रोलर ............ –

19

मैंने पाया कि एनीमेशन ब्लॉक और विकल्पों के साथ भी, addSubview अकेले एनिमेट करने का प्रयास करने से मेरे लिए कुछ भी नहीं होगा। मुझे एक वर्कअराउंड मिला जो सबव्यू के अल्फा को 0.0 पर सेट करना है, सबव्यू जोड़ें, और उसके बाद अल्फा की सेटिंग 0.0 से 1.0 तक एनिमेट करें। यह मुझे उस फीका में प्रभाव देता है जिसकी मैं तलाश कर रहा था।

उम्मीद है कि यह किसी और की मदद करेगा।

UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)]; 
redView.backgroundColor = [UIColor redColor]; 
redView.alpha = 0.0; 

[self.view addSubview:redView]; 

[UIView animateWithDuration:0.5 animations:^{ 
    redView.alpha = 1.0; 
}]; 
+0

एक दृश्य में भंग करने का सही उत्तर – Fattie

10

चलो इसे तेजी से करें।

var redView = UIView(frame:CGRectMake(20,20,100,100)) 
redView.backgroundColor = UIColor.redColor 
redView.alpha = 0.0 
view.addSubview(redView) 

UIView.animateWithDuration(0.25) {() -> Void in 
    redView.alpha = 1.0 
} 

एक subview जोड़ने बस एक animateWithDuration ब्लॉक में इसके सामने ऐनिमेटेड नहीं होनी चाहिए। और इसे छुपाकर एनिमेटेड नहीं किया जा सकता है।

संबंधित मुद्दे