这是一张简单的图纸
- (void)drawRect:(CGRect)rect
{
//vertical line with 1 px stroking
UIBezierPath *vertLine = [[UIBezierPath alloc] init];
[vertLine moveToPoint:CGPointMake(20.0, 10.0)];
[vertLine addLineToPoint:CGPointMake(20.0, 400.0)];
vertLine.lineWidth = 1.0;
[[UIColor blackColor] setStroke];
[vertLine stroke];
//vertical rectangle 1px width
UIBezierPath *vertRect= [UIBezierPath bezierPathWithRect:CGRectMake(40.0, 10.0, 1.0, 390.0)];
[[UIColor blackColor] setFill];
[vertRect fill];
}
Run Code Online (Sandbox Code Playgroud)
在非视网膜3GS和模拟器上,第一行是模糊的,看起来比1 px宽,但第二行是清晰的.
不幸的是我没有iPhone4和新iPad进行测试,但在视网膜模拟器上两条线看起来都一样.
问题:矩形而非中风是获得非视网膜和视网膜设备相同结果的唯一方法吗?
将一些Cocoa代码移动到Cocoa Touch时,我很失望地发现UIBezierPath缺少"访问路径元素"方法:
– elementCount
– elementAtIndex:
– elementAtIndex:associatedPoints:
– removeAllPoints
– setAssociatedPoints:atIndex:
Run Code Online (Sandbox Code Playgroud)
在Cocoa Touch中获取这些元素的唯一方法似乎就是通过CGPathApply.在我尝试将其重新创建为UIBezierPath的子类或类别之前,我想知道这是否已经完成.有没有人知道这样的事情是否已经可用?
我正在创建一个CAShapeLayer用作UIView图层的掩码.我用a UIBezierPath来绘制形状图层.它工作得很好,除了我画画时得到一些奇怪的结果.几何体的行为不符合预期.我正试图在右上角绘制简单的"馅饼切片":
#define degreesToRadians(x) ((x) * M_PI / 180.0)
...
// "layer" refer's to the UIView's root layer
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame =
layer.presentationLayer ?
((CAGradientLayer *)layer.presentationLayer).bounds : layer.bounds;
maskLayer.fillRule = kCAFillRuleEvenOdd;
maskLayer.needsDisplayOnBoundsChange = YES;
CGFloat maskLayerWidth = maskLayer.bounds.size.width;
CGFloat maskLayerHeight = maskLayer.bounds.size.height;
CGPoint maskLayerCenter =
CGPointMake(maskLayerWidth/2,maskLayerHeight/2);
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:maskLayerCenter];
[path addArcWithCenter:maskLayerCenter radius:(maskLayerWidth/2)
startAngle:degreesToRadians(0) endAngle:degreesToRadians(90) clockwise:YES];
[path closePath];
maskLayer.path = path.CGPath;
layer.mask = maskLayer;
Run Code Online (Sandbox Code Playgroud)
最终结果是在右下角绘制了饼图切片.弧的第一个点是以90度绘制的,然后是180度.即使我使用0度和90度角,它为什么会这样做?
我设计了这个代码来生成一个Bezier路径,用作CAShapeLayer掩盖UIView的路径(视图的高度和宽度是可变的)
这段代码生成一个边缘锐利的三角形,但我想让它成为一个圆角!我花了好2小时试图一起工作addArcWithCenter...,lineCapStyle和lineJoinStyle等,但似乎没有任何为我工作.
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
CGPoint center = CGPointMake(rect.size.width / 2, 0);
CGPoint bottomLeft = CGPointMake(10, rect.size.height - 0);
CGPoint bottomRight = CGPointMake(rect.size.width - 0, rect.size.height - 0);
[bezierPath moveToPoint:center];
[bezierPath addLineToPoint:bottomLeft];
[bezierPath addLineToPoint:bottomRight];
[bezierPath closePath];
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,如何在UIBezierPath中舍入三角形的所有边缘(我需要子层,多路径等)吗?
NB我没有画这个BezierPath所以所有的CGContext...功能drawRect都无法帮助我:(
谢谢!
目前我正在使用CIDetector来检测UIImage中的矩形.我正在做的建议方法是将坐标传递给过滤器以获取CIImage以放置所采用的UIImage.它看起来像这样:
func performRectangleDetection(image: UIKit.CIImage) -> UIKit.CIImage? {
var resultImage: UIKit.CIImage?
let detector:CIDetector = CIDetector(ofType: CIDetectorTypeRectangle, context: nil, options: [CIDetectorAccuracy : CIDetectorAccuracyHigh])
// Get the detections
let features = detector.featuresInImage(image)
for feature in features as! [CIRectangleFeature] {
resultImage = self.drawHighlightOverlayForPoints(image, topLeft: feature.topLeft, topRight: feature.topRight,
bottomLeft: feature.bottomLeft, bottomRight: feature.bottomRight)
}
return resultImage
}
func drawHighlightOverlayForPoints(image: UIKit.CIImage, topLeft: CGPoint, topRight: CGPoint,
bottomLeft: CGPoint, bottomRight: CGPoint) -> UIKit.CIImage {
var overlay = UIKit.CIImage(color: CIColor(red: 1.0, green: 0.55, blue: 0.0, alpha: 0.45))
overlay = overlay.imageByCroppingToRect(image.extent) …Run Code Online (Sandbox Code Playgroud) 我正在使用此代码使按钮的两个角变圆.
let buttonPath = UIBezierPath(roundedRect: button.bounds,
byRoundingCorners: .TopLeft | .BottomLeft,
cornerRadii: CGSizeMake(1.0, 1.0))
Run Code Online (Sandbox Code Playgroud)
它抛出一个错误:
二元运算符'|' 不能应用于两个UIRectCorner操作数.
如何在Swift 2.0中使用此方法?
我正在尝试动画CALayer从正常角落到圆角的过渡.我只想绕过顶角,所以我使用的是UIBezierPath.这是代码:
UIRectCorner corners = UIRectCornerTopLeft | UIRectCornerTopRight;
UIBezierPath* newPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(8.0f, 8.0f)];
UIBezierPath* oldPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(0.0f, 0.0f)];
CAShapeLayer* layer = [CAShapeLayer layer];
layer.frame = self.bounds;
layer.path = oldPath.CGPath;
self.layer.mask = layer;
CABasicAnimation* a = [CABasicAnimation animationWithKeyPath:@"path"];
[a setDuration:0.4f];
[a setFromValue:(id)layer.path];
[a setToValue:(id)newPath.CGPath];
layer.path = newPath.CGPath;
[layer addAnimation:a forKey:@"path"];
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,角落是圆形的,但是没有动画 - 它会立即发生.我在这里看到了一些类似的问题,但它们并没有解决我的问题.
我敢肯定,我只是做了一件小问题导致了这个问题,但对于我的生活,我无法理解.
解:
- (void)roundCornersAnimated:(BOOL)animated {
UIRectCorner corners = UIRectCornerTopLeft | UIRectCornerTopRight;
UIBezierPath* newPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(8.0f, 8.0f)];
UIBezierPath* …Run Code Online (Sandbox Code Playgroud) 我无法弄明白......我正在玩
-[UIBezierPath bezierPathWithRoundedRect:byRoundingCorners:cornerRadii:] 因此:
bezierPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 10, 80, 80)
byRoundingCorners:(UIRectCornerBottomLeft)
cornerRadii:CGSizeMake(20, 20)];
Run Code Online (Sandbox Code Playgroud)
它按预期工作.但是,如果我用,比方说,cornerRadii:CGSizeMake(20, 5)或者替换cornerRadii:CGSizeMake(20,20)CGSizeMake(20, 40),则没有区别.
为什么是cornerRadii CGSize而不是CGFloat呢?什么是CGSize.height?
任何想法和建议将不胜感激:)

目前,我正在尝试设计如何设置一个自定义按钮的动画,我有一个图像并具有坐标,但我发现你可以通过使用CGPath类或UIBezierPath创建一个按钮/对象类.有人可以告诉我两者之间有什么区别吗?
我正在使用UIBezierPath让我的imageview有圆角,但我也想为imageview添加边框.请记住,顶部是uiimage,底部是标签.
目前使用此代码生成:
let rectShape = CAShapeLayer()
rectShape.bounds = myCell2.NewFeedImageView.frame
rectShape.position = myCell2.NewFeedImageView.center
rectShape.path = UIBezierPath(roundedRect: myCell2.NewFeedImageView.bounds,
byRoundingCorners: .TopRight | .TopLeft,
cornerRadii: CGSize(width: 25, height: 25)).CGPath
myCell2.NewFeedImageView.layer.mask = rectShape
Run Code Online (Sandbox Code Playgroud)

我想为此添加绿色边框,但我无法使用
myCell2.NewFeedImageView.layer.borderWidth = 8
myCell2.NewFeedImageView.layer.borderColor = UIColor.greenColor().CGColor
Run Code Online (Sandbox Code Playgroud)
因为它会切断边框的左上角和右上角,如下图所示:

有没有办法添加UIBezierPath边框以及我当前的代码?
uibezierpath ×10
ios ×8
swift ×4
cgpath ×2
objective-c ×2
calayer ×1
cashapelayer ×1
cidetector ×1
cocoa-touch ×1
drawing ×1
geometry ×1
iphone ×1
uikit ×1