我正在尝试在 Swift 中实现简单的图像操作,我想在 UIImageView 的图像上方绘制。
所以界面会像下图这样:
当点击表情符号下方时,我想将其拖放到 ImageView 上,或者只有点击才会将其移动到 uiimageview,
然后它可以在 uiimageview 中四处移动并将整个图像保存到 galery。
我在谷歌上找不到有用的资源。
那么从哪里开始呢?
最佳答案
如果表情符号是 UIImageView
, 你可以实现 touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
.以下是 mainImageView 是您要在其中添加表情符号的 View 。
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = event?.allTouches()?.first
let touchLocation = touch?.locationInView(self.view)
if(CGRectContainsPoint(emojiImageView?.frame, touchLocation)) {
//your emoji imageView has been selected.
addImageviewToMainImageView(emojiImageView)
}
}
func addImageviewToMainImageview(imageView: UIImageView!) {
imageView.removeFromSuperView()
let origin = CGPoint(mainImageView.center.x , mainImageView.center.y)
let frame = CGRect(origin.x, origin.y, imageView.frame.size.width, imageView.frame.size.height)
imageView.frame = frame
mainImageView.addSubview(imageView)
}
如果你想移动 emojiImageView
周围mainImageView
内, 你应该继承 UIView
实现touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
和 touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?)
以类似的方式。
1) 用 touchesBegan
检测触摸, 确定是否是 mainImageView
之一s subview 已被触及。设置对此 subview 的引用。
2) 如果一个 subview 被触摸,touchesMoved
将使用该引用来确定 subview 的新位置:
let touch = event?.allTouches()?.first
let touchLocation = touch?.locationInView(self)
selectedSubview.center = touchLocation
关于ios - 在 uiimageview 上方绘制和移动图像 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34424679/