2012-02-04 9 views
13

मैं एक एनीमेशन बनाने की कोशिश कर रहा हूं जो उपयोगकर्ता की ओर 2 फ्रेंच दरवाजे (या 2 हैच दरवाजे) की तरह दिखता है।UIImageViews को एनिमेट करने के लिए कैसे करें हैच दरवाजे खोलने

मैंने निर्मित UIViewAnimationOptionTransitionFlipFromRight संक्रमण का उपयोग करने का प्रयास किया, लेकिन संक्रमण की उत्पत्ति बाएं किनारे की बजाय UIImageView का केंद्र प्रतीत होती है। असल में मेरे पास 2 UIImageViews हैं जिनमें प्रत्येक भरने की स्क्रीन होती है। मैं चाहता हूं कि एनीमेशन यूआईएममेज व्यू की तरह दिखने के लिए स्क्रीन के केंद्र से किनारों पर उठा रहे हों।

[UIView transitionWithView:leftView 
        duration:1.0 
        options:UIViewAnimationOptionTransitionFlipFromRight       
       animations:^ { leftView.alpha = 0; } 
       completion:^(BOOL finished) { 
        [leftView removeFromSuperview]; 
       }]; 

क्या किसी ने पहले ऐसा कुछ किया है? कोई भी सहायता कमाल की होगी!

अद्यतन: निक लॉकवुड को कार्य कोड धन्यवाद

leftView.layer.anchorPoint = CGPointMake(0, 0.5); // hinge around the left edge 
leftView.frame = CGRectMake(0, 0, 160, 460); //reset view position 

rightView.layer.anchorPoint = CGPointMake(1.0, 0.5); //hinge around the right edge 
rightView.frame = CGRectMake(160, 0, 160, 460); //reset view position 

[UIView animateWithDuration:0.75 animations:^{ 
    CATransform3D leftTransform = CATransform3DIdentity; 
    leftTransform.m34 = -1.0f/500; //dark magic to set the 3D perspective 
    leftTransform = CATransform3DRotate(leftTransform, -M_PI_2, 0, 1, 0); 
    leftView.layer.transform = leftTransform; 

    CATransform3D rightTransform = CATransform3DIdentity; 
    rightTransform.m34 = -1.0f/500; //dark magic to set the 3D perspective 
    rightTransform = CATransform3DRotate(rightTransform, M_PI_2, 0, 1, 0); 
    rightView.layer.transform = rightTransform; 
}]; 
+0

नोट: यदि आप छोड़ दिया और सही दरवाजा रोटेशन पर ऋण चिह्न स्वैप कर सकते हैं बनाने के लिए दरवाजे अंदर खुले हैं। 312 परिप्रेक्ष्य सेट करने के लिए * अंधेरे जादू के लिए –

उत्तर

26

सबसे पहले अपनी परियोजना के लिए QuartzCore पुस्तकालय जोड़ सकते हैं और #import <QuartzCore/QuartzCore.h>

हर दृश्य में उप-गुणों कि animatable हैं के साथ एक layer संपत्ति है। यह वह जगह है जहां आपको एनीमेशन क्षमताओं की बात आती है जब आप सभी वास्तव में अच्छी चीजें पा सकते हैं (मैं CALayer क्लास गुणों को पढ़ने के लिए सुझाव देता हूं जो आप सेट कर सकते हैं - यह आपके दिमाग को गति देगा - गतिशील मुलायम ड्रॉप छाया किसी भी दृश्य पर?)

वैसे भी, विषय पर वापस। अपने दरवाजे को 3 डी में खोलने के लिए, पहले उन्हें स्थिति दें जैसे कि वे बंद थे, इसलिए प्रत्येक दरवाजा आधे स्क्रीन भरने के साथ।

अब उनके view.layer.anchorPoint गुण के रूप में इस प्रकार है

leftDoorView.layer.anchorPoint = CGPoint(0, 0.5); // hinge around the left edge 
rightDoorView.layer.anchorPoint = CGPoint(1.0, 0.5); // hinge around the right edge 

अब निम्नलिखित एनीमेशन

[UIView animateWithDuration:0.5 animations:^{ 
    CATransform3D leftTransform = CATransform3DIdentity; 
    leftTransform.m34 = -1.0f/500; //dark magic to set the 3D perspective 
    leftTransform = CATransform3DRotate(leftTransform, M_PI_2, 0, 1, 0); //rotate 90 degrees about the Y axis 
    leftDoorView.layer.transform = leftTransform; 
    //do the same thing but mirrored for the right door, that probably just means using -M_PI_2 for the angle. If you don't know what PI is, Google "radians" 
}]; 

लागू करते हैं और है कि यह करना चाहिए निर्धारित किया है।

अस्वीकरण: मैंने वास्तव में इसका परीक्षण नहीं किया है, इसलिए कोण पीछे हो सकते हैं, और परिप्रेक्ष्य खराब हो सकता है, लेकिन कम से कम यह एक अच्छी शुरुआत होनी चाहिए।

अद्यतन: जिज्ञासा मेरे लिए बेहतर हो गई। यहाँ पूरी तरह से काम कर रहा है कोड (इस मानता है कि छोड़ दिया और सही दरवाजे निब फ़ाइल में बंद स्थिति में बाहर रखी हैं):

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    leftDoorView.layer.anchorPoint = CGPointMake(0, 0.5); // hinge around the left edge 
    leftDoorView.center = CGPointMake(0.0, self.view.bounds.size.height/2.0); //compensate for anchor offset 
    rightDoorView.layer.anchorPoint = CGPointMake(1.0, 0.5); // hinge around the right edge 
    rightDoorView.center = CGPointMake(self.view.bounds.size.width,self.view.bounds.size.height/2.0); //compensate for anchor offset 
} 

- (IBAction)open 
{ 
    CATransform3D transform = CATransform3DIdentity; 
    transform.m34 = -1.0f/500; 
    leftDoorView.layer.transform = transform; 
    rightDoorView.layer.transform = transform; 

    [UIView animateWithDuration:0.5 animations:^{ 

     leftDoorView.layer.transform = CATransform3DRotate(transform, M_PI_2, 0, 1, 0); 
     rightDoorView.layer.transform = CATransform3DRotate(transform, -M_PI_2, 0, 1, 0); 
    }]; 
} 

- (IBAction)close 
{ 
    [UIView animateWithDuration:0.5 animations:^{ 

     CATransform3D transform = CATransform3DIdentity; 
     transform.m34 = -1.0f/500; 
     leftDoorView.layer.transform = transform; 
     rightDoorView.layer.transform = transform; 
    }]; 
} 
+1

+1 * – Till

+1

"अंधेरा जादू" "मैट्रिस को अभी समझाने की तरह महसूस नहीं करता" से बेहतर लगता है ;-) –

+4

असल में, मुझे यह कहना चाहिए था कि "कोई भी नहीं बताया जा सकता है मैट्रिक्स है ... " –