1

I have the following code in my Draw2D class which is attached to the view in the viewcontroller.

In the viewcontroller I have a var Drawing = Draw2D(). I have a button hooked up which performs the function "Check" with self.Drawing.Check(). The problem is I can't get the line on the screen. The ellipse works fine and function Check performs well as I can see with println(). What is wrong?

import UIKit

class Draw2D: UIView {

    override func drawRect(rect: CGRect) {
        let context = UIGraphicsGetCurrentContext()
        CGContextSetLineWidth(context, 4.0)
        CGContextSetStrokeColorWithColor(context, UIColor.blueColor().CGColor)
        let rectangle = CGRectMake(60,170,200,80)
        CGContextAddEllipseInRect(context, rectangle)
        CGContextStrokePath(context)
    }

    func Check() {
        let context = UIGraphicsGetCurrentContext()
        CGContextSetLineWidth(context, 2.0)
        CGContextMoveToPoint(context, CGFloat(100), CGFloat(300))
        CGContextAddLineToPoint(context, CGFloat(100 + 200), CGFloat(300 + 100))
        CGContextStrokePath(context)
    }
}
4
  • where is Check() called? Commented Jul 4, 2015 at 13:24
  • This is the viewcontroller Commented Jul 4, 2015 at 13:28
  • 'var Drawing = Draw2D() @IBAction func Button1(sender: AnyObject) { self.Drawing.Check() }' Commented Jul 4, 2015 at 13:28
  • Drawing is done in drawRect which is call by the system. You need to change the drawRect to handle drawing the check. Commented Jul 4, 2015 at 13:33

1 Answer 1

2

Your call to Check() is outside of the regular rendering loop - I do not really know what UIGraphicsGetCurrentContext() actually returns in that case. I am guessing it will return nil:

The current graphics context is nil by default. Prior to calling its drawRect: method, view objects push a valid context onto the stack, making it current.

Anyway that would not change the fact that you cannot just call Check() and expect the line to be rendered. Assume for a second that the line was actually rendered: in the next rendering iteration you again only will do what is written inside drawRect and will therefore not display the line again.

What you have to do is create some kind of logic inside the drawRect to determine wether to call Check() or not.

A possible way to do this would be the following:

class Draw2D: UIView {

    var callCheck:Bool? // changing it to Bool! or Bool will change the required check a few lines below

    override func drawRect(rect: CGRect) {
        let context = UIGraphicsGetCurrentContext()
        CGContextSetLineWidth(context, 4.0)
        CGContextSetStrokeColorWithColor(context, UIColor.blueColor().CGColor)
        let rectangle = CGRectMake(60,170,200,80)
        CGContextAddEllipseInRect(context, rectangle)
        CGContextStrokePath(context)
        if callCheck != nil && callCheck! {
            Check()
        }
    }

    func Check() {
        let context = UIGraphicsGetCurrentContext()
        CGContextSetLineWidth(context, 2.0)
        CGContextMoveToPoint(context, CGFloat(100), CGFloat(300))
        CGContextAddLineToPoint(context, CGFloat(100 + 200), CGFloat(300 + 100))
        CGContextStrokePath(context)
    }
}

Alter your IBAction:

@IBAction func Button1(sender: AnyObject) { 
    self.Drawing.callCheck = true
    self.Drawing.setNeedsDisplay() 
}
Sign up to request clarification or add additional context in comments.

9 Comments

Hi and thx thusfar. What I really would like to do is a setup described in drawRect and then a func drawLine which draws all the lines and can be called form every other function. Where and how would I declare that drawLine function?
And BTW, I can't get the suggested alterations to work
You can declare it wherever you want, you just have to move the logic when to call that drawLine method to drawRect. EVERYTHING that draws anything must be invoked directly or indirectly from drawRect
I have put the drawLine function in drawRect before, however with the same negative result.
To be more specific: in all the examples regarding drawing and using graphics, everything is put up in a single func drawRect. I can't find any examples of calling other functions which draw as well on the screen.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.