【问题标题】:Display an animate two shapes显示一个动画两个形状
【发布时间】:2013-11-10 17:32:25
【问题描述】:

我有一个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];
}

【问题讨论】:

  • 你可以在动画块内做很多事情,你不局限于只调整一个属性。如果您想要的第二个效果符合时间限制,只需在此处进行多条声明即可。否则尝试连续调用多个动画,该方法不会等待每个完成块,它会按顺序将它们全部关闭,之间有毫秒延迟。对于许多效果而言,这种延迟与同时发生的效果完全没有区别。
  • 谢谢 Ryan,我知道我可以在动画块中做多极的事情,我想真正的问题是如何添加另一个“对象”来制作动画?
  • 通常,您调用动画的视图将是您正在制作动画的许多子视图的父视图。您的示例正在为自己设置动画,但您可以为您引用的任何对象的任何属性设置动画(假设它是可动画的属性......)
  • 即。 self.someProperty.somePropertyWhichIsAView.frame = etc...那种东西
  • @ryancumley 请查看我更新的问题,谢谢

标签: ios objective-c uiview cashapelayer animatewithduration


【解决方案1】:

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

我注意到您没有遵循使用小首字母命名变量的约定。 _color_Color 更可取。

另外,如果您想将图层置于其原始状态,您通常使用识别变换矩阵:

self.transform = CGAffineTransformIdentity;

【讨论】:

  • 我一直有错误的命名习惯 atm,我会努力记住变得更好。如何向图层添加另一个元素并保留对它的引用?然后我还必须为“pop”设置动画,因为我必须为bezierPath 而不是整个UIView 设置动画?
  • 您可以添加视图 (addSubview:) 或图层 (addSublayer:)。通过声明一个私有变量来保留引用(在@implementation 之间,在{} 之间)。
  • 我已经做了,但是第二层没有动画,请看我的更新,谢谢:)
猜你喜欢
  • 2018-02-21
  • 1970-01-01
  • 2013-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-21
  • 2016-07-29
相关资源
最近更新 更多