2012-06-03 20 views
22

को एनिमेट करने के लिए कैसे करें मैं पथ पर एक दृश्य को एनिमेट करने के लिए नहीं, बल्कि पथ को एनिमेट करने के लिए एक पथ के UIBezierPath को एनिमेट करना चाहता हूं, लेकिन यह एक आकार से दूसरे आकार में बदल जाता है। पथ एक CALayerUIBezierPath

CAShapeLayer *maskLayer = [CAShapeLayer layer]; 
maskLayer.fillColor = [[UIColor whiteColor] CGColor];  
maskLayer.path = [self getRectPath]; 

-(CGPathRef)getTrianglePath 
{ 
    UIBezierPath* triangle = [UIBezierPath bezierPath]; 
    [triangle moveToPoint:CGPointZero]; 
    [triangle addLineToPoint:CGPointMake(width(self.view),0)]; 
    [triangle addLineToPoint:CGPointMake(0, height(self.view))]; 
    [triangle closePath]; 
    return [triangle CGPath]; 
} 

-(CGPathRef)getRectPath 
{ 
    UIBezierPath*rect = [UIBezierPath bezierPathWithRect:self.view.frame]; 
    return [rect CGPath]; 
} 

उत्तर

28

में जोड़ा जाता है मैं CoreAnimation पर एक विशेषज्ञ नहीं हूँ, लेकिन जैसा कि

CABasicAnimation *morph = [CABasicAnimation animationWithKeyPath:@"path"]; 
morph.duration = 5; 
morph.toValue = (id) [self getTrianglePath]; 
[maskLayer addAnimation:morph forKey:nil]; 

पहली पंक्ति इस प्रकार आप एक CABasicAnimation परिभाषित कर सकते हैं कहा गया है कि आप एक एनीमेशन है कि बदलता है परिभाषित करना चाहते हैं path परत की संपत्ति। दूसरी पंक्ति में कहा गया है कि एनीमेशन में पांच सेकंड लगते हैं और तीसरी पंक्ति एनीमेशन की अंतिम स्थिति देती है। अंत में, हम परत को एनीमेशन जोड़ते हैं। कुंजी एनीमेशन के लिए एक अद्वितीय पहचानकर्ता है, यानी, यदि आप एक ही कुंजी के साथ दो एनिमेशन जोड़ते हैं तो केवल अंतिम व्यक्ति को माना जाता है। इस कुंजी का उपयोग तथाकथित निहित एनिमेशन को ओवरराइड करने के लिए भी किया जा सकता है। CALayer के कुछ गुण हैं जो डिफ़ॉल्ट रूप से एनिमेटेड हैं। अधिक सटीक, यदि आप इन गुणों में से किसी एक को बदलते हैं तो परिवर्तन 0.25 की अवधि के साथ एनिमेटेड होगा।

+0

धन्यवाद, सरल। मैं CAKeyframe एनीमेशन का उपयोग कर रहा था। –