- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在使用 AVMutableComposition 在视频中叠加一些文本,我想根据某个时间间隔重复更改字符串。我正在尝试使用核心动画来实现这一点;但是,字符串属性似乎不可设置动画。有没有其他方法可以达到目的?谢谢。
代码(不工作):
func getSubtitlesAnimation(withFrames frames: [String], duration: CFTimeInterval)->CAKeyframeAnimation {
let animation = CAKeyframeAnimation(keyPath:"string")
animation.calculationMode = kCAAnimationDiscrete
animation.duration = duration
animation.values = frames
animation.keyTimes = [0,0.5,1]
animation.repeatCount = Float(frames.count)
animation.isRemovedOnCompletion = false
animation.fillMode = kCAFillModeForwards
animation.beginTime = AVCoreAnimationBeginTimeAtZero
return animation
}
最佳答案
字符串不是 CATextLayer 上的动画路径。这是 list of keyPaths你可以在任何 CALayer 上使用。
关于将 Core Animation 与 AVFoundation 一起使用的其他一些重要事项。
Example1Using-Discrete
import UIKit
class ViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//center the frame
let textFrame = CGRect(x: (self.view.bounds.width - 200)/2, y: (self.view.bounds.height - 100)/2, width: 200, height: 50)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()) {
//animation spacing could be a negative value of half the animation to appear to fade between strings
self.animateText(subtitles: ["Hello","Good Morning","Good Afternoon","Good Evening","Goodnight","Goodbye"], duration: 2, animationSpacing: 0, frame: textFrame, targetLayer: self.view.layer)
}
}
func animateText(subtitles:[String],duration:Double,animationSpacing:Double,frame:CGRect,targetLayer:CALayer){
var currentTime : Double = 0
for x in 0..<subtitles.count{
let string = subtitles[x]
let textLayer = CATextLayer()
textLayer.frame = frame
textLayer.string = string
textLayer.font = UIFont.systemFont(ofSize: 20)
textLayer.foregroundColor = UIColor.black.cgColor
textLayer.fontSize = 20.0
textLayer.alignmentMode = kCAAlignmentCenter
let anim = getSubtitlesAnimation(duration: duration, startTime: currentTime)
targetLayer.addSublayer(textLayer)
textLayer.add(anim, forKey: "opacityLayer\(x)")
currentTime += duration + animationSpacing
}
}
func getSubtitlesAnimation(duration: CFTimeInterval,startTime:Double)->CAKeyframeAnimation {
let animation = CAKeyframeAnimation(keyPath:"opacity")
animation.duration = duration
animation.calculationMode = kCAAnimationDiscrete
//have to fade in and out with a single animation because AVFoundation
//won't allow you to animate the same propery on the same layer with
//two different animations
animation.values = [0,1,1,0,0]
animation.keyTimes = [0,0.001,0.99,0.999,1]
animation.isRemovedOnCompletion = false
animation.fillMode = kCAFillModeBoth
//Replace with AVCoreAnimationBeginTimeAtZero for AVFoundation
animation.beginTime = CACurrentMediaTime() + startTime
return animation
}
}
示例 2 - 使用长淡入淡出的 Gif 附件
import UIKit
class ViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
//center the frame
let textFrame = CGRect(x: (self.view.bounds.width - 200)/2, y: (self.view.bounds.height - 100)/2, width: 200, height: 50)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()) {
//animation spacing could be a negative value of half the animation to appear to fade between strings
self.animateText(subtitles: ["Hello","Good Morning","Good Afternoon","Good Evening","Goodnight","Goodbye"], duration: 4, animationSpacing: -2, frame: textFrame, targetLayer: self.view.layer)
}
}
func animateText(subtitles:[String],duration:Double,animationSpacing:Double,frame:CGRect,targetLayer:CALayer){
var currentTime : Double = 0
for x in 0..<subtitles.count{
let string = subtitles[x]
let textLayer = CATextLayer()
textLayer.frame = frame
textLayer.string = string
textLayer.font = UIFont.systemFont(ofSize: 20)
textLayer.foregroundColor = UIColor.black.cgColor
textLayer.fontSize = 20.0
textLayer.alignmentMode = kCAAlignmentCenter
let anim = getSubtitlesAnimation(duration: duration, startTime: currentTime)
targetLayer.addSublayer(textLayer)
textLayer.add(anim, forKey: "opacityLayer\(x)")
currentTime += duration + animationSpacing
}
}
func getSubtitlesAnimation(duration: CFTimeInterval,startTime:Double)->CAKeyframeAnimation {
let animation = CAKeyframeAnimation(keyPath:"opacity")
animation.duration = duration
//have to fade in and out with a single animation because AVFoundation
//won't allow you to animate the same propery on the same layer with
//two different animations
animation.values = [0,0.5,1,0.5,0]
animation.keyTimes = [0,0.25,0.5,0.75,1]
animation.isRemovedOnCompletion = false
animation.fillMode = kCAFillModeBoth
//Replace with AVCoreAnimationBeginTimeAtZero for AVFoundation
animation.beginTime = CACurrentMediaTime() + startTime
return animation
}
}
关于ios - 如何为 CATextLayer 的字符串设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51722320/
我有几个 CATextLayers。当我双击其中之一时,我希望能够编辑它的字符串。想象一下在 Keynote 或许多其他应用程序中处理文本的方式。有什么想法吗? 我想在图层前面放置一个可编辑的文本字段
我在 CATextLayer 中绘制字符时遇到问题,使得图层的大小恰好等于字符的大小。 我使用下面的代码来获取与字符串中的字符对应的字形的大小。目前我忽略了变音符号,因此我假设字形和字符之间存在一对一
我已经束手无策了。我已经尝试了我认为的消除 CATextLayer 模糊的一切方法,但无济于事。诚然,有些东西是有帮助的,但是当放大 2 倍时,文本仍然太模糊,远不及非 CATextLayer 文本的
这让我发疯。每当我更改CATextLayer.foregroundColor属性的值时,无论我做什么,该更改都是动画的。例如,我有一个称为CATextLayer的layer,它是CALayer的子层s
我在使用 CATextLayer 时遇到了一些麻烦,这可能是我造成的,但我没有找到有关此主题的任何帮助。我在 OS X 上(在 iOS 上应该是一样的)。 我创建了一个比例因子 > 1 的 CATex
我有一个具有动态长度的动态字符串。我使用 CATextlayer 在 View 中绘制它。我设置了 wrapped = YES,但我不知道如何检查 View 中 CATextlayer 的行数来为另一
我正在开发一个需要更改 很多 CATextLayers 字符串的应用程序,但是,只有其中的一个或两个字符(但一般来说,字符串的长度约为 2-5字符)。 起初我使用的 UILabels 非常慢,因此我尝
我有一个带有 CATextLayer 子层的 CALayer。当我应用变换或以其他方式调整 CALayer 的大小时,我需要 CATextLayer 在其父级的边界内调整大小。调整大小时,CAText
我想使用 CATextLayer 以 NSAttributedString 的形式显示一些部分加粗的文本 - 但仍然想使用 IB 的便利性进行定位。 有没有办法通过界面生成器放入 CATextLaye
我正在尝试构建一个自定义控件,它可能看起来像这个 Inkscape 模型: 我正在使用 drawRect() 做一些事情,但我需要为一些元素设置动画,所以我决定切换到 CALayer 的组合。我一直在
上图显示了一个红色的 CATextLayer 框架。 当我增加文本字体大小时,文本超出框架。 我正在使用捏合手势来增加字体大小。我想获取 CATextLayer 中文本的大小,以便在文本框架超出图层框
我正在尝试将 CATextLayer 添加到 UITableViewCell。问题是文本呈现为黑色,而我已经设置了它的 foregroundColor。有人能告诉我我错过了什么吗?谢谢。 CAText
如果我包装了 == YES,有没有办法让 CATextLayer 显示“...”? 最佳答案 是的,设置 truncationMode = kCATruncationEnd。它默认为 kCATrunc
这应该真的有效,但不是: CATextLayer* textLayer = [CATextLayer layer]; textLayer.string = @"text"; [textLayer se
我有与 question 相关的问题 我设置了 contentsScale 之后,文本看起来不错,但如果我应用 3d 旋转变换,文本就会变得模糊。 图片 here 初始化代码 // init
我希望我的文本被白色边框包围。我正在使用 CATextLayer 作为文本。我知道 CATextLayer 没有属性 borderColor/borderWidth。当然,我可以使用它的父类(supe
我正在尝试修改图层中文本的字体属性,但没有成功。有人可以帮忙吗?请在下面找到代码: - (id)initWithFrame:(CGRect)frame { self = [super init
我想为我的图像创建一个文本叠加层。问题是,如果我尝试添加第二个文本,比如字幕,它会忽略我的字体大小。 titleLayer.frame = CGRectMake(0, 80, imageVie
我正在尝试对 CATextLayer 的字符串属性进行动画处理,以便我可以使用 AV Foundation 在我的视频中添加时间戳。有谁知道如何设置动画以便我可以每秒更改字符串值? 最佳答案 NSSt
我想画一个这样的列表: 1. List item 1 2. List item 2 3. List item 3 这是我的代码: NSTextList *list = [[NSTextList
我是一名优秀的程序员,十分优秀!