将CGPath粘贴到底部

时间:2016-08-09 21:05:31

标签: ios iphone swift algorithm cocoa-touch

我有一个CGPath,我重绘,一切都很好,问题是视图粘到顶部而不是底部,我确信解决方案非常简单,但我可以我找不到应该改变的东西。

这是我的代码,如果你看看我真的很感激。谢谢!

let boundingBox = CGPathGetBoundingBox(myPath)

let boundingBoxAspectRatio = CGRectGetWidth(boundingBox)/CGRectGetHeight(boundingBox)
let viewAspectRatio = CGRectGetWidth(self.frame)/CGRectGetHeight(self.frame)

var scaleFactor = 1.0 as CGFloat
if (boundingBoxAspectRatio > viewAspectRatio) {
    // Width is limiting factor
    scaleFactor = CGRectGetWidth(self.frame)/CGRectGetWidth(boundingBox)
} else {
    // Height is limiting factor
    scaleFactor = CGRectGetHeight(self.frame)/CGRectGetHeight(boundingBox)
}


// Scaling the path ...
var scaleTransform = CGAffineTransformIdentity
// Scale down the path first
scaleTransform = CGAffineTransformScale(scaleTransform, scaleFactor, scaleFactor)
// Then translate the path to the upper left corner
scaleTransform = CGAffineTransformTranslate(scaleTransform, -CGRectGetMinX(boundingBox), -CGRectGetMinY(boundingBox))

let scaledSize = CGSizeApplyAffineTransform(boundingBox.size, CGAffineTransformMakeScale(scaleFactor, scaleFactor))
let centerOffset = CGSizeMake((CGRectGetWidth(self.frame)-scaledSize.width)/(scaleFactor*2.0),
                                 (CGRectGetHeight(self.frame)-scaledSize.height)/(scaleFactor*2.0))
scaleTransform = CGAffineTransformTranslate(scaleTransform, centerOffset.width, centerOffset.height);

let transformedPath = CGPathCreateCopyByTransformingPath(myPath, &scaleTransform)

0 个答案:

没有答案