【发布时间】:2016-08-12 08:36:17
【问题描述】:
简介
我有一个CGPath,我使用PocketSVG API 从SVG 文件创建。一切正常。
问题
问题是形状由于某种原因拉长了,请看这张图(蓝色只是为了让你更容易看到,请忽略它,应该是ClearColor):
我想实现什么?我想实现一个覆盖整个屏幕宽度的形状(我不在乎高度,它应该根据宽度自行修改),并且粘在屏幕底部,请看这张图片好吧(请忽略圆形按钮):
守则
重要的部分;)
我有一个UIView 的子类,它从SVG 文件中绘制这个形状,它称为CategoriesBarView。然后,在我的MainViewController(UIViewController 的子类)上创建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)
}
你们有谁知道这里的问题是什么以及为什么形状会这样拉伸?如果有人可以在这里帮助我,我将不胜感激。
非常感谢:)
【问题讨论】:
-
您是否尝试过根据屏幕分辨率缩放路径?
-
@DipenPanchasara 你是什么意思?我怎样才能做到这一点?我应该在我的代码中更改什么?谢谢!
-
假设您在 100x100 中绘制了一条路径,并且您希望它在 320x100 中看起来也一样,然后将路径的 x 点乘以 3.2,因为它的新区域是实际路径的 3.2 倍。简单缩放路径。
-
@DipenPanchasara 我不太明白,它与拉伸路径有什么关系,您能否添加一个代码示例以帮助我更好地理解?谢谢!
标签: ios objective-c iphone swift cocoa-touch