- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有以下代码(基于 this 答案)来创建一个“可变形”的圆形 <> 方形。
它在 iPhone 上运行良好:
但在 iPad 上不太好(运行相同版本的 iOS - 10):
代码是:
-(UIBezierPath*)circlePathWithCenter:(CGPoint)center andRadius:(CGFloat)radius {
UIBezierPath *circlePath = [UIBezierPath bezierPath];
[circlePath addArcWithCenter:center radius:radius startAngle:-M_PI endAngle:-M_PI/2 clockwise:YES];
[circlePath addArcWithCenter:center radius:radius startAngle:-M_PI/2 endAngle:0 clockwise:YES];
[circlePath addArcWithCenter:center radius:radius startAngle:0 endAngle:M_PI/2 clockwise:YES];
[circlePath addArcWithCenter:center radius:radius startAngle:M_PI/2 endAngle:M_PI clockwise:YES];
[circlePath closePath];
NSLog(@"%@", [circlePath debugDescription]);
return circlePath;
}
- (UIBezierPath *)squarePathWithCenter:(CGPoint)center andSize:(CGFloat)size
{
CGFloat startX = center.x-size/2;
CGFloat startY = center.y-size/2;
UIBezierPath *squarePath = [UIBezierPath bezierPath];
[squarePath moveToPoint:CGPointMake(startX, startY)];
[squarePath addLineToPoint:squarePath.currentPoint];
[squarePath addLineToPoint:CGPointMake(startX+size, startY)];
[squarePath addLineToPoint:squarePath.currentPoint];
[squarePath addLineToPoint:CGPointMake(startX+size, startY+size)];
[squarePath addLineToPoint:squarePath.currentPoint];
[squarePath addLineToPoint:CGPointMake(startX, startY+size)];
[squarePath addLineToPoint:squarePath.currentPoint];
[squarePath closePath];
NSLog(@"%@", [squarePath debugDescription]);
return squarePath;
}
动画代码为:
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
animation.duration = 0.5;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.fromValue = (__bridge id)(_innerShape.path);
animation.toValue = (__bridge id)(_innerSquare.CGPath);
_innerShape.path = _innerSquare.CGPath;
[_innerShape addAnimation:animation forKey:@"animatePath"];
在仔细检查路径时,我注意到 iPad 版本(运行相同的代码)上有更多的“曲线”点。从上一期的讨论来看,我认为控制点的数量需要相同才能得到平滑的过渡。
设备之间预期的点数是否不同?有没有更可靠的方法来实现可变形的形状?
iPad调试圆圈:
<UIBezierPath: 0x6c4f0d0; <MoveTo {13.5, 45.000004}>,
<CurveTo {44.999996, 13.5} {13.499998, 27.603035} {27.603027, 13.500002}>,
<CurveTo {45, 13.5} {44.999996, 13.5} {45, 13.5}>,
<LineTo {45, 13.5}>,
<CurveTo {76.5, 45} {62.396969, 13.499999} {76.5, 27.603031}>,
<CurveTo {76.5, 45} {76.5, 45} {76.5, 45}>,
<LineTo {76.5, 45}>,
<CurveTo {45, 76.5} {76.5, 62.396969} {62.396969, 76.5}>,
<CurveTo {45, 76.5} {45, 76.5} {45, 76.5}>,
<LineTo {45, 76.5}>,
<CurveTo {13.5, 45} {27.603031, 76.5} {13.499999, 62.396969}>,
<CurveTo {13.5, 44.999996} {13.5, 45} {13.5, 44.999996}>,
<Close>
iPhone 版:
<UIBezierPath: 0x6180000b24e0; <MoveTo {13.5, 44.999999999999993}>,
<CurveTo {45, 13.5} {13.500000000000002, 27.603030380330004} {27.603030380330011, 13.499999999999998}>,
<LineTo {45, 13.5}>,
<CurveTo {76.5, 45} {62.396969619669989, 13.500000000000002} {76.5, 27.603030380330011}>,
<LineTo {76.5, 45}>,
<CurveTo {45, 76.5} {76.5, 62.396969619669989} {62.396969619669989, 76.5}>,
<LineTo {45, 76.5}>,
<CurveTo {13.5, 45.000000000000007} {27.603030380330011, 76.5} {13.500000000000002, 62.396969619669996}>,
<Close>
两者都是正方形:
<UIBezierPath: 0x6000000b0f80; <MoveTo {29.25, 29.25}>,
<LineTo {29.25, 29.25}>,
<LineTo {60.75, 29.25}>,
<LineTo {60.75, 29.25}>,
<LineTo {60.75, 60.75}>,
<LineTo {60.75, 60.75}>,
<LineTo {29.25, 60.75}>,
<LineTo {29.25, 60.75}>,
<Close>
最佳答案
如果在两条路径之间转换,重要的是它们具有相同数量的段。
有两种基本方法:
一种方法是将圆创建为多条线段,然后将相同数量的线段组合成正方形。您只需要使用足够多的线段,使圆看起来是圆的:
static NSInteger segments = 360;
- (UIBezierPath*)circlePathWithCenter:(CGPoint)center andRadius:(CGFloat)radius {
CGFloat startAngle = -M_PI * 3.0 / 4.0;
CGPoint point = CGPointMake(radius * cos(startAngle) + center.x, radius * sinf(startAngle) + center.y);
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:point];
for (NSInteger angle = 1; angle <= segments; angle++) {
point = CGPointMake(radius * cos(startAngle + ((CGFloat)angle / 360.0 * M_PI * 2.0)) + center.x,
radius * sin(startAngle + ((CGFloat)angle / 360.0 * M_PI * 2.0)) + center.y);
[path addLineToPoint:point];
}
return path;
}
- (UIBezierPath *)squarePathWithCenter:(CGPoint)center andSize:(CGFloat)size {
CGFloat startX = center.x-size/2;
CGFloat startY = center.y-size/2;
CGFloat endX = startX+size;
CGFloat endY = startY;
UIBezierPath *path = [UIBezierPath bezierPath];
CGPoint point = CGPointMake(startX, startY);
[path moveToPoint:point];
for (NSInteger i = 1; i <= segments / 4; i++) {
CGPoint point = CGPointMake(startX + (endX - startX) * (CGFloat) i / 90.0,
startY + (endY - startY) * (CGFloat) i / 90.0);
[path addLineToPoint:point];
}
startX = endX; startY = endY;
endY += size;
for (NSInteger i = 1; i <= segments / 4; i++) {
CGPoint point = CGPointMake(startX + (endX - startX) * (CGFloat) i / 90.0,
startY + (endY - startY) * (CGFloat) i / 90.0);
[path addLineToPoint:point];
}
startX = endX; startY = endY;
endX -= size;
for (NSInteger i = 1; i <= segments / 4; i++) {
CGPoint point = CGPointMake(startX + (endX - startX) * (CGFloat) i / 90.0,
startY + (endY - startY) * (CGFloat) i / 90.0);
[path addLineToPoint:point];
}
startX = endX; startY = endY;
endY -= size;
for (NSInteger i = 1; i <= segments / 4; i++) {
CGPoint point = CGPointMake(startX + (endX - startX) * (CGFloat) i / 90.0,
startY + (endY - startY) * (CGFloat) i / 90.0);
[path addLineToPoint:point];
}
return path;
}
或者,您可以使用圆弧,但您可能希望确保线段和圆弧在两条路径中一对一地匹配。例如,我可以将正方形渲染为一系列零半径圆弧,它们之间有线段,并将圆渲染为一系列全尺寸圆弧,它们之间有零长度线段。这样,对 moveToPoint
、addArcWithCenter
和 addLineToPoint
的每次调用都会一一对应:
- (UIBezierPath*)circlePathWithCenter:(CGPoint)center andRadius:(CGFloat)radius {
UIBezierPath *path = [UIBezierPath bezierPath];
CGFloat startAngle = -M_PI;
[path moveToPoint:CGPointMake(center.x - radius, center.y)];
for (NSInteger angle = 0; angle < 4; angle++) {
[path addArcWithCenter:center radius:radius startAngle:startAngle endAngle:startAngle + M_PI_2 clockwise:true];
[path addLineToPoint:path.currentPoint];
startAngle += M_PI_2;
}
return path;
}
- (UIBezierPath *)squarePathWithCenter:(CGPoint)center andSize:(CGFloat)size {
CGFloat startX = center.x-size/2;
CGFloat startY = center.y-size/2;
CGFloat angle = -M_PI;
CGFloat endX = startX+size;
CGFloat endY = startY;
UIBezierPath *path = [UIBezierPath bezierPath];
CGPoint point = CGPointMake(startX, startY);
[path moveToPoint:point];
[path addArcWithCenter:point radius:0 startAngle:angle endAngle:angle + M_PI_2 clockwise:true];
point = CGPointMake(endX, endY);
[path addLineToPoint:point];
angle += M_PI_2;
startX = endX; startY = endY;
endY += size;
[path addArcWithCenter:point radius:0 startAngle:angle endAngle:angle + M_PI_2 clockwise:true];
point = CGPointMake(endX, endY);
[path addLineToPoint:point];
angle += M_PI_2;
startX = endX; startY = endY;
endX -= size;
[path addArcWithCenter:point radius:0 startAngle:angle endAngle:angle + M_PI_2 clockwise:true];
point = CGPointMake(endX, endY);
[path addLineToPoint:point];
angle += M_PI_2;
startX = endX; startY = endY;
endY -= size;
[path addArcWithCenter:point radius:0 startAngle:angle endAngle:angle + M_PI_2 clockwise:true];
point = CGPointMake(endX, endY);
[path addLineToPoint:point];
return path;
}
产生:
这两者会产生不同的效果,所以这取决于您的目的。
关于ios - UIBezierPath:在 iPad 上创建的控制点多于 iPhone?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39709892/
我有两个 UIBezierPath,一个表示图像的多边形部分,另一个是要在其上绘制的路径。 我需要找到它们之间的交点,以便只有在这个交点区域内的点才会被着色。 UIBezierPath 中是否有一种方
我正在制作一个应用程序,允许用户用手指在屏幕上绘制不同的颜色。图纸是用 UIBezierPaths 绘制的,但我需要橡皮擦。我确实有一个橡皮擦,它只是一条以背景图像为颜色的路径,但这种方法会导致内存问
我想用 SceneKit 画一条贝塞尔曲线,并认为这会起作用: func drawCurvedLine() { let scene = SCNScene() let scnView =
我在设置为 iPhone (Retina 4-inch) 的模拟器中运行此代码 -(void)drawRect:(CGRect)rect { [[UIColor whiteColor]set]
我想绘制月亮,然后为月亮的阴影设置动画。但启动此代码后,我可以在动画线上看到一些故障: GIF: 为什么会发生这种情况? Playground 代码 here 更新1:此函数创建的两条路径具有不同的角
假设我们有一个 UIBezierPath...它的边界是完全正方形的...像这样: func getExponentPath(rotate180: Bool) -> UIBezierPath {
我想绘制月亮,然后为月亮的阴影设置动画。但启动此代码后,我可以在动画线上看到一些故障: GIF: 为什么会发生这种情况? Playground 代码 here 更新1:此函数创建的两条路径具有不同的角
假设我们有一个 UIBezierPath...它的边界是完全正方形的...像这样: func getExponentPath(rotate180: Bool) -> UIBezierPath {
我正在试验 UIBezierPath。我想做一个 GridView 。 NSInteger yAxisIncremental = 0; NSInteger xAxisIncremental = 0;
我需要使用bezeirPath绘制此形状: 我做了以下事情: 我创建了矩形路径。 然后我添加了五个圈子。 如下面的代码所示: let stickLayerBezeirPath = UIBezierPa
我将 Apple 文档阅读到 UIBezierPath目的。我是否正确理解它不支持相对路径? 我正在尝试将此 svg 路径转换为 UIBezierPath: // svg path m90 36c
我正在尝试制作一个球从用户画线反弹的游戏。绘制线的代码包含在下面并且可以正常工作,但是一旦球与它接触或玩家画一条新线,我将如何删除线? path = [UIBezierPath bezierPath]
我想画一条这样的路径: 这就是我写的: CGFloat radius = 50; UIBezierPath *path = [UIBezierPath bezierPath]; [path moveT
我在 View 中有一个 UIBezierPath 。我想在单击形状时显示警报,但实际发生的情况是,不仅在我单击形状时显示警报,而且当我单击 View 中的任意位置时也会显示警报。 如何更改此代码,以
我正在尝试从圆形到带圆角的正方形制作动画。据我了解,对于流畅的动画,两条路径的点数应该相等。所以我写了一个基于多条弧构建圆的函数: private func circleShape(segmentNu
对于横向和纵向模式,是否可以将元素重置到相同的%位置(距x = 0轴的距离)?如果是的话你能告诉我怎么做吗?另外,由于我是 Swift 的新手,如果您发现任何错误的代码错误,请DO通知我以便得到更好的
我正在尝试在叠加渲染器中使用 UIBezierPath 绘制一些线条。我收到错误 CGContextSetStrokeColorWithColor: invalid context 0x0 我怀疑这是
我想圆化我的右角,但只适用于左角 let path = UIBezierPath(roundedRect: view.bounds, byRoundingCorners: [UIRectCorner.
我想圆化我的右角,但只适用于左角 let path = UIBezierPath(roundedRect: view.bounds, byRoundingCorners: [UIRectCorner.
当我尝试平移路径,将其移动到原点 {0, 0} 时,生成的路径边界错误。 (或者,我的假设是错误的)。 例如该路径给出以下边界信息: let bezier = UIBezierPath(cgPath:
我是一名优秀的程序员,十分优秀!