Swift 3.0 -用手指在imageView上绘图

kmb7vmvb  于 10个月前  发布在  Swift
关注(0)|答案(3)|浏览(63)

我一直致力于能够使用swift 3. 0在图像视图上绘制红线,并遇到了一些问题。正如你所猜到的,我使用了一些来自堆栈溢出的代码来帮助我进行第一次尝试,并且得到了一个奇怪的bug。
当我在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)
    }
}

字符串
谢谢你们的帮助!

hgncfbus

hgncfbus1#

下面的代码可以使用触摸绘制图像(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
}
}

字符串

im9ewurl

im9ewurl2#

下面的代码,你可以绘制图像使用触摸

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

if let touch = touches.first as? UITouch{

    prevPoint1 = touch.previousLocationInView(self.view)

    prevPoint2 = touch.previousLocationInView(self.view)

    lastPoint = touch.locationInView(self.view)

}

}


 override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {

if let touch = touches.first as? UITouch{

    let currentPoint = touch.locationInView(view)


    prevPoint2 = prevPoint1

    prevPoint1 = touch.previousLocationInView(self.view)



    UIGraphicsBeginImageContext(view.frame.size)

    let context = UIGraphicsGetCurrentContext()

    TempImage.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height))


    var mid1 = CGPointMake((prevPoint1.x + prevPoint2.x)*0.5, (prevPoint1.y + prevPoint2.y)*0.5)

    var mid2 = CGPointMake((currentPoint.x + prevPoint1.x)*0.5, (currentPoint.y + prevPoint1.y)*0.5)


    CGContextMoveToPoint(context, mid1.x, mid1.y)

    CGContextAddQuadCurveToPoint(context, prevPoint1.x, prevPoint1.y, mid2.x, mid2.y)

    //CGContextAddLineToPoint(context, toPoint.x, toPoint.y)


    CGContextSetLineCap(context, kCGLineCapRound)

    CGContextSetLineWidth(context, brushWidth)

    CGContextSetRGBStrokeColor(context, red, green,blue, 1.0)

    CGContextSetBlendMode(context, kCGBlendModeNormal)


    CGContextStrokePath(context)


    TempImage.image = UIGraphicsGetImageFromCurrentImageContext()

    TempImage.alpha = opacity

    UIGraphicsEndImageContext()


    lastPoint = currentPoint

}

 }


 override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {


UIGraphicsBeginImageContext(MainImage.frame.size)

MainImage.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: kCGBlendModeNormal, alpha: 1.0)

TempImage.image?.drawInRect(CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height), blendMode: kCGBlendModeNormal, alpha: opacity)

MainImage.image = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()


TempImage.image = nil

}

字符串

093gszye

093gszye3#

Swift 5.7.2解决方案

import UIKit

class DrawerViewController: UIViewController {
    
    enum Spec {
        static let brushColor = UIColor.green
        static var brushWidth: CGFloat = 10.0
        static var opacity: CGFloat = 1.0
    }
    
    var lastPoint = CGPoint.zero
    var swiped = false
        
    private var maskView = UIImageView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // MaskView
        view.addSubview(maskView)
        maskView.translatesAutoresizingMaskIntoConstraints = false
        maskView.backgroundColor = .white
        NSLayoutConstraint.activate([
            maskView.topAnchor.constraint(equalTo: view.topAnchor),
            maskView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            maskView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            maskView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
    }

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

        UIGraphicsBeginImageContext(view.frame.size)
        let context = UIGraphicsGetCurrentContext()
        maskView.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(Spec.brushWidth)
        context?.setStrokeColor(Spec.brushColor.cgColor)
        context?.setBlendMode(CGBlendMode.normal)
        context?.strokePath()

        maskView.image = UIGraphicsGetImageFromCurrentImageContext()
        maskView.alpha = Spec.opacity
        UIGraphicsEndImageContext()
    }

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

            lastPoint = currentPoint
        }
    }
}

字符串

相关问题