Search code examples
iosobjective-cswiftswift3cgpath

CGPathCreateCopyByTransformingPath to Swift 3


I´m new to Swift 3 and I´m trying to translate the function to Swift 3:

- (void) drawRect: (CGRect)rect
{
    if (self.editionMode == Zoom) {

        for (Area *area in self.mArrayPaths) {

            CGAffineTransform zoom = CGAffineTransformMakeScale(self.scale, self.scale);

            CGPathRef movedPath = CGPathCreateCopyByTransformingPath([area.pathArea CGPath],                                                         &zoom);
            area.pathAreaTransformed = [UIBezierPath bezierPathWithCGPath:movedPath];


            [area.fillColor setFill];
            [area.strokeColor setStroke];

            [area.pathAreaTransformed fill];
            [area.pathAreaTransformed stroke];
        }
    }
    else if (self.editionMode == MoveShapes) {

        [self.currentArea.fillColor setFill];
        [self.currentArea.pathAreaShift fill];
        [self.currentArea.pathAreaShift stroke];

        for (Area *area in self.mArrayPaths) {

            if (area == self.currentArea) {

                continue;
            }

            [area.fillColor setFill];
            [area.strokeColor setStroke];

            [area.pathArea fill];
            [area.pathArea stroke];
        }

    } else {

        [self.currentArea.fillColor setFill];
        [self.currentArea.pathArea fill];
        [self.currentArea.pathArea stroke];

        for (Area *area in self.mArrayPaths) {

            [area.fillColor setFill];
            [area.strokeColor setStroke];

            [area.pathArea fill];
            [area.pathArea stroke];
        }
    }
}

I have made so far this but I have not bean able to translate this part:

    CGAffineTransform zoom = CGAffineTransformMakeScale(self.scale, self.scale);

    CGPathRef movedPath = CGPathCreateCopyByTransImformingPath([area.pathArea CGPath], &zoom);
    area.pathAreaTransformed = [UIBezierPath bezierPathWithCGPath:movedPath];

I´m this:

override func draw(_ rect: CGRect) {
    if self.editionMode == EditionMode.Zoom {

        for area in self.mArrayPaths {

            if let area = area as? Area {
                var zoom: CGAffineTransform = CGAffineTransform.init(scaleX: self.scale, y: self.scale)

                var movedPath = CGPath.copy(using: &zoom)

                if let movedPath = movedPath {
                    area.pathAreaTransformed = UIBezierPath(cgPath: movedPath)
                }
            }
        }
    }
}

I´m getting this error:

Ambiguous reference to member 'copy(dashingWithPhase:lengths:transform :) '

I have found nothing on the web but this:

https://developer.apple.com/reference/coregraphics/1411161-cgpathcreatecopybytransformingpa?language=objc

But I can't make it work.

Thanks in advance.

Happy coding.


Solution

  • This line is incorrect:

    var movedPath = CGPath.copy(using: &zoom)
    

    .copy(using:) is a instance method, not a class method. You meant (according to the original code):

    let movedPath = area.pathArea.cgPath.copy(using: &zoom)