0

我試圖在特定形狀上設置路徑變化的動畫。我可以通過用簡單的.path [cgpath] setter替換此代碼來手動更改動畫。但是,我想爲美學創造動態變化。CAShapeLayer路徑動畫

執行此代碼時,雖然日誌表明動畫已完成,但實際情況沒有變化。

for (CALayer *layer in self.view.layer.sublayers) { 
    if ([layer isKindOfClass:[CAShapeLayer class]]) { 
     CAShapeLayer *copy = (CAShapeLayer *)layer; 
     if ([sideChoice isEqualToString:@"datum"]) { 
      if ([copy.name isEqualToString:@"datumSideLayer"]) { 
       [CATransaction begin]; 
       [CATransaction setCompletionBlock:^{ 
        NSLog(@"Animation complete."); 
       }]; 
       CABasicAnimation *morph = [CABasicAnimation animationWithKeyPath:@"path"]; 
       morph.duration = 1; 
       morph.toValue = pathChange; 
       [copy addAnimation:morph forKey:nil]; 

       [CATransaction commit]; 
       break; 
      } 
     } else if ([sideChoice isEqualToString:@"opposite"]) { 
      if ([copy.name isEqualToString:@"oppositeSideLayer"]) { 

      } 
     } 
    } 
} 

回答

0
for (CALayer *layer in self.view.layer.sublayers) { 
    if ([layer isKindOfClass:[CAShapeLayer class]]) { 
     CAShapeLayer *copy = (CAShapeLayer *)layer; 
     if ([sideChoice isEqualToString:@"datum"]) { 
      if ([copy.name isEqualToString:@"datumSideLayer"]) { 
       [CATransaction begin]; 
       [CATransaction setCompletionBlock:^{ 
        NSLog(@"Animation complete."); 
       }]; 
       CABasicAnimation *morph = [CABasicAnimation animationWithKeyPath:@"path"]; 
       morph.duration = 1; 
       morph.fromValue = (id) copy.path; 
       morph.toValue = (id) pathChange.CGPath; 
       [copy addAnimation:morph forKey:nil]; 

       copy.path = [pathChange CGPath]; 
       [CATransaction commit]; 
       break; 
      } 
     } else if ([sideChoice isEqualToString:@"opposite"]) { 
      if ([copy.name isEqualToString:@"oppositeSideLayer"]) { 

      } 
     } 
    } 
} 

我沒有正確設置fromValue也不是我指着CGPath。