2016-06-07 72 views
0

我正在努力爲CAShape圖層設置動畫,但無法正確繪製它。我有一個圖層。在UIView子類我創建的CALayer如下 -在CAShapeLayer中展開放置動畫

CAShapeLayer *shape1 = [CAShapeLayer layer]; 
shape1.fillColor = [[UIColor clearColor] CGColor]; 
shape1.strokeColor = [[UIColor purpleColor] CGColor]; 

CGRect frame1 = CGRectMake(13, 13, self.bounds.size.width - 26, self.bounds.size.height - 26); 
shape1.frame = frame1; 
shape1.position = CGPointMake(0,0); 
shape1.anchorPoint = CGPointMake(self.tableContainerView.frame.size.width/2.0, 
           self.tableContainerView.frame.size.height/2.0); 
shape1.path = [self pathForFrame:frame1]; 
shape1.strokeEnd = 0.0f; 
shape1.lineWidth = 1.0; 

[self.layer addSublayer:shape1]; 

我想,使其達到一定的地方看起來比初始稍大,同時保持中心製作動畫,這將創造擴大地方影響。我的縮放動畫代碼是 -

CABasicAnimation *animation2 = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; 

//animation2.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity]; 
animation2.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.1, 1.1, 1.1)]; 
animation2.repeatCount = 1; 
animation2.removedOnCompletion = NO; 
animation2.fillMode = kCAFillModeForwards; 
animation2.duration = 10; 
animation2.beginTime = CACurrentMediaTime() + 4; 
[shape1 addAnimation:animation2 forKey:nil]; 

任何幫助表示讚賞。提前致謝。

+0

中序,使這個動畫作品,形狀圖層添加到一個UIView,而不是做[shape1 addAnimation:animation2 forKey:@ 「transform.scale」];嘗試做[view.layer addAnimation:animation2 forKey:@「transform.scale」]; –

+0

已編輯的問題。已經添加了形狀來查看(self.layer正在這樣做)。請分析收益。 – Avi

回答

0

您可以使用CAKeyFrameAnimation更好地控制動畫。

你可以簡單地做:

CAMediaTimingFunction *linearTiming = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]; 
CAKeyframeAnimation *scaleXAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale.x"]; 
    scaleXAnimation.duration = 0.300; 
    scaleXAnimation.values = @[@(0.100), @(0.120), @(0.140), @(0.160)]; 
    scaleXAnimation.keyTimes = @[@(0.000), @(0.333), @(0.667), @(1.000)]; 
    scaleXAnimation.timingFunctions = @[linearTiming, linearTiming, linearTiming]; 
    scaleXAnimation.beginTime = 0; 
    scaleXAnimation.fillMode = kCAFillModeBoth; 

    [[self.yourView layer] addAnimation: scaleXAnimation forKey:@"ScaleUp_ScaleX"]; 

    //or if you are subclassing the uiview 

    [[self layer] addAnimation: scaleXAnimation forKey:@"ScaleUp_ScaleX"];