我试图通过使用CAShapeLayer并在其上设置圆形路径来绘制一个描边圆.但是,这种方法在渲染到屏幕时的效果始终低于使用borderRadius或直接在CGContextRef中绘制路径.
以下是所有三种方法的结果:
请注意,第三个渲染效果不佳,尤其是在顶部和底部的笔划内.
我已将该contentsScale
属性设置为[UIScreen mainScreen].scale
.
这是我对这三个圆圈的绘图代码.使CAShapeLayer顺利绘制的缺失是什么?
@interface BCViewController ()
@end
@interface BCDrawingView : UIView
@end
@implementation BCDrawingView
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame])) {
self.backgroundColor = nil;
self.opaque = YES;
}
return self;
}
- (void)drawRect:(CGRect)rect
{
[super drawRect:rect];
[[UIColor whiteColor] setFill];
CGContextFillRect(UIGraphicsGetCurrentContext(), rect);
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), NULL);
[[UIColor redColor] setStroke];
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1);
[[UIBezierPath bezierPathWithOvalInRect:CGRectInset(self.bounds, 4, 4)] stroke];
}
@end
@interface BCShapeView : UIView
@end
@implementation BCShapeView
+ (Class)layerClass
{
return [CAShapeLayer class];
} …
Run Code Online (Sandbox Code Playgroud) 我有一个CAShapeLayer(它是UIView子类的一层),path
只要视图的bounds
大小发生变化,它就会更新.为此,我重写了图层setBounds:
重置路径的方法:
@interface CustomShapeLayer : CAShapeLayer
@end
@implementation CustomShapeLayer
- (void)setBounds:(CGRect)bounds
{
[super setBounds:bounds];
self.path = [[self shapeForBounds:bounds] CGPath];
}
Run Code Online (Sandbox Code Playgroud)
这工作正常,直到我为边界变化设置动画.我希望在任何动画边界变化(在UIView动画块中)中创建路径动画,以便形状图层始终采用其大小.
由于该path
属性默认没有动画,我想出了这个:
覆盖图层的addAnimation:forKey:
方法.在其中,确定是否将边界动画添加到图层.如果是这样,通过复制传递给方法的边界动画中的所有属性,创建第二个显式动画,以便为边界旁边的路径设置动画.在代码中:
- (void)addAnimation:(CAAnimation *)animation forKey:(NSString *)key
{
[super addAnimation:animation forKey:key];
if ([animation isKindOfClass:[CABasicAnimation class]]) {
CABasicAnimation *basicAnimation = (CABasicAnimation *)animation;
if ([basicAnimation.keyPath isEqualToString:@"bounds.size"]) {
CABasicAnimation *pathAnimation = [basicAnimation copy];
pathAnimation.keyPath = @"path";
// The path property has not been updated to the new value yet
pathAnimation.fromValue = (id)self.path;
// …
Run Code Online (Sandbox Code Playgroud) 有没有人有任何将渐变应用于CAShapeLayer的经验?CAShapeLayer是一个很棒的图层类,但它似乎只支持实心填充着色,而我希望它有一个渐变填充(实际上是一个可动画的渐变).
与CAShapeLayer(阴影,形状,笔触颜色,动画形状路径)相关的所有其他内容都非常棒.
我已经尝试将CAGradientLayer放在CAShapeLayer中,或者确实将CAShapeLayer设置为GradientLayer的掩码并将两者都添加到容器层,但这些都没有正确的结果.
我应该继承CAShapeLayer,还是有更好的前进方向?
谢谢.
在诸如如何绘制一个平滑的圆圈......,...绘制圆...和...绘制填充圆圈的问题和答案是非常广泛的,包含许多不必要的步骤,并使用的方法并不总是最容易重新创建或管理.
什么是绘制圆圈并将其添加到我的简单方法UIView
?
我设计了这个代码来生成一个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
都无法帮助我:(
谢谢!
如何为CAShapeLayer路径和fillColor设置动画?
如何为strokeEnd设置动画以显示正在绘制的路径?以及如何为fillColor设置动画?
我已经能够完成我想要完成的任务,那就是复制iOS内置的圆形照片裁剪器,用于内置的联系人应用程序.但是,我一直在试图让我的CAShapeLayers
制作正确.我正在尝试制作一个透明的320像素直径圆圈,其余部分则填充0.9 alpha黑色背景.圆形和矩形在正确的位置,但是,圆形并不像我需要的那样完全透明.
我迷失了如何解决这个问题.我感谢您的帮助!代码和截图:
- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
if ([navigationController.viewControllers count] == 3)
{
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenHeight = screenRect.size.height;
UIView *plCropOverlay = [[[viewController.view.subviews objectAtIndex:1]subviews] objectAtIndex:0];
plCropOverlay.hidden = YES;
CAShapeLayer *circleLayer = [CAShapeLayer layer];
if (screenHeight == 568)
{
[circleLayer setPosition:CGPointMake(0.0f,124.0f)];
}
else
{
[circleLayer setPosition:CGPointMake(0.0f,80.0f)];
}
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:
CGRectMake(0.0f, 0.0f, 320.0f, 320.0f)];
[circleLayer setPath:[path CGPath]];
[circleLayer setFillColor:[[UIColor whiteColor] CGColor]];
circleLayer.opacity = 0.7f;
// Set to 0.7f to show …
Run Code Online (Sandbox Code Playgroud) 简而言之:
frame
或bounds
为CAShapeLayer
(并且Apple未实现等效[UIView sizeThatFits]
)那么,什么是编程设定的新创建的框架的正确方法CAShapeLayer
与新添加的CGPath
?Apple的文档在此事上保持沉默.
我尝试过的东西,不起作用:
CAShapeLayer
CGPath
,将其添加到图层frame
- 它是{{0,0},{0,0}}
layer.frame = CGPathGetBoundingBox( layer.path )
帧现在是正确的,但路径现在是DOUBLE偏移 - 改变frame
路径有效地移动一个额外的(x,y)
像素
组: layer.bounds = CGPathGetBoundingBox( layer.path )
layer.position = CGPathGetBoundingBox( layer.path ).origin
有一件事我尝试过DID可以工作,但会导致其他地方出现问题:
编辑:一旦您自动旋转屏幕,这个BREAKS.我的猜测:Apple的自动旋转需要控制"转换"属性.
CAShapeLayer
CGPath
,将其添加到图层{{0,0},{0,0}}
layer.frame = CGPathGetBoundingBox( layer.path )
layer.transform = CGAffineTransformMakeTranslation( CGPathGetBoundingBox( …
我正在绘制一个圆,初始半径为200
self.circle = [CAShapeLayer layer];
self.circle.fillColor = nil;
self.circle.strokeColor = [UIColor blackColor].CGColor;
self.circle.lineWidth = 7;
self.circle.bounds = CGRectMake(0, 0, 2 * radius, 2 * radius);
self.circle.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.radius, self.radius)
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我如何将变化动画为半径100?
我创建了UIView
一个半透明的黑色背景,可以看到我的主应用程序视图.我在这里的目的是在一个UIView
没有看到半透明黑色背景的地方创建一个切出的圆形状,除了圆形区域外,在整个图像上产生黑色遮罩的效果(这是为了突出显示)该区域的按钮).
我已经设法用来CAShapeLayer
创建一个圆形面具,但这有相反的效果(即视图的其余部分是清晰的,并且圆圈内部是半透明的黑色.我想要的是除了圆圈保持半透明的黑色,然后内部清楚.这是我使用的代码,我将如何使其以相反的方式工作?我blackMask
是半透明的黑色视图,我buttonCircle
是圆圈我我希望保持清醒.
也许我需要以某种方式颠倒路径?
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
CGRect maskRect = buttonCircle.frame;
CGPathRef path = CGPathCreateWithEllipseInRect(maskRect, NULL);
maskLayer.path = path;
CGPathRelease(path);
blackMask.layer.mask = maskLayer;
Run Code Online (Sandbox Code Playgroud)
编辑:现在尝试这个,没有看到任何掩码与这一个:
UIView *circularMaskView = [[UIView alloc] initWithFrame:buttonCircle.frame];
circularMaskView.backgroundColor = [UIColor greenColor];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
CGPathRef path = CGPathCreateWithEllipseInRect(buttonCircle.frame, NULL);
maskLayer.path = path;
CGPathRelease(path);
circularMaskView.layer.mask = maskLayer;
[blackMask addSubview:circularMaskView];
Run Code Online (Sandbox Code Playgroud) cashapelayer ×10
ios ×8
objective-c ×5
calayer ×2
cgpath ×2
iphone ×2
uibezierpath ×2
animation ×1
gradient ×1
ios7 ×1
swift ×1
uikit ×1
uiview ×1