2016-08-12 138 views
0

介绍调整CGPath大小

我有一个CGPath我从SVG文件中使用PocketSVG API创建。它一切正常。

的问题

的问题是,形状绵延出于某种原因,请把这张图片上看看(蓝色是只是为了让对你更加明显,请忽略它,它应该是ClearColor):

Image 1 的目标

我想要什么来实现呢?我想要实现一个遍布屏幕宽度的形状(我不关心高度,它应该根据宽度自行修改),并粘贴到屏幕的底部,请看看这张图片为阱(请忽略圆形按钮):

Image 2

守则

最重要的部分;)

我有UIView一个子类,从SVG文件绘制这种形状,它叫CategoriesBarView。然后,在我的MainViewControllerUIViewController的一个子类)上,我创建了CategoriesBarView的一个对象,并以编程方式将其设置为子视图。

CategoriesBarView

class CategoriesBarView: UIView { 
    override func layoutSubviews() { 
     let myPath = PocketSVG.pathFromSVGFileNamed("CategoriesBar").takeUnretainedValue() 

     var transform = CGAffineTransformMakeScale(self.frame.size.width/750.0, self.frame.size.height/1334.0) 
     let transformedPath = CGPathCreateCopyByTransformingPath(myPath, &transform) 

     let myShapeLayer = CAShapeLayer() 
     myShapeLayer.path = transformedPath 

     let blur = UIBlurEffect(style: .Light) 
     let effectView = UIVisualEffectView(effect: blur) 
     effectView.frame.size = self.frame.size 
     effectView.frame.origin = CGPointMake(0, 0) 
     effectView.layer.mask = myShapeLayer 

     self.addSubview(effectView) 

    } 
} 

MainViewController

override func viewDidLoad() { 
    super.viewDidLoad() 

    let testHeight = UIScreen.mainScreen().bounds.size.height/6 // 1/6 of the screen’s height, that is the height in the target picture approximately, doesn’t it? 

    let categoriesBarView = CategoriesBarView(frame: CGRect(x: 0, y: UIScreen.mainScreen().bounds.size.height - testHeight , width: UIScreen.mainScreen().bounds.size.width, height: testHeight)) 
     categoriesBarView.backgroundColor = UIColor.blueColor() // AS I said, it should be ClearColor 
    self.view.addSubview(categoriesBarView) 
} 

请问你们谁知道这里为什么形状拉伸这样的问题是什么?如果有人能帮助我,我会很感激。

非常感谢你:)

+0

您是否试图根据屏幕分辨率缩放您的路径? –

+0

@DipenPanchasara你是什么意思?我怎样才能做到这一点?我应该在代码中更改哪些内容?谢谢! –

+0

比方说你画一条100x100的路径,并且你希望它在320x100上看起来相同,然后把你的路径的x点乘以3.2,因为它的新区域比你的实际路径多3.2倍。简单的缩放你的路径。 –

回答

1

考虑以下哪些吸引100×100尺寸的方形码。我在这里所做的是以100x100作为基准尺寸(因为它很容易计算各自的比例或比例尺寸),因为您可以看到我已经定义了代表您当前路径比例的scaleWidthscaleHeight变量。缩放比例为1.0,这意味着它将绘制100x100的正方形,如果将其分别更改为0.50.75,则会绘制50X75像素的矩形。请参阅清晰描述刻度宽度和高度差异的图像,分别为1.00.50.75

CGFloat scaleWidth = 0.50f; 
CGFloat scaleHeight = 0.75f; 

//// Square Drawing 
UIBezierPath* bezierSquarePath = [UIBezierPath bezierPath]; 

// ***Starting point of path *** 
[bezierSquarePath moveToPoint: CGPointMake(1, 1)]; 

// *** move to x and y position to draw lines, calculate respective x & y position using scaleWidth & scaleHeight *** 
[bezierSquarePath addLineToPoint: CGPointMake(100*scaleWidth, 1)]; 
[bezierSquarePath addLineToPoint: CGPointMake(100*scaleWidth, 100*scaleHeight)]; 
[bezierSquarePath addLineToPoint: CGPointMake(1, 100*scaleHeight)]; 

// *** end your path *** 
[bezierSquarePath closePath]; 
[UIColor.blackColor setStroke]; 
bezierSquarePath.lineWidth = 1; 
[bezierSquarePath stroke]; 

图1:使用scaleWidth = 1.0和scaleHeight = 1表示100x100平方。0 Represents 100x100 square using scaleWidth = 1.0 and scaleHeight = 1.0

图2:表示在使用scaleWidth = 0.50和scaleHeight 50x75平方= 0.75 Represents 100x100 square using scaleWidth = 0.50 and scaleHeight = 0.75

注意:在给定的图像的所有附图中UIView- (void)drawRect:(CGRect)rect方法完成的,因为只有UIView能够画画。我已经放置了一张UIView,它在图像中用GrayColor高亮显示。

我相信它给你一个关于缩放路径来解决你的问题的观点,因为你不能使用相同的代码,但你可以使用它来生成一个代码。

有用的工具:如果你不是图形编码方面的专家,你可以推荐使用PaintCode software,它使用UI生成Objective-C代码。以为可能有其他软件可以选择。

快乐编码:)