我有一个UICollectionViewCell子类,我需要绕其角并添加阴影.该单元看起来像一张方形卡,单元格之间有足够的空间.
因此,在每个单元格"下面",我想添加一些阴影.我可以成功地做到这一点,但后来我的手机底部只有圆角.顶部只有正常的角落.我需要所有四个角的圆角.
我已经在这里找到了解决方案UIViews,建议添加一个单独UIView的subview,但我更愿意出于性能原因避免这种情况.
我找到了一个使用此方法的解决方案,您可以在我的代码中找到:
[UIBezierPath bezierPathWithRoundedRect: cornerRadius:]
但这对我来说也不起作用.是否有可能它不适合我,因为我试图只在单元格的底部添加阴影"下面"?似乎大多数这些答案都是针对开发人员想要在整个单元格周围添加阴影的问题提供的.
我想我愿意为subview我的UICollectionViewCell子类添加一个特殊的,但我想将它作为最后的手段.
我的目标iOS 7+和使用Xcode 6.1.1.
这是我在UICollectionViewCell子类中使用的代码,用于尝试实现阴影和圆角:
- (void)load:(CustomUserObject *)customObject
{
self.customObject = customObject;
// Round cell corners
self.layer.cornerRadius = 12;
// Add shadow
self.layer.masksToBounds = NO;
self.layer.shadowOpacity = 0.75f;
self.layer.shadowRadius = 10.0f;
self.layer.shouldRasterize = NO;
self.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(self.frame.size.width/2 - (self.frame.size.width - 50)/2, self.frame.size.height, self.frame.size.width - 50, 10) cornerRadius:self.layer.cornerRadius].CGPath;
}
Run Code Online (Sandbox Code Playgroud)
编辑:如果我设置self.layer.masksToBounds到NO,影子工程,但顶角不圆.如果我设置self.layer.masksToBounds …
在SpriteKit中,clockwise方向相反,UIBezierPath但不是CGPath.例如,如果我有
do {
let path = CGPathCreateMutable()
CGPathAddArc(path, nil, 0, 0, 10, 0, CGFloat(M_PI_2), true)
let node = SKShapeNode(path: path)
node.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
self.addChild(node)
}
do {
let path = UIBezierPath()
path.addArcWithCenter(CGPoint(x: 0, y: 0), radius: 10, startAngle: 0, endAngle: CGFloat(M_PI_2), clockwise: true)
let node = SKShapeNode(path: path.CGPath)
node.position = CGPoint(x: self.size.width/2, y: self.size.height/2 - 100)
self.addChild(node)
}
Run Code Online (Sandbox Code Playgroud)
在GameScene.didMoveToView(view: SKView),第一个节点绘制一个3/4弧形,右上角缺失,但第二个节点在右上角绘制四分之一弧.在这篇文章中UIBezierPath解释了相反的"顺时针"方向,但为什么CGPath表现不一样?不UIBezierPath只是一个包装 …
Hej研究员,
我正在尝试将一个字母和/或多个字母转换为CGPathRef,以便手动将它们绘制到自定义UIView中.我尝试过CoreText和Framesetters,包括这个小片段,但它似乎不起作用....
NSAttributedString *stringToDraw = [[NSAttributedString alloc] initWithString:content
attributes:nil];
// now for the actual drawing
CGContextRef context = UIGraphicsGetCurrentContext();
// flip the coordinate system
// draw
CTFramesetterRef frameSetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)stringToDraw);
CTFrameRef frame = CTFramesetterCreateFrame(frameSetter, CFRangeMake(0, 0), NULL, NULL);
return CTFrameGetPath(frame);
Run Code Online (Sandbox Code Playgroud) 我制作了一个简单的基于iOS View的应用程序,可以让你在屏幕上绘制和擦除.我正在使用NSMutableArray存储已绘制的路径.每次我将控件从标记切换到橡皮擦或反之,我将当前的CGPath添加到数组并创建一个新的.
每次在drawRect中我都会用适当的颜色重绘数组的路径,具体取决于它是橡皮擦的路径还是标记的路径
并且当触摸移动时绘制当前正在绘制的那个.
现在我知道这并不是一个很好的解决方案,随着阵列规模的增长会占用大量的RAM.我的数组已经包含多余的路径,这些路径实际上已经过了已经用相同颜色着色的点,并且不必要地花费处理器的时间来再次处理它和内存.
任何人都可以参考更好的算法来节省资源吗?
我正在制作一些自定义控件.控件的其中一个子视图是透明的,UIView其中UIBezierPath绘制了路径.在其中一张照片上,我们可以说是UIBezierPath(这里称为[路径描边])的边界,但是在第二张照片上你可以看到当我调用[路径填充]时会发生什么.我希望填充的路径可以创建类似于第一张照片上的形状.drawRect这个透明的UIView方法如下.
- (void)drawRect:(CGRect)rect
{
// Drawing code
[super drawRect:rect];
UIBezierPath *path;
[[UIColor greenColor] setStroke];
[[UIColor greenColor] setFill];
//Angles
CGFloat startAngle = degreesToRadians(225);
CGFloat endAngle = degreesToRadians(315);
CGPoint center = CGPointMake(CGRectGetMidX(rect), lRadius);
path = [UIBezierPath bezierPathWithArcCenter: center radius:lRadius startAngle:startAngle endAngle:endAngle clockwise:YES];
rightEndOfLine = path.currentPoint;
bigArcHeight = rightEndOfLine.y;
CGFloat smallArcHeight = lRadius - bigArcHeight - sRadius;
CGFloat smallArcWidthSquare = (8*smallArcHeight*sRadius) - (4 * (smallArcHeight * smallArcHeight));
smallArcWidth = sqrtf(smallArcWidthSquare);
leftStartOfLine = CGPointMake((rect.size.width …Run Code Online (Sandbox Code Playgroud) 我目前正在使用此代码为角色的绘制设置动画:
var path = UIBezierPath()
var unichars = [UniChar]("J".utf16)
var glyphs = [CGGlyph](count: unichars.count, repeatedValue: 0)
let gotGlyphs = CTFontGetGlyphsForCharacters(font, &unichars, &glyphs, unichars.count)
if gotGlyphs {
let cgpath = CTFontCreatePathForGlyph(font, glyphs[0], nil)
path = UIBezierPath(CGPath: cgpath!)
}
Run Code Online (Sandbox Code Playgroud)
从字符创建bezierpath(在本例中为"J").获取跟踪iOS UIFont中的角色的路径
然后我创建一个CAShapeLayer()并添加动画.
let layer = CAShapeLayer()
layer.position = //CGPoint
layer.bounds = //CGRect()
view.layer.addSublayer(layer)
layer.path = path.CGPath
layer.lineWidth = 5.0
layer.strokeColor = UIColor.blackColor().CGColor
layer.fillColor = UIColor.clearColor().CGColor
layer.geometryFlipped = true
layer.strokeStart = 0.0
layer.strokeEnd = 1.0
let anim = CABasicAnimation(keyPath: "strokeEnd") …Run Code Online (Sandbox Code Playgroud) 我想在iPhone屏幕上存储手指移动的路径.截至目前,我只是阅读触摸并向NSMutableArray添加CGPoints.当我尝试打印该数组中的所有cgpoint时,它是如何缺少中间点的.有更好的方法吗?我们可以直接存储整条路径吗?
这是我正在使用的代码
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
fingerSwiped = NO;
UITouch *touch = [touches anyObject];
lastPoint = [touch locationInView:self.view];
[self.myPoints addObject:[NSValue valueWithCGPoint:lastPoint]];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
fingerSwiped = YES;
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
UIGraphicsBeginImageContext(self.view.frame.size);
[slateImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound);
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), lineWidth);
//CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),0,0,0, 1.0);
CGContextSetStrokeColorWithColor(UIGraphicsGetCurrentContext(), self.drawcolor.CGColor);
CGContextBeginPath(UIGraphicsGetCurrentContext());
CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
CGContextStrokePath(UIGraphicsGetCurrentContext());
slateImage.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
lastPoint = currentPoint;
[myPoints addObject:[NSValue valueWithCGPoint:lastPoint]];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent …Run Code Online (Sandbox Code Playgroud) 如何为 UIView 的 4 个角中的每个角设置完全不同的圆角半径?
UIBezierPath 允许我为一个或多个特定角设置 ONE 值,但不能为每个角设置不同的值。
我认为从理论上讲,自定义 CGPath 应该是可能的,但我无法实现它。
我正在使用CGMutablePath绘制 wifi 徽标。当我绘制第二条弧 by 时 addArc(center:radius:startAngle:endAngle:clockwise:transform:),它会自动绘制一条与指定文档相同的线If the path already contains a subpath, this method adds a line connecting the current point to the starting point of the arc.。没有这条线我怎么画?谢谢
我想添加文本,无论是 aUILabel还是CATextLayera CGPath。我意识到此功能背后的数学相当复杂,但想知道 Apple 是否提供了开箱即用的此功能,或者是否有开源 SDK 可以在 Swift 中实现这一点。谢谢!
cgpath ×10
ios ×7
swift ×3
calayer ×2
iphone ×2
objective-c ×2
uibezierpath ×2
catextlayer ×1
cgpathref ×1
cgpoint ×1
cocoa ×1
cocoa-touch ×1
core-text ×1
drawing ×1
sprite-kit ×1
touchesmoved ×1
uikit ×1
uilabel ×1