ios - AVFoundation 焦点动画使用 Core Animation

标签 ios objective-c core-animation avfoundation

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

-(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 个不同的独立动画也许它会起作用,但不起作用。

最佳答案

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

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

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
}

关于ios - AVFoundation 焦点动画使用 Core Animation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19381764/

相关文章:

iphone - 在设置下打开热点取代屏幕 Xcode iPhone 的用户界面

ios - 如何制作自定义滚动容器 View Controller ?

javascript - iOS:这家伙用什么样的工具来制作这个演示的原型(prototype)?

ios - 真的没有办法从照片库中检索照片的原始文件名吗?

ios - ScheduledLocalNotifications 显示错误的时区

opengl-es - 如何使 OpenGL 绘图与 UIKit 更新同步

iphone - 使用 CATransform3D 翻转两个 UIView

ios - 多次调用 CATransaction 完成

ios - 如何从以前的比例开始捏缩放手势?

ios - 验证 Facebook/Twitter 连接的最佳实践。 (防止逆向工程 key )