1

I'm trying to code an app which in part involves using one's finger to trace an object in a photograph. I've got two relevant files:

import UIKit

class SecondViewController: UIViewController {


    @IBOutlet weak var image_edit: UIImageView!




    var rec_houseimages: [UIImage]?



    override func viewDidLoad() {

        super.viewDidLoad()
        image_edit.image = rec_houseimages?[0]

    }

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




}

and this one:

import UIKit

class Draw: UIView {


    var touch : UITouch!
    var lineArray : [[CGPoint]] = [[CGPoint]()]
    var index = -1


    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {


        touch = touches.first! as UITouch
        let lastPoint = touch.location(in: self)

        index += 1
        lineArray.append([CGPoint]())
        lineArray[index].append(lastPoint)
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        touch = touches.first! as UITouch
        let currentPoint = touch.location(in: self)

        self.setNeedsDisplay()

        lineArray[index].append(currentPoint)

    }



    override func draw(_ rect: CGRect) {

        let context = UIGraphicsGetCurrentContext()

        if(index >= 0){

            context!.setLineWidth(5)
            context!.setStrokeColor((UIColor(red:0.00, green:0.38, blue:0.83, alpha:1.0)).cgColor)
            context!.setLineCap(.round)

            var j = 0
            while( j <= index ){
                context!.beginPath()
                var i = 0
                context?.move(to: lineArray[j][0])
                while(i < lineArray[j].count){
                    context?.addLine(to: lineArray[j][i])
                    i += 1
                }

                context!.strokePath()
                j += 1

            }

            }


        }

When I run my program, I get my desired image placed on the screen and the drawing code executes fine; but the image is in front of the drawing so that you can only see the lines if you draw outside of the image. I've been looking up things like bringSubviewToFront but I'm not sure how to implement them with this drawing stuff.

iehrlich
  • 3,572
  • 4
  • 34
  • 43
Deltron
  • 11
  • 1
  • Is your `Draw` view the "main" view of a view controller? If not, then try to (1) make sure your `Draw` view is `self.backgroundColor = UIColor.clear`, (2) add it as a subview *at the same level* (as a sibling) to your image view in the view heirarchy, then (3) in the view controller, bring the `Draw` view to the front ` view.bringSubview(toFront: myDrawView)`. I won't guarantee that that will work, but I think it might. –  Jul 10 '17 at 01:02

0 Answers0