Asyncdisplaykit: custom drawing in swift 3

Created on 26 Sep 2016  路  5Comments  路  Source: facebookarchive/AsyncDisplayKit

here is my GradientNode:

class GradientNode: ASDisplayNode {

    override class func draw(_ bounds: CGRect, withParameters parameters: NSObjectProtocol?, isCancelled isCancelledBlock: AsyncDisplayKit.asdisplaynode_iscancelled_block_t, isRasterizing: Bool)
    {
        let myContext = UIGraphicsGetCurrentContext()!
        myContext.saveGState()
        myContext.clip(to: bounds)

        let componentCount: Int = 2
        let locations: [CGFloat] = [0.0, 1.0]
        let components: [CGFloat] = [0.0, 0.0, 0.0, 1.0,
                                     0.0, 0.0, 0.0, 0.0]
        let myColorSpace = CGColorSpaceCreateDeviceRGB()
        let myGradient = CGGradient(colorSpace: myColorSpace, colorComponents: components,
                                    locations: locations, count: componentCount)

        let myStartPoint = CGPoint(x: bounds.midX, y: bounds.maxY)
        let myEndPoint = CGPoint(x: bounds.midX, y: bounds.midY)
        myContext.drawLinearGradient(myGradient!, start: myStartPoint,
                                     end: myEndPoint, options: CGGradientDrawingOptions.drawsAfterEndLocation)

        myContext.restoreGState()
    }
}

but I seems I can't override draw method, because compiler gives me error: Method doesn't override any method from its superclass

what should I do?

Swift

Most helpful comment

Now, this method declaration (in ASDisplayNode_Subclasses class) has changed to -

class func draw(_ bounds: CGRect, withParameters parameters: Any?, isCancelled isCancelledBlock: () -> Bool, isRasterizing: Bool)

So if anyone come across this issue in future your method declaration when overriding would be something like this -

override class func draw(_ bounds: CGRect, withParameters parameters: Any?, isCancelled isCancelledBlock: () -> Bool, isRasterizing: Bool)

HTH.

All 5 comments

Same issue here.

The error message isn't clear, but I fixed this by adding @escaping before the block type like so:
override class func draw(_ bounds: CGRect, withParameters parameters: NSObjectProtocol?, isCancelled isCancelledBlock: @escaping asdisplaynode_iscancelled_block_t, isRasterizing: Bool)

For anyone who hit into this issue after upgrading to Swift 3, no need for @escaping after @Adlai-Holler last merge, so it should be:

override class func draw(_ bounds: CGRect, withParameters parameters: NSObjectProtocol?, isCancelled isCancelledBlock: asdisplaynode_iscancelled_block_t, isRasterizing: Bool) {

Now, this method declaration (in ASDisplayNode_Subclasses class) has changed to -

class func draw(_ bounds: CGRect, withParameters parameters: Any?, isCancelled isCancelledBlock: () -> Bool, isRasterizing: Bool)

So if anyone come across this issue in future your method declaration when overriding would be something like this -

override class func draw(_ bounds: CGRect, withParameters parameters: Any?, isCancelled isCancelledBlock: () -> Bool, isRasterizing: Bool)

HTH.

The signature I've had to use in Xcode 9.1 is

override class func draw(_ bounds: CGRect, withParameters parameters: NSObjectProtocol!, isCancelled isCancelledBlock: () -> Bool, isRasterizing: Bool) {

}

None of the others listed here worked for me.

Was this page helpful?
0 / 5 - 0 ratings