显示两个形状的动画

时间:2013-11-10 17:32:25

标签: ios objective-c uiview cashapelayer animatewithduration

我有一个CircleView : UIView类,当你触摸它时(通过pop函数)可以激活(收缩和增长)。当你触摸它时,我也想要一个抚摸的圆圈出现并长出来(就像一个基本的水波纹)。 (我所说的效果显示为here in CSS

我该怎么做呢?

更多信息:

  • 理想情况下,两个动画都可以从单个CircleView
  • 中控制
  • CircleView继承了UIView

更新

根据答案,我通过subLayer添加了一个新对象。这显示确定但它在pop期间没有动画。谁能帮我理解为什么?

这是我当前的代码(无论如何都是相关的位)

- (void)layoutSubviews
{
    [self setLayerProperties];
}

- (void)setLayerProperties {
//The view’s Core Animation layer used for rendering.
CAShapeLayer *layer = (CAShapeLayer *)self.layer;

UIBezierPath *bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)
                                                  byRoundingCorners:UIRectCornerAllCorners
                                                        cornerRadii:self.frame.size];
layer.path = bezierPath.CGPath;
layer.fillColor = _Color.CGColor;

rippleLayer = [[CALayer alloc] init];
layer.path = bezierPath.CGPath;
layer.strokeColor = [UIColor blueColor].CGColor;
[layer addSublayer:rippleLayer];
}

// Does the animated "pop"
- (void)pop{

    CABasicAnimation *animation = [self animationWithKeyPath:@"transform.scale"];
    [animation setFromValue:[NSNumber numberWithFloat:1.0f]];
    [animation setToValue:[NSNumber numberWithFloat:0.8f]];
    [animation setRemovedOnCompletion:YES];
    [animation setDuration:0.15];
    [animation setAutoreverses:YES];

    [self.layer addAnimation:animation forKey:animation.keyPath];

    rippleLayer.anchorPoint = CGPointMake(1, 1);
    CABasicAnimation *scale = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    [scale setFromValue:[NSNumber numberWithFloat:1.0f]];
    [scale setToValue:[NSNumber numberWithFloat:2.0f]];
    [scale setRepeatCount:1];
    [scale setDuration:1.0f];
    //r[scale setRemovedOnCompletion:YES];
    [scale setFillMode:kCAFillModeForwards];

    [rippleLayer addAnimation:scale forKey:scale.keyPath];
}

1 个答案:

答案 0 :(得分:0)

只需将要设置动画的其他对象添加到图层,并将其alpha设置为0.0。然后,您可以在动画之前或之前播放该值。

我注意到你没有遵循使用小的初始命名变量的约定。 _color优于_Color

此外,如果要将图层置于其原始状态,通常使用标识转换矩阵:

self.transform = CGAffineTransformIdentity;