ios - 在 uiimageview 上方绘制和移动图像 - Swift

标签 ios swift uiimageview uiimage

我正在尝试在 Swift 中实现简单的图像操作,我想在 UIImageView 的图像上方绘制。

所以界面会像下图这样:

Descreption

当点击表情符号下方时,我想将其拖放到 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/

相关文章:

ios - 可从所有文件访问 Swift 数组

ios - 仍与 Google 保持联系

swift - 在 Swift 中访问接收对象中的值时出现问题

javascript - IOS 应用程序 webview SVG ClipPath 问题

IOS推特应用添加图片

ios - 捏合时图像在拉伸(stretch)

iphone - 如何在 ImageView 中设置图像标签?

iphone - 调整 UIImage 大小并更改 UIImageView 的大小

ios - 为什么在 swift 中一切都是元组?

ios - 如何以编程方式查找 View Controller 高度?