我正在尝试向我的CAShapeLayer
路径添加子路径,但除非我首先分配nil
到路径然后重新分配myPath
到CAShapeLayer
. 我已经尝试过setNeedsRedisplay
但它不起作用。
LoadView
这是wheremyPath
和are 属性中的代码shapeLayer
:
// The CGMutablePathRef
CGPathMoveToPoint(myPath, nil, self.view.center.x, self.view.center.y);
CGPathAddLineToPoint(myPath, nil, self.view.center.x + 100.0, self.view.center.y - 100.0);
// The CAShapeLayer
shapeLayer = [CAShapeLayer layer];
shapeLayer.path = myPath;
shapeLayer.strokeColor = [UIColor greenColor].CGColor;
shapeLayer.lineWidth = 2.0;
[self.view.layer addSublayer:shapeLayer];
Run Code Online (Sandbox Code Playgroud)
例如,这是我对路径进行更改的地方。我只是添加一个新的子路径myPath
:
- (void)handleOneFingerSingleTapGesture:(UIGestureRecognizer *)sender
{
// I add a new subpath to 'myPath'
CGPathMoveToPoint(myPath, nil, self.view.center.x, self.view.center.y);
CGPathAddLineToPoint(myPath, nil, self.view.center.x + 100.0, self.view.center.y + 100.0); …
Run Code Online (Sandbox Code Playgroud) 我正在使用 CAShapeLayer 绘制一个半圆,我希望在开头有一个 kCGLineCapRound ,在结尾有一个 kCGLineCapButt 。我怎样才能做到这一点?
UIBezierPath *circlePathMax = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.view.center.x, self.view.center.y) radius:radius startAngle:angle1 endAngle:angle2 clockwise:YES];
CAShapeLayer *circleMax;
circleMax = [CAShapeLayer layer];
circleMax.path = circlePathMax.CGPath;
circleMax.lineCap = kCALineCapRound;
circleMax.fillColor = [UIColor clearColor].CGColor;
circleMax.lineWidth = 10;
circleMax.strokeColor = [UIColor colorWithRed:255.0/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:0.7f].CGColor;
circleMax.zPosition = 3;
[self.view.layer addSublayer:circleMax];
Run Code Online (Sandbox Code Playgroud)
我只能指定一个通用 lineCap
我已经为进度条实现了一个基本的线条绘制动画。线条有一个圆形的帽边缘。当我移动滑块时,线条的末端绘制的线条有一个圆形的帽,但在开始时它是一个正方形。下面是我正在使用的代码,我错过了什么吗??
附图片:预期输出
下面的代码我试过
@interface ViewController ()
{
CGPoint center;
CGFloat radius;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
center = CGPointMake(200.0, 200.0);
self.view.layer.backgroundColor = [[UIColor grayColor] CGColor];
}
- (IBAction)greenSlider:(UISlider *)sender {
[self.view.layer addSublayer:[self drawArcAtCenter:center startAngle:sender.minimumValue endAngle:sender.value radius:100.0 withColor:[UIColor greenColor]]];
}
- (IBAction)blueSlider:(UISlider *)sender {
int value = (int)sender.value;
if (value > 360) {
value = value % 90;
}
[self.view.layer addSublayer:[self drawArcAtCenter:center startAngle:sender.minimumValue endAngle:(float)value radius:100 withColor:[UIColor blueColor]]];
}
- (CAShapeLayer *) drawArcAtCenter:(CGPoint)newCenter startAngle:(CGFloat)sAngle endAngle:(CGFloat)bAngle radius:(CGFloat) radious …
Run Code Online (Sandbox Code Playgroud) 当我使用CAShapeLayer
并创建矩形形状时,路径从矩形的原点(左上角)开始并顺时针绘制。现在,如果我只想绘制形状的一部分,那么我将使用strokeStart
和strokeEnd
属性。当我想绘制由路径端点组成的部分时,问题就出现了。在这种情况下,路径是闭合的,并且它在矩形的左上角开始和结束。当我设置时strokeStart=0.8
,strokeEnd=0.2
我希望它能够绘制路径的最后一部分以及从路径的开头开始的一点。然而,这是行不通的。有什么想法或技巧可以做到这一点吗?
更新:
我正在尝试使用 CASpringAnimation 对 CAShapeLayer 路径进行动画处理。预期的结果是形状之间的“变形”,表现出“弹性”。
我有一个圆形和方形路径之间的基本代码示例,如下所示,但最终结果是一个弹簧动画,它不会“超出”最终的较大方形路径,这是预期的行为。
我的代码是:
let springAnimation = CASpringAnimation(keyPath: "path")
springAnimation.damping = 1
springAnimation.duration = springAnimation.settlingDuration
springAnimation.fromValue = standardCirclePath().cgPath
springAnimation.toValue = standardSquarePath().cgPath
circleLayer.add(springAnimation, forKey: nil) // Where circleLayer (red background) is a sublayer of a basic UIView in the frame (blue background)
Run Code Online (Sandbox Code Playgroud)
我从这个答案中得到了我的道路。
有没有办法使用 CASpringAnimation 来实现 CAShapeLayer 路径转换?否则,还有什么替代方案?
我画了一个简单的圆形进度视图,如下所示:
let trackLayer:CAShapeLayer = CAShapeLayer()
let circularPath:UIBezierPath = UIBezierPath(arcCenter: CGPoint(x: progressView.frame.width / 2, y: 39.5), radius: progressView.layer.frame.size.height * 0.4, startAngle: -CGFloat.pi / 2, endAngle: 2 * CGFloat.pi, clockwise: true)
trackLayer.path = circularPath.cgPath
trackLayer.strokeColor = UIColor.lightGray.cgColor
trackLayer.lineWidth = 6
trackLayer.fillColor = UIColor.clear.cgColor
trackLayer.lineCap = kCALineCapRound
progressView.layer.insertSublayer(trackLayer, at: 0)
let progressLayer:CAShapeLayer = CAShapeLayer()
progressLayer.path = circularPath.cgPath
progressLayer.strokeColor = UIColor.blue.cgColor
progressLayer.lineWidth = 6
progressLayer.fillColor = UIColor.clear.cgColor
progressLayer.lineCap = kCALineCapSquare
progressLayer.strokeEnd = 0
progressView.layer.insertSublayer(progressLayer, above: trackLayer)
Run Code Online (Sandbox Code Playgroud)
在某个事件中,我尝试更新progressLayer
:
if let sublayers = progressView.layer.sublayers …
Run Code Online (Sandbox Code Playgroud) 我正在尝试将自定义形状添加到imageView
. 请检查以下图片。
这是必需的:
这是我到目前为止所做的:
我是新手,Core Graphics
到目前为止我已经做到了:
private func customImageClipper(imageV: UIImageView){
let path = UIBezierPath()
let size = imageV.frame.size
print(size)
path.move(to: CGPoint(x: 0.0, y: size.height))
path.addLine(to: CGPoint(x: 0.8, y: size.height/2))
path.close()
let shape = CAShapeLayer()
shape.path = path.cgPath
imageV.layer.sublayers = [shape]
}
Run Code Online (Sandbox Code Playgroud)
我正在创建一个函数来实现这样的形状,但是每当我将 传递给imageView
这个函数时,我根本看不到任何变化。我知道我必须从一个点移动到另一个点才能达到这个形状,但我从来没有这样做过。任何帮助,将不胜感激。这就是我调用这个函数的方式:
imageV.layoutIfNeeded()
customImageClipper(imageV: imageV)
Run Code Online (Sandbox Code Playgroud)
PS:我没有使用Storyboard
,我以编程方式创建了这个。
感谢StackOverflow的一些帮助,我目前正在动画CAShapeLayer中的一个路径来制作一个三角形,从移动的精灵指向屏幕上的另一个移动点.
动画完成后,三角形将从屏幕上消失.我使用的持续时间非常短,因为每个精灵每秒.1秒会触发此代码.结果是红色三角形轨道正确,但它快速闪烁或完全没有.当我加速持续时间时,我可以看到三角形停留的时间更长.
我可以做些什么来让三角形保持在屏幕上,使用它的当前路径(tovalue
),直到再次调用该方法从该点到下一个点的动画?我尝试设置removedOnCompletion和removeAllAnimations,但无济于事.
代码如下:
-(void)animateConnector{
//this is the code that moves the connector from it's current point to the next point
//using animation vs. position or redrawing it
//set the newTrianglePath
newTrianglePath = CGPathCreateMutable();
CGPathMoveToPoint(newTrianglePath, nil, pointGrid.x, pointGrid.y);//start at bottom point on grid
CGPathAddLineToPoint(newTrianglePath, nil, pointBlob.x, (pointBlob.y - 10.0));//define left vertice
CGPathAddLineToPoint(newTrianglePath, nil, pointBlob.x, (pointBlob.y + 10.0));//define the right vertice
CGPathAddLineToPoint(newTrianglePath, nil, pointGrid.x, pointGrid.y);//close the path
CGPathCloseSubpath(newTrianglePath);
//NSLog(@"PointBlob.y = %f", pointBlob.y);
CABasicAnimation *connectorAnimation = [CABasicAnimation animationWithKeyPath:@"path"];`enter code here` …
Run Code Online (Sandbox Code Playgroud) 我有一个CAShapeLayer,它必须做一个简单的任务,在用户的手指指导下移动屏幕.
问题是运动太慢了.该层确实移动,但有一个滞后,感觉很慢.
我有另一个测试应用程序,其中移动UIImage并且没有任何延迟,图像立即移动.
我该怎么做才能克服这个问题?
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { currentPoint = [[touches anyObject] locationInView:self]; } - (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event { CGPoint activePoint = [[touches anyObject] locationInView:self]; CGPoint newPoint = CGPointMake(activePoint.x - currentPoint.x,activePoint.y - currentPoint.y); curLayer.position = CGPointMake(shapeLayer.position.x+newPoint.x,shapeLayer.position.y+newPoint.y); currentPoint = activePoint; }
谢谢!
我正在尝试使用下面的代码来绕顶角
func roundCorners(corners:UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: self.bounds,
byRoundingCorners: corners,
cornerRadii: CGSize(width: radius, height: radius))
let maskLayer = CAShapeLayer()
maskLayer.frame = self.bounds
maskLayer.path = path.cgPath
self.layer.mask = maskLayer
}
Run Code Online (Sandbox Code Playgroud)
使用 myView.roundCorners(corners:[.topLeft, .topRight], radius: radius)
这是在tableView sectionHeader中,如果我向下滚动然后使用相同的代码向上舍入:
顶部折扣视角也使用相同的功能进行四舍五入.
感谢帮助.
更新 如果我修复视图上的宽度然后它工作正常.
uitabbarcontroller uitableview cashapelayer uibezierpath swift
cashapelayer ×10
ios ×6
swift ×3
uibezierpath ×3
animation ×2
iphone ×2
calayer ×1
cgpath ×1
ios9 ×1
objective-c ×1
redraw ×1
spring ×1
uikit ×1
uitableview ×1