调整 UIView 的大小以适合 CGPath
我有一个 UIView 子类,用户可以在其上添加随机 CGPath。 CGPath是通过处理UIPanGestures添加的。
我想将 UIView 的大小调整为包含 CGPath 的最小矩形。在我的 UIView 子类中,我重写了 sizeThatFits 以返回最小尺寸,如下所示:
- (CGSize) sizeThatFits:(CGSize)size {
CGRect box = CGPathGetBoundingBox(sigPath);
return box.size;
}
这按预期工作,并且 UIView 的大小调整为返回的值,但 CGPath 也按比例“调整大小”,导致用户拥有的路径不同原来画的。例如,这是具有用户绘制的路径的视图:
这是具有以下内容的视图:调整大小后的路径:
如何调整 UIView 的大小而不是“调整”路径的大小?
I have a UIView subclass on which the user can add a random CGPath. The CGPath is added by processing UIPanGestures.
I would like to resize the UIView to the minimal rect possible that contains the CGPath. In my UIView subclass, I have overridden sizeThatFits to return the minimal size as such:
- (CGSize) sizeThatFits:(CGSize)size {
CGRect box = CGPathGetBoundingBox(sigPath);
return box.size;
}
This works as expected and the UIView is resized to the value returned, but the CGPath is also "resized" proportionally resulting in a different path that what the user had originally drawn. As an example, this is the view with a path as drawn by the user:
And this is the view with the path after resizing:
How can I resize my UIView and not "resize" the path?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 CGPathGetBoundingBox。来自苹果文档:
这里有一个小的概念验证的drawRect方法。希望对您有帮助!
Use the CGPathGetBoundingBox. From Apple documentation:
Here a small proof-of-concept drawRect methods. Hope it helps you!