【问题标题】:AVFoundation focus animation using Core Animation使用 Core Animation 的 AVFoundation 焦点动画
【发布时间】:2013-10-15 12:53:38
【问题描述】:

所以,我想显示一个方形框,这是用户点击屏幕时的典型焦点动画。这是我尝试过的:

-(void)showFocusAnimation:(CGPoint)location{
UIView *square = [[UIView alloc]initWithFrame:CGRectMake(location.x, location.y, 40, 40)];
square.alpha=1.0;
square.layer.borderColor = (__bridge CGColorRef)[UIColor colorWithRed:12.0/255.0 green:185.0/255.0 blue:249.0/255.0  alpha:1];
square.layer.borderWidth = 2.0;
[overlayView addSubview:square];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.1];
square.frame = CGRectMake(location.x, location.y, 90, 90);
[UIView commitAnimations];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.1];
square.frame = CGRectMake(location.x, location.y, 40, 40);
[UIView commitAnimations];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.2];
square.frame = CGRectMake(location.x, location.y, 90, 90);
square.alpha = 0;
[UIView commitAnimations];


}  

我有几个问题似乎无法解决:

  1. 我的边框无法显示。

  2. 目前我正在从用户点击屏幕的点开始绘制一个正方形。用户点击它的点实际上应该是正方形的中心。

  3. 我似乎无法让动画正确。我想做的是,减小正方形大小,增加它然后再减小它,然后是alpha = 0

我想如果我有 3 个不同的独立动画,也许它会起作用,但不起作用。

【问题讨论】:

  • 触发动画是异步的... sigh
  • 哎呀,不知道。那么我将如何获得所需的动画呢?我看它的方式,它是一个以上动画的组合,因此是指定的方法

标签: ios objective-c core-animation avfoundation


【解决方案1】:

您的问题是触发动画是异步的,因此它们都同时开始,前两帧动画被第三帧替换。

您可以做的一件事是使用核心动画(您的问题实际上使用 UIView 动画,甚至不是新的块的东西)来为大小和不透明度动画创建动画组。它看起来像这样(注意我没有运行它,所以它可能包含拼写错误等)

CAKeyframeAnimation *resize = [CAKeyframeAnimation animationWithKeyPath:@"bounds.size"];
resize.values = @[[NSValue valueWithCGSize:CGSizeMake(40, 40)],
                  [NSValue valueWithCGSize:CGSizeMake(90, 90)],
                  [NSValue valueWithCGSize:CGSizeMake(40, 40)],
                  [NSValue valueWithCGSize:CGSizeMake(90, 90)]];
resize.keyTimes = @[@0, @.25, @.5, @1];

CABasicAnimation *fadeOut = [CABasicAnimation animationWithKeyPath:@"opacity"];
fadeOut.toValue = @0;
fadeOut.beginTime = .2;

CAAnimationGroup *both = [CAAnimationGroup animation];
both.animations = @[resize, fadeOut];

CALayer *squareLayer = nil; // Your layer code here.
[squareLayer addAnimation:both forKey:@"Do the focus animation"];

// Make sure to remove the layer after the animation completes.

需要注意的是:

  • 我正在为bounds.size 制作动画,因为帧并没有真正改变,最好是精确的。
  • 该组有总时长
  • keyTimes 指定从 0 到 1
  • 动画完成后,将从图层中移除。

由于动画中的最后一件事是将不透明度淡化为 0,因此您应该在完成后将其移除。一种方法是成为小组的代表并实施

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    // remove layer here since it should have faded to 0 opacity
}

【讨论】:

  • 感谢您的回答,但您能否向我解释一下 both.animations = @[resize, fadeout] 中的 both 是什么?
  • 没关系,我编辑了你的答案来初始化变量。谢谢
猜你喜欢
  • 1970-01-01
  • 2012-04-27
  • 1970-01-01
  • 2011-03-08
  • 1970-01-01
  • 1970-01-01
  • 2017-05-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多