- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题是...
我有一条路径,我创建了一个在其中绘制它的 View 。 View 与路径具有相同的维度。然后,我创建第二个 View ,在其中重写 sizeThatFit: 方法,以便缩放第一个 View ,直到第二个 View 的所有空间已满(这是我对 sizeThatFit: 方法的作用的想法!我不知道它是否正确)。这就是代码:
CGRect rect = CGPathGetBoundingBox(objPath);
CGRect areaPath = CGRectMake(x, y, rect.size.width, rect.size.height);
FirstView* first = [[FirstView alloc] initWithFrame:areaPath andObjPath:objPath];
SecondView* second = [[SecondView alloc] initWithFrame:CGRectMake(x, y, 200, 200)];
[second addSubview:first];
在 SecondView 中,我重写了 sizeThatFit: 方法!
我不知道为什么它不起作用!路径始终具有相同的维度。我会选择一条路径并将其绘制在 View 中,其中路径占据他的维度。因此,例如,如果路径的边界框是 [10,10] 并且 View 是 [100,100],我希望该路径变得与 View 尺寸一样大。我该怎么办??
我希望问题足够清楚。抱歉我的英语:)
这是 FirstView.m:
@synthesize objPath;
- (id)initWithFrame:(CGRect)frame andObjPath:(CGMutablePathRef)path {
self = [super initWithFrame:frame];
if (self) {
// Initialization code.
objPath = CGPathCreateMutable();
CGRect rect = CGPathGetBoundingBox(path);
CGAffineTransform trans = CGAffineTransformMake(10, 0, 0, 10, self.bounds.size.width, self.bounds.size.height);
CGPathAddPath(objPath, &trans, path);
CGRect pathContainer = CGPathGetBoundingBox(path);
CGPathAddRect(objPath, &trans, pathContainer);
CGPathCloseSubpath(objPath);
}
return self;
}
- (void)drawRect:(CGRect)rect {
// Drawing code.
CGContextRef current = UIGraphicsGetCurrentContext();
CGContextAddPath(current, objPath);
CGContextSetLineWidth(current, 0.6);
CGContextSetRGBStrokeColor(current, 0xff / 255.0, 0x40 / 255.0, 0x40 / 255.0, 1);
CGContextSetRGBFillColor(current, 0xff / 255.0, 0xc1 / 255.0, 0xc1 / 255.0, 1);
CGContextDrawPath(current, kCGPathFillStroke);
}
编辑:
如果我愿意
FirstView* first = [[FirstView alloc] initWithFrame:areaPath andObjPath:objPath];
SecondView* second = [[SecondView alloc] initWithFrame:CGRectMake(x, y, 200, 200)];
[second addSubview:first];
[first sizeToFit];
在 SecondView.m 中,我重写了 sizeThatFit 方法,第一个 View 已安装!问题是路径也没有安装!它始终具有相同的尺寸:(
编辑2
我也尝试过这种方式:
FirstView* first = [[FirstView alloc] initWithFrame:areaPath andObjPath: objPath];
[first setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[first setContentMode:UIViewContentModeScaleAspectFit];
SecondView* second = [[SecondView alloc] initWithFrame:CGRectMake(x, y, 200, 200)];
second.autoresizesSubviews = YES;
[second setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[second setContentMode:UIViewContentModeCenter];
[second addSubview:first];
但是什么也没有!压力好大……:'(
请问我需要帮助!
最佳答案
您的路径具有一定的大小,因此为了放大或缩小该路径,您需要对正在绘制的 CGContext 应用变换。例如,您可以通过查看 View 的大小并将其与路径的大小进行比较来计算比例,然后将该比例应用于上下文:
// get current context
CGRect pathBoundingBox = CGPathGetBoundingBox(objPath);
CGFloat scale = MIN(self.bounds.size.width/pathBoundingBox.size.width,
self.bounds.size.height/pathBoundingBox.side.height);
CGContextScaleCTM(current, scale, scale);
// draw path
关于ios - 带有 CGPath 的 UIView 和另一个包含第一个 UIView 的 UIView。如何使第一个 View 适合第二个 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7862002/
我在 CAShapeLayer 上执行了一些 CGPath(一个带有两个圆圈的多边形)。 我可以像这样修剪/减去路径吗? 最佳答案 无需计算,可以在绘制时使用 kCGBlendModeClear 获得
我试图将 UITextField 的左上角和左下角四舍五入。这是我正在使用的代码: override func viewDidLayoutSubviews() { super.viewDidL
我有一个代表椭圆的 SKShape 节点。玩家被放置在基于椭圆 CGPath 的贝塞尔路径的当前点上: 我有两个玩家节点可以执行的操作。玩家可以顺时针或逆时针沿着路径行驶。 rotateCounter
我在 CGPath.h 头文件中找到了它。我很好奇这个 const 的作用是什么? typedef struct CGPath *CGMutablePathRef; typedef const str
我正在使用 CGPath 绘制形状并在绘制后添加到 CAShapeLayer。我正在使用 CGPathGetPathBoundingBox() 获取框架(此框架将设置为 CAShapeLayer)但它
下面的代码有点问题。 points 数组是由寻路算法提供的一组点,它给出了在下面的方法中创建的开始和结束 CGPoints 之间的最短路径。调试此代码后,我知道它有效。 我认为是 CGPath 导致了
我已经研究了一段时间了,作为一个初学者,我似乎无法完成它。我想要做的是采用简单的渐变,并用它来描边 CGPath。我尝试使用 CGContextSetStokePattern 和 PNG 线的宽度和一
我有一个 UIView,其中包含一个带有不规则大小切片的“饼图”,我需要它们是可点击的区域。通过大量的反复试验,我写了一个方法来制作馅饼,但现在我不知道如何将这些切片分成区域。目前这个方法返回 voi
我想创建一个类别来“扩展”CGPath 的“类别”并添加更多功能。因为 CGPath 不是 ObjectiveC 的常规对象,我该怎么做? 请原谅我对c/c++的了解不足 最佳答案 很简单,您只需编写
我正在使用 Swift 制作游戏,并且我有一个任意角度的 CGPath。现在让我们假设它始终是一条直线。 我想创建的是这样的: 我已经尝试了一种非常简单的方法,即以黑色的粗线宽进行描边,复制路径,然后
是否可以在创建时绘制 CGPath(添加每条线时)而不是立即绘制点的整个路径?我希望看到在添加线条时绘制的路径。 此外,是否可以一次绘制一条线作为较大路径的一部分,而不是每次都在自身上绘制一条额外的线
我正在尝试找到一种方法来创建随机闭合平滑路径(CGPath 或 UIBezierPath)。我读过 De Casteljau's algorithm以及其他关于贝塞尔路径的文章,但它似乎不符合我试图实
简单地说,我正在寻找可在 iOS 上使用的与 NSBezierPath 的 -bezierPathByFlatteningPath 等效的方法。这对我来说是直接处理 CGPath 的函数还是 UIBe
更新 我通过使用 OpenGL 绘制所有内容来绕过 CG 的限制。仍然存在一些问题,但到目前为止,它的工作速度要快得多。 一些有趣的点: GLKView :这是一个特定于 iOS 的 View ,它对
我正在尝试绘制一个 CGPath,它的笔划有一个笔划。 基本上我想使用 CGPath 画一条线。然后我想返回并在最后一个 CGPath 的两侧画线,使其具有轮廓效果。 这条线可以以任何方式弯曲和转动,
如何删除路径的某些部分?我在屏幕上绘制了一条路径,现在假设我想删除 1/3: 我试过这样做(路径是我的原始路径,cPath是将被剪切的副本): let sizePath = CGPathGetBoun
我的新游戏 Sprite 有问题... 我的“汽车”在第一圈完美地沿着赛道行驶,但从那时起,它似乎在每圈结束时都会发生偏移。有人能告诉我我做错了什么吗? - (void)createSceneCont
我需要一些示例代码,我可以在其中为曲线/圆弧路径设置动画,该路径应该绘制一个完整的圆作为动画? 我们将不胜感激。 谢谢,周六 最佳答案 更新:意识到这个问题有更好的解决方案。只需创建一个圆形路径并将
我意识到在 iPhone 上让图像遵循预先确定的路径并不是很困难(请参阅 here ),但我想知道图像是否有可能仅遵循部分的路径。例如,如果我有一条从 A 到 B 的路径,如何使图像仅以动画形式呈现朝
假设我有一个 CGPath,它是 CGContextRef 中的一个圆圈。我想做的是用颜色填充路径的反面。下面演示了其中圆圈是当前绘制的路径,我想用颜色填充圆圈的反面,留下一个空洞: 最佳答案 首先将
我是一名优秀的程序员,十分优秀!