Swift 3.0-用手指在imageView上绘图 [英] Swift 3.0 - Drawing on imageView with finger

查看:86
本文介绍了Swift 3.0-用手指在imageView上绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力使用swift 3.0在图像视图上绘制红线,并且遇到了一些问题.您可能会猜到,我正在使用堆栈溢出中的一些代码来帮助我进行首次尝试,并且遇到了奇怪的错误.

I have been working on being able to draw a red line on an image view using swift 3.0 and have ran across some problems. As you could guess, I am using some code from stack overflow to help me with my first initial attempt at this and am getting a weird bug.

当我在图像视图上拖动手指时,它确实绘制了一条红线,但是对于每个帧",它会将我绘制的所有内容都放到了屏幕上.因此,当我绘制时,看起来好像我正在绘制的线条正在下降,然后完全脱离屏幕.

As I drag my finger across the imageview it does draw a red line but for each "frame" it drops all that I drew down further on the screen. So as I draw it looks like the lines I am drawing are falling and then just completely go off screen.

我要寻找的是能够简单地在图像视图上进行绘制,而不是使所有帧向下移动,我希望它保持在相同的位置,类似于在所有绘图应用程序等上的工作方式

What I am looking for is to be able to simply draw on the image view and instead of everything moving down each frame, I would like it to keep in the same position similar to how it works on all the drawing apps and etc.

下面是所有代码:

@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)
    }
}

感谢帮助人员!

推荐答案

以下代码可以使用触摸绘制图像

Following code you can Draw the image using touch

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

}

这篇关于Swift 3.0-用手指在imageView上绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆