2012-06-19 13 views
5

मैं एक गोलाकार आयताकार की चौड़ाई को एनिमेट करने की कोशिश कर रहा हूं, समस्या तब बड़ी होती है जब बड़ी चौड़ाई से पतली चौड़ाई तक जा रही है, एनीमेशन "aberration easy jump" करता है।(आईओएस) आकार लेयर के साथ गोल आयत को एनिमेट करने के लिए कैसे?

कोड यह रहा:

shapeLayer = [CAShapeLayer layer]; 
shapeRect = CGRectMake(0.0f, 0.0f, 150.0f, 200.0f); 
[shapeLayer setBounds:shapeRect]; 
[shapeLayer setPosition:CGPointMake(iniPosX, 80.0f)]; 
[shapeLayer setFillColor:[[UIColor blackColor] CGColor]]; 
[shapeLayer setStrokeColor:[[UIColor clearColor] CGColor]]; 
[shapeLayer setLineWidth:1.0f]; 
[shapeLayer setLineJoin:kCALineJoinRound]; 
[shapeLayer setOpacity:0.2]; 
path = [UIBezierPath bezierPathWithRoundedRect:shapeRect cornerRadius:15.0]; 
[shapeLayer setPath:path.CGPath]; 
[self.layer addSublayer:shapeLayer]; 

और जब मैं एनीमेशन शुरू:

- (void)adjustSelectorToPosAndSize:(float)posX andWidth:(float)width 
{ 
    shapeRect = CGRectMake(0.0f, 0.0f, width, 200.0f); 
    [shapeLayer setBounds:shapeRect]; 
    [shapeLayer setPosition:CGPointMake(posX, 80.0f)]; 
    path = [UIBezierPath bezierPathWithRoundedRect:shapeRect cornerRadius:15.0]; 
    [shapeLayer setPath:path.CGPath]; 
} 

क्या मैं गलत कर रहा हूँ?

CABasicAnimation* anim = [CABasicAnimation animationWithKeyPath: @"path"]; 
anim.fromValue = (id) fromPath.CGPath; 
anim.toValue = (id) toPath.CGPath; 
anim.duration = 1.0; 
[layer addAnimation: anim forKey: @“resize”]; 

के बाद से आप शायद सभी तीन एनिमेशन एक साथ होने के लिए, आप कर सकते हैं:

+0

सुधार एलेक्सिस के लिए धन्यवाद। – murb83

उत्तर

3

इसे कैशपलेयर के साथ करने से मुझे बहुत जटिल लगता है अगर आप इसे केवल गोलाकार आयताकार के लिए उपयोग कर रहे हैं। the cornerRadius properly सेट के साथ कैलियर का उपयोग क्यों न करें?

कोनेराइडस सेट के साथ फ्रेम को एनिमेट करना ठीक काम करेगा।

myLayer = [CALayer layer]; 
shapeRect = CGRectMake(0.0f, 0.0f, 150.0f, 200.0f); 
[myLayer setBounds:shapeRect]; 
[myLayer setPosition:CGPointMake(iniPosX, 80.0f)]; 
[myLayer setBackgroundColor:[[UIColor blackColor] CGColor]]; 
[myLayer setBorderColor:[[UIColor clearColor] CGColor]]; 
[myLayer setBorderWidth:1.0f]; 
[myLayer setOpacity:0.2]; 
[myLayer setCornerRadius:15.0]; 
[self.layer addSublayer:myLayer]; 

animating यह बस फ्रेम (पथ नहीं)

- (void)adjustSelectorToPosAndSize:(float)posX andWidth:(float)width 
{ 
    shapeRect = CGRectMake(0.0f, 0.0f, width, 200.0f); 
    [myLayer setBounds:shapeRect]; 
    [myLayer setPosition:CGPointMake(posX, 80.0f)]; 
} 

महत्वपूर्ण चेनिंग द्वारा किया जाता है:

कुछ गुण नाम बदल fillColor तरह backgroundColor बन गया है और strokeColor और lineWidthborderColor और borderWidth आदि बन गए

+0

धन्यवाद! लेकिन एक एनीमेशन नहीं करता है, तात्कालिक है। – murb83

+0

मैंने अभी कोशिश की। यह डिफ़ॉल्ट 0.3 अवधि एनीमेशन का उपयोग कर एनिमेट करता है। क्या आप परत के साथ कुछ और कर रहे हैं? –

+0

यदि आप एनीमेशन को कस्टमाइज़ करना चाहते हैं तो आपको इसे स्पष्ट रूप से करने की आवश्यकता है –

3

हालांकि CAShapeLayer के पथ संपत्ति animatable है, यह सीमा और स्थिति की तरह तो परोक्ष ऐसा नहीं करता है, आप एक स्पष्ट एनीमेशन करना है अपने आकार सामग्री या बच्चों तो नहीं है

CABasicAnimation* anim1 = [CABasicAnimation animationWithKeyPath: @"position"]; 
anim1.fromValue = [NSValue valueWithCGPoint: fromPosition]; 
anim1.toValue = [NSValue valueWithCGPoint: toPosition]; 
anim1.duration = 1.0; 

CABasicAnimation* anim2 = [CABasicAnimation animationWithKeyPath: @"bounds"]; 
anim2.fromValue = [NSValue valueWithCGRect: fromBounds]; 
anim2.toValue = [NSValue valueWithCGRect: toBounds]; 
anim2.duration = 1.0; 

CABasicAnimation* anim3 = [CABasicAnimation animationWithKeyPath: @"path"]; 
anim3.fromValue = (id) fromPath.CGPath; 
anim3.toValue = (id) toPath.CGPath; 
anim3.duration = 1.0; 

CAAnimationGroup* animG = [CAAnimationGroup animation]; 
animG.animations = [NSArray arrayWithObjects: anim1, anim2, anim3, nil]; 
animG.duration = 1.0; 
[layer addAnimation: animG forKey:@"resize"]; 

यदि आप के बजाय एक CALayer उपयोग करने में सक्षम हो सकता है::

उन सब को स्पष्ट और एक साथ समूहीकृत कर
CALayer* rectLayer = [CALayer layer]; 
rectLayer.masksToBounds = YES; 
rectLayer.bounds = startBounds; 
rectLayer.backgroundColor = [[UIColor blackColor] CGColor]; 
rectLayer.cornerRadius = 15.0; 
[self.layer addSublayer: rectLayer]; 

// Then later implicitly animate the bounds: 
rectLayer.bounds = newBounds; 
+0

धन्यवाद !! मैं आपके कोड का परीक्षण कर रहा हूं, जब आप परीक्षण पूरा करते हैं तो मैं आपको बताता हूं। – murb83

+0

मुझे खेद है, लेकिन अच्छी तरह से काम नहीं करता है, मुझे लगता है कि मैं कुछ गलत कर रहा हूं, लेकिन परत जहां मैं चाहता हूं वहां नहीं चलता है, मुझे लगता है कि जब मैं एनीमेशन करता हूं तो संदर्भ बिंदु अलग होता है। माफ कीजिएगा मेरी अंग्रेजी अच्छी नही है। जिस तरह से आप एनिमेशन का उपयोग करते हैं, वास्तव में मेरी रूचि है, आपने समूह एनिमेशन में मेरे लिए एक नया तरीका खोला है। ^^ – murb83

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