user2761551
user2761551

Reputation: 23

Calling a Draw function in Swift

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

Upvotes: 1

Views: 5299

Answers (1)

luk2302
luk2302

Reputation: 57114

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

Upvotes: 2

Related Questions