带有TextKit的UITextView中的动画UIImage [英] Animated UIImage in UITextView with TextKit

查看:113
本文介绍了带有TextKit的UITextView中的动画UIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIImage,其中包含多个图像帧和一个时长,该时长是从GIF动画文件中切出的.如果将图像加载到UIWebView中,它将按照预期进行动画处理,但是我想使用UITextView代替.显而易见的选择是将TextKit与自定义NSLayoutManger和许多自定义NSAttributedString属性一起使用,以实现各种绘图效果.我有一个UITextView,我想给出一个NSAttributedString,其中包含我要在线显示的所有图像的NSTextAttachment.除了我加载这些动画的UIImages之一时,它们不会通过其帧进行动画处理,它工作正常.关于如何实现这一目标的任何想法?我提出了一个理论选择:

I have a UIImage that contains multiple image frames and a duration which was sliced together from an animated GIF file. If I load the image in a UIWebView it will animate as expected, but I want to use a UITextView instead. The obvious choice is to use TextKit with a custom NSLayoutManger and many custom NSAttributedString attributes to achieve various drawing effects. I have a single UITextView that I'd like to give a NSAttributedString that contains NSTextAttachments for all of the images I need to display inline. It's working fine except for when I load one of these animated UIImages, they do not animate through their frames. Any thoughts on how to accomplish this? I've come up with a theoretical option:

  1. 为每个动画图像添加一个自定义的属性
  2. 当布局管理器开始绘制背景时,请迭代此属性以查找附件的位置.
  3. 回调到文本视图以使用动画图像创建UIImageView,添加为子视图并调用startAnimating
  1. For each animated image add a custom defined attribute
  2. When layout manager goes to draw backgrounds, iterate this attribute to locate the positions of the attachment.
  3. Callback to the text view to create UIImageViews with the animated image, add as a subview, and call startAnimating

我还没有尝试过,但是它很hacky.我想知道是否有更好的选择.

I have yet to try this, but it's fairly hacky. I'm wondering if there's a better option.

总结:我想通过NSAttributedString内部的NSTextAttachmentUITextView内部的UIImage设置动画,而无需额外的UIImageView对象.您可以忽略以下事实:该操作以前是用UIWebView完成的. UIImage具有用于设置动画帧的属性(imagesduration),该属性当前已配置.如果将其分配给UIImageView,则它会设置动画,如果将其设置为NSTextAttachment,则没有动画.

In summary: I want to animate a UIImage inside a UITextView via NSTextAttachment inside a NSAttributedString without the necessity for extra UIImageView objects. You can ignore the fact that this was previously done with a UIWebView. UIImage has properties (images, duration) for setting up animated frames, which I currently have configured. If I assign it to a UIImageView it animates, if I set it as a NSTextAttachment, no animation.

任何反馈,我将不胜感激.谢谢!

I would appreciate any feedback. Thanks!

推荐答案

我认为此行为是因为UITextView不会使用UIImageView子视图显示附件.当我观察到在[c2>中添加和未添加NSTextAttachment的视图层次结构时,我看不到任何新视图(可以使用Xcode中的View Debugging或说[[[UIApplication sharedApplication] keyWindow] recursiveDescription]来观察视图层次结构).大概UITextView只是直接绘制其附件.

I think this behaviour is because the UITextView doesn't display the attachments using UIImageView subviews. When I observe the view hierarchy with and without the NSTextAttachments added to the UITextView, I don't see any new views (you can observe the view hierarchy using View Debugging in Xcode, or by saying [[[UIApplication sharedApplication] keyWindow] recursiveDescription]). Presumably, the UITextView just draws it's attachments directly.

也就是说,我想包括一个动画附件的一种方法是自己创建一个动画的UIImageView作为UITextView的子视图,将其精确地放置在NSLayoutManager放置NSTextAttachment的位置,然后每当UITextView滚动时,都将其滚动.您可以通过调用boundingRectForGlyphRange:inTextContainer:(可能在layoutManager:didCompleteLayoutForTextContainer:atEnd:中)从NSLayoutManager中获取NSTextAttachment的矩形.

That said, one way I can think of to include an animatable attachment is to create an animated UIImageView yourself as a subview of the UITextView, place it exactly where the NSLayoutManager places the NSTextAttachment, and scroll it whenever the UITextView scrolls. You can get the rectangle of the NSTextAttachment from the NSLayoutManager by calling boundingRectForGlyphRange:inTextContainer: (possibly in layoutManager:didCompleteLayoutForTextContainer:atEnd:).

这篇关于带有TextKit的UITextView中的动画UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆