0

I am trying to call drawRect in the DrawLines class from UIViewcontroller (would like to set the tappoint variable too from the UIViewController). I would like the lines that are drawn by the DrawLines class to redraw as the user pans. I have come up with the following that produces errors :

UIViewController

@IBAction func pan(sender: UIPanGestureRecognizer) {
  DrawLines.tappoint = sender.locationInView(GraphView?()) // trying to set the point
  DrawLines.setNeedsDisplay() //trying to redraw display
 }

DrawLines

import UIKit

class DrawLines: UIView{

 required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
 }


 var tappoint: CGPoint!
 var touched = false


 override func drawRect(rect: CGRect) {

    if(touched){
        var xAxispoint: CGPoint = CGPoint(x: tappoint.x,y: 613)

        var vertical = UIBezierPath()
        vertical.moveToPoint(tappoint)
        vertical.addLineToPoint(xAxispoint)
        UIColor.blackColor().setStroke()
        vertical.stroke()

        var yAxispoint: CGPoint = CGPoint(x: 8, y: tappoint.y)
        var horizontal = UIBezierPath()
        horizontal.moveToPoint(tappoint)
        horizontal.addLineToPoint(yAxispoint)
        UIColor.blackColor().setStroke()
        horizontal.stroke()


    }
 }

}
Daniel
  • 43
  • 1
  • 8
  • 1
    Here is the well-explained answer of your requirement. http://stackoverflow.com/questions/27678582/swift-uiview-drawrect-how-to-get-drawrect-to-update-when-required – Sohil R. Memon Sep 19 '15 at 16:22
  • Thank you! Once I make the subview I get a black box. Also is there a way to have the original frame of the DrawLines view instead of the CGRectMake() ? – Daniel Sep 19 '15 at 16:44
  • I dont get the pan action either when I pan over the subview. – Daniel Sep 19 '15 at 16:56
  • @Daniel seems like you don't have lots of experience in programming, I would suggest first getting a bit more familiar with Swift before you start drawing things. However, you need to call setNeedsDisplay on an instance of the class DrawLines. In sender.locationInView, you need an instance of your class as well. Remember, that with UIPanGestureRecognizer, you can use the method sender.translationInView to get the delta that the pan moved since the method was called last. You might then reset it for the next call by calling sender.setTranslation(CGPoint.zero, inView: ...) – borchero Sep 19 '15 at 17:51

0 Answers0