ios - 在 Swift 中动画字符串淡入/淡出

标签 ios string animation swift

我是编程新手 - 但在过去两个月里,我在学习适用于 iOS 的 Swift 方面取得了长足进步。我正在制作一个简单的打字游戏 - 我构建我的项目的方式是我有一个隐藏的 UITextView 来检测玩家按下的字符,然后我将该字符串与可见的 UITextView 的字符串匹配。

我现在要做的是添加某种动画 - 我希望单个字母淡入/淡出

我创建了一个属性字符串,将其添加到 UITextView,但我只是想不出一种方法来为字符串中的特定范围设置动画。我试过使用类似这样的东西:

            UIView.animateWithDuration(1, delay: 0.5, options: .CurveEaseOut, animations: {
                self.stringForPlayer.addAttribute(
                    NSForegroundColorAttributeName,
                    value: UIColor.greenColor(),
                    range: NSRange(location: self.rangeOfText, length: 1))
                self.textViewForPlayer.attributedText = self.textForPlayer
                }, completion: { finished in
                    println("FINISHED")}
            )

运气不好。我想也许 UIView 动画只在 View 对象本身上,不能用于属性字符串。有什么想法甚至解决方法可以实现这一目标吗?感谢任何帮助,非常感谢!

最佳答案

您可以使用 transition(with:...)做动画。在这种情况下,将单词 ipsum 淡化为绿色。例如。在 Swift 3 及更高版本中:

let range = (textView.text as NSString).range(of: "ipsum")
if range.location == NSNotFound { return }

let string = textView.attributedText.mutableCopy() as! NSMutableAttributedString
string.addAttribute(.foregroundColor, value: UIColor.green, range: range)

UIView.transition(with: textView, duration: 1.0, options: .transitionCrossDissolve, animations: {
    self.textView.attributedText = string
})

最初,您还询问了在动画期间让文本变大和变小的问题,这比较复杂。但是你可以搜索文字,找到selectionRects ,拍摄这些 View 的快照,并为它们制作动画 transform .例如:

func growAndShrink(_ searchText: String) {
    let beginning = textView.beginningOfDocument
    
    guard
        let string = textView.text,
        let range = string.range(of: searchText),
        let start = textView.position(from: beginning, offset: string.distance(from: string.startIndex, to: range.lowerBound)),
        let end = textView.position(from: beginning, offset: string.distance(from: string.startIndex, to: range.upperBound)),
        let textRange = textView.textRange(from: start, to: end)
    else {
        return
    }
    
    textView.selectionRects(for: textRange)
        .forEach { selectionRect in
            guard let snapshotView = textView.resizableSnapshotView(from: selectionRect.rect, afterScreenUpdates: false, withCapInsets: .zero) else { return }
            
            snapshotView.frame = view.convert(selectionRect.rect, from: textView)
            view.addSubview(snapshotView)
            
            UIView.animate(withDuration: 1, delay: 0, options: .autoreverse, animations: {
                snapshotView.transform = CGAffineTransform(scaleX: 1.5, y: 1.5)
            }, completion: { _ in
                snapshotView.removeFromSuperview()
            })
    }
}

growAndShrink("consectetaur cillium”)

将导致:

grow

如果您只是为文本的颜色设置动画,您可能希望在将其淡化为所需的颜色(使其“流行”一点)之前将其淡化为clear,您可以使用完成 block :

func animateColor(of searchText: String) {
    let range = (textView.text as NSString).range(of: searchText)
    if range.location == NSNotFound { return }
    
    let string = textView.attributedText.mutableCopy() as! NSMutableAttributedString
    string.addAttribute(.foregroundColor, value: UIColor.clear, range: range)
    
    UIView.transition(with: textView, duration: 0.25, options: .transitionCrossDissolve, animations: {
        self.textView.attributedText = string
    }, completion: { _ in
        string.addAttribute(.foregroundColor, value: UIColor.red, range: range)
        UIView.transition(with: self.textView, duration: 0.25, options: .transitionCrossDissolve, animations: {
            self.textView.attributedText = string
        })
    })
}

导致:

red

有关以前版本的 Swift,请参阅 prior revision of this answer .

关于ios - 在 Swift 中动画字符串淡入/淡出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28245285/

相关文章:

objective-c - UIImageView 中的 JPG 图像内存占用

ios - 如何防止 Core Data 在 iOS 5 中重复?

Swift - 实现字符串的源文件在哪里

swift - 如何使用在不同约束之间进行插值的自动布局执行交互式动画?

JavaScript Canvas 动画

ios - 解决 iOS 应用程序在分发版本中崩溃的问题

ios - 更改动画速度系列图像

regex - 检查字符串是否以 OCaml 中的某些文本结尾的最方便方法?

c - 在 C 中使用符号拆分字符串

ios - 尝试使用以编程方式创建的图像为 UIImageView 设置动画时抛出错误