我使用exclusionPaths遇到了UITextKit的问题:当我在TextView中放置一个图像时,我正在编辑,首先看起来一切都很好:
但是当“完成”-Button被点击时,它看起来像这样,身份是错误的:
这是存储图像信息的类
class ImageClass {
var image: UIImage!
var imageView: UIImageView
var imageName: String!
var active:Bool
var exclusivePath: UIBezierPath!
init(image: UIImage!, imageName: String!) {
self.image = image
imageView = UIImageView(image: image!)
self.imageName = imageName
self.active = false
self.exclusivePath = nil
}
}
用户在选择图像时选择触发委托的UICollectionView中的图像。 (imageViewObjects是一个数组,我可以放置ImageClass的对象,所以我可以使用多个图像)
// MARK: - SelectImageDelegate
func selectedImage(name:String) {
let image = UIImage(named: name)
let imageViewObject = ImageClass(image: image, imageName: name)
self.imageViewObjects.append(imageViewObject)
imageViewObject.imageView.frame = CGRectMake(4, 7, width!, height!)
// define the exlusionPaths
let bezierPath = self.exclusionPathForImageView(imageViewObject.imageView)
imageViewObject.exclusivePath = bezierPath
self.updateExclusionPaths()
self.textView.addSubview(imageViewObject.imageView)
}
// set UIBezierPath for the UIImageView
func exclusionPathForImageView(imageView: UIImageView) -> UIBezierPath {
let bezierPath = UIBezierPath(rect:
CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y,
imageView.frame.width, imageView.frame.height))
return bezierPath
}
func updateExclusionPaths() {
textView.textContainer.exclusionPaths = []
for imageViewObject in self.imageViewObjects {
textView.textContainer.exclusionPaths.append(imageViewObject.exclusivePath)
}
}
bezierPath的上下文没问题。
点按“完成”后,我停止编辑
let doneBarButton = UIBarButtonItem(title: "Done", style: .Done, target: view, action: Selector("endEditing:"))
现在我在textViewDidEndEditing()中什么都不做。
我还尝试使用 UIButton 作为图片。类似的结果。
解决方案的任何提示?请帮助我!!!!
答案 0 :(得分:0)
找到了解决方法:
func textViewShouldEndEditing(textView: UITextView) -> Bool {
textView.editable = false
}
我必须添加到设置textView.editable = true,即在viewDidLoad()
let tapGesture = UITapGestureRecognizer(target: self, action: "resetEditable")
tapGesture.numberOfTapsRequired = 1
self.textView.addGestureRecognizer(tapGesture)
func resetEditable() {
textView.editable = true
textView.becomeFirstResponder()
}