ios - Swift 3.0 - 用手指在 imageView 上绘图

标签 ios swift swift3

我一直致力于使用 swift 3.0 在 ImageView 上绘制红线,但遇到了一些问题。正如您可能猜到的那样,我正在使用堆栈溢出中的一些代码来帮助我进行第一次初步尝试,但遇到了一个奇怪的错误。

当我在 ImageView 上拖动手指时,它确实绘制了一条红线,但对于每个“帧”,它都会丢弃我在屏幕上进一步绘制的所有内容。因此,当我绘制时,看起来我正在绘制的线条正在下降,然后完全消失在屏幕之外。

我正在寻找的是能够简单地在 ImageView 上绘图,而不是每帧向下移动所有内容,我希望它保持在相同的位置,类似于它在所有绘图应用程序等上的工作方式.

下面是全部代码:

@IBOutlet weak var mainImageView: UIImageView!
var lastPoint = CGPoint.zero
var fromPoint = CGPoint();
var toPoint = CGPoint.zero
var red: CGFloat = 255.0
var green: CGFloat = 0.0
var blue: CGFloat = 0.0
var brushWidth: CGFloat = 10.0
var opacity: CGFloat = 1.0
var swiped = false

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint)
{
    UIGraphicsBeginImageContextWithOptions(self.mainImageView.bounds.size, false, 0);
    mainImageView.image?.draw(in: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height))
    let context = UIGraphicsGetCurrentContext()

    context?.move(to: fromPoint)
    context?.addLine(to: toPoint)
    context?.setLineCap(CGLineCap.round)
    context?.setLineWidth(brushWidth)
    context?.setStrokeColor(red: red, green: green, blue: blue, alpha: 1.0)
    context?.setBlendMode(CGBlendMode.normal)
    context?.strokePath()

    mainImageView.image = UIGraphicsGetImageFromCurrentImageContext()
    mainImageView.alpha = opacity
    UIGraphicsEndImageContext()

}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    swiped = false;
    if let touch = touches.first {
        lastPoint = touch.location(in: self.mainImageView)
    }
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    swiped = true;
    if let touch = touches.first {
        let currentPoint = touch.location(in: mainImageView)
        drawLineFrom(fromPoint: lastPoint, toPoint: currentPoint)
        lastPoint = currentPoint
    }
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if(!swiped){
        self.drawLineFrom(fromPoint: lastPoint, toPoint: lastPoint)
    }
}

感谢各位的帮助!

最佳答案

通过以下代码,您可以使用触摸绘制图像(Swift 3.0)

func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
swiped = false
if let touch = touches.first as? UITouch {
    lastPoint = touch.location(in: self.view)
}
 }
 func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) {

UIGraphicsBeginImageContext(view.frame.size)
let context = UIGraphicsGetCurrentContext()
tempImageView.image?.draw(in: CGRect(x: 0, y: 0, width:     view.frame.size.width, height: view.frame.size.height))


context?.move(to: fromPoint)
context?.addLine(to: toPoint)

context?.setLineCap(CGLineCap.round)
context?.setLineWidth(brushWidth)
context?.setStrokeColor(red: red, green: green, blue: blue, alpha: 1.0)
context?.setBlendMode(CGBlendMode.normal)
context?.strokePath()


tempImageView.image = UIGraphicsGetImageFromCurrentImageContext()
tempImageView.alpha = opacity
UIGraphicsEndImageContext()

}

 func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
swiped = true
if let touch = touches.first as? UITouch {
    let currentPoint = touch.location(in: view)
    drawLineFrom(fromPoint: lastPoint, toPoint: currentPoint)

    lastPoint = currentPoint
}
}

关于ios - Swift 3.0 - 用手指在 imageView 上绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43108417/

相关文章:

swift - 为什么在类中需要声明 self 的结构中不需要声明 self ?

ios - 使用 NavigationView 和 List 时如何更改 SwiftUI 中的背景颜色?

swift - 如何在 Swift 中创建一个通用枚举 Equatable?

ios - 如何在 iOS UI 的后台线程中处理和显示返回的异步数据

iOS:如何防止调整大小后的 View 自行重置?

ios - 如何在单元格中单击按钮时获取 TableView 单元格中的标签值

swift - 访问从 firebase 数据库获取的快照数据

swift - 如何在 Swift 的单个 View 中制作多种日期格式?

ios - iOS MultipeerConnectivity 框架中的 session 加密

ios - 使用 Testflight sdk 获取 iOS 应用程序的生产数据