标签: cashapelayer

使用波动画动画CAShapeLayer

下面我有附加图像,我想实现下面的动画.我尝试过水波动画,但不知道如何像上面那样控制动画.

我必须CAShapeLayer在其中实现此动画.

在此输入图像描述

初始代码

UIBezierPath *leftPath = [UIBezierPath bezierPath];
// Set the starting point of the shape.
[leftPath moveToPoint:CGPointMake(0,self.bounds.size.height/2)];
// Draw the lines.


[leftPath addLineToPoint:CGPointMake(0,self.bounds.size.height/2)];
[leftPath addLineToPoint:CGPointMake(self.bounds.size.width,self.bounds.size.height/2)];


leftLayer.path = leftPath.CGPath;
leftLayer.strokeColor = [[UIColor whiteColor] CGColor];
leftLayer.fillColor = nil;
leftLayer.borderWidth = 3.0f;
leftLayer.lineCap = kCALineCapRound;
leftLayer.lineJoin = kCALineJoinRound;

leftLayer.borderColor=[UIColor blackColor].CGColor;
[self.layer addSublayer:leftLayer];
Run Code Online (Sandbox Code Playgroud)

动画代码

-(void)animateCureve{

CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
pathAnimation.duration = 3.5;
pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
pathAnimation.fromValue = (id)leftLayer.path;
pathAnimation.toValue = (id)[self wavePath].CGPath;
pathAnimation.removedOnCompletion=NO;
[leftLayer addAnimation:pathAnimation forKey:@"path"];
}
Run Code Online (Sandbox Code Playgroud)

曲线路径

- …
Run Code Online (Sandbox Code Playgroud)

animation cashapelayer ios uibezierpath

7
推荐指数
1
解决办法
1699
查看次数

CABasicAnimation - 设置起始行程位置

我正在绘制一个基本圆圈的动画.这很好用,除了动画开始在3点位置绘制.有谁知道我怎么能在12点开始?

self.circle = [CAShapeLayer layer];
self.circle.fillColor = nil;
self.circle.lineWidth = 7;
self.circle.strokeColor = [UIColor blackColor].CGColor;
self.circle.bounds = CGRectMake(0, 0, 200, 200);
self.circle.path = [UIBezierPath bezierPathWithOvalInRect:self.circle.bounds].CGPath;
[self.view.layer addSublayer:self.circle];

CABasicAnimation *drawAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
drawAnimation.duration            = 5.0;
drawAnimation.repeatCount         = 1.0;
drawAnimation.removedOnCompletion = NO;
drawAnimation.fromValue = [NSNumber numberWithFloat:0.0f];
drawAnimation.toValue   = [NSNumber numberWithFloat:1.0f];
drawAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
[self.circle addAnimation:drawAnimation forKey:@"drawCircleAnimation"];
Run Code Online (Sandbox Code Playgroud)

objective-c cabasicanimation cashapelayer ios

6
推荐指数
1
解决办法
3030
查看次数

在UIButton中创建三角形

我正在尝试创建一个带有三角形图标/形状的按钮.我创建了三角形Paintcode然后复制了beizer路径并将其添加到按钮层,如下所示.

-(void)setupShowHideRouteButton {
    UIBezierPath* bezierPath = UIBezierPath.bezierPath;
    [bezierPath moveToPoint: CGPointMake(10.5, 8.5)];
    [bezierPath addCurveToPoint: CGPointMake(40.5, 8.5) controlPoint1: CGPointMake(39.5, 8.5) controlPoint2: CGPointMake(40.5, 8.5)];
    [bezierPath addLineToPoint: CGPointMake(26.39, 22.3)];
    [bezierPath addLineToPoint: CGPointMake(25.2, 23.5)];
    [bezierPath addLineToPoint: CGPointMake(10.5, 8.5)];
    [UIColor.blackColor setStroke];
    bezierPath.lineWidth = 1;
    [bezierPath stroke];

    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = self.showHideRouteViewBtn.bounds;
    shapeLayer.path = bezierPath.CGPath;
    shapeLayer.fillColor = [UIColor clearColor].CGColor;
    shapeLayer.strokeColor = [UIColor blackColor].CGColor;
    shapeLayer.lineWidth = 2;
    [self.showHideRouteViewBtn.layer addSublayer:shapeLayer];
}
Run Code Online (Sandbox Code Playgroud)

但是,这似乎不起作用.我正在设置UIButton的背景颜色,所以我知道框架是正确的,并且插座按预期工作,它只是没有添加形状?

第二种方法

UIBezierPath* bezierPath = UIBezierPath.bezierPath;
[bezierPath moveToPoint: CGPointMake(10.5, 8.5)];
[bezierPath addCurveToPoint: CGPointMake(40.5, …
Run Code Online (Sandbox Code Playgroud)

uibutton uiview cashapelayer ios uibezierpath

6
推荐指数
1
解决办法
3092
查看次数

如何在swift中将多个CAShapeLayers合并为一个

我有多个bezier路径,它们被整合到CAShapeLayers中用于动画目的.我需要知道是否可以将所有图层合并到一个集合对象中,以便我可以按以下方式对其进行缩放:

shapes.transform = CATransform3DMakeScale(2, 2, 1)
Run Code Online (Sandbox Code Playgroud)

有人知道怎么做这个吗?

scale cashapelayer ios swift

6
推荐指数
0
解决办法
909
查看次数

UIView图层蒙版动画

我试图在UIView上设置遮罩层的动画.

基本上这段代码显示如下图片:

let bounds: CGRect = self.manualWBMaskView!.bounds
let maskLayer: CAShapeLayer = CAShapeLayer()
maskLayer.frame = bounds
maskLayer.fillColor = UIColor.blackColor().CGColor
let screenWith  : CGFloat = UIScreen.mainScreen().bounds.width
let roundedRectFrame : CGRect = CGRectMake(self.manualWBMaskView!.bounds.midX - (screenWith/4), self.manualWBMaskView!.bounds.midY - (screenWith/4), screenWith/2, screenWith/2)
let path: UIBezierPath = UIBezierPath(roundedRect:roundedRectFrame, cornerRadius:10  )
path.appendPath(UIBezierPath(rect: bounds))
maskLayer.path = path.CGPath
maskLayer.fillRule = kCAFillRuleEvenOdd
self.manualWBMaskView!.layer.mask = maskLayer
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我想让它从全屏到以上位置动画到切口的位置:

在此输入图像描述

我试过UIView动画,没有运气.既然我已经有一个CAShapeLayer,我应该能够动画那个?

编辑**

这是我试过的动画代码,它不起作用:

 //Before Animation Make the Mask fill the whole view:
    self.manualWBMaskView!.layer.mask = self.manualWBMaskView!.bounds

    var duration: NSTimeInterval = 0.8

    CATransaction.begin()
    CATransaction[kCATransactionAnimationDuration] = Int(duration) …
Run Code Online (Sandbox Code Playgroud)

mask layer uiview cashapelayer swift

6
推荐指数
1
解决办法
8243
查看次数

如何使用颜色渐变笔划绘制圆形路径

我想在iOS和macOS上绘制一个带有颜色渐变笔画的圆圈,如下图所示:

圆路径

是否可以用/ CAShapeLayer或?还是其他任何方式?NSBezierPathCGPath

macos cashapelayer ios nsbezierpath swift

6
推荐指数
2
解决办法
7744
查看次数

如何获得CAShapeLayer的超级视图位置?

我正在制作圆形图表,我需要在突出显示的圆圈(不是阴影)的末尾添加小视图以指向目标值.我可以根据笔画终点找到一个圆(高亮)超视图x,y位置吗?

UIView->Circle(使用CAShapeLayerUIBezierPath).我需要得到一个UIView基于结束Circle笔画值的位置.

请参考此链接(如带有虚线的23%)http://support.softwarefx.com/media/74456678-5a6a-e211-84a5-0019b9e6b500/large

提前致谢! 需要找到绿色圆圈位置

更新: 我已经尝试了alexburtnik代码,实际上我正在研究目标c中的时钟图,但这不是问题.我试着像alexburtnik提到的那样,我相信它完全适用于反时钟图.我们需要对代码进行一些更改以便为顺时针方向工作,如果您知道,请提供解决方案.

CGFloat radiusCircle = (self.frame.size.height * 0.5) - ([_lineWidth floatValue]/2.0f); 
-(void)addTargetViewWithOptions:(CGFloat)progress andRadius:(CGFloat)radius{

CGFloat x = radius * (1 + (cos(M_PI * (2 * progress + 0.5))));
CGFloat y = radius * (1 - (sin(M_PI * (2 * progress + 0.5))));
UIView *targetView = [[UIView alloc]initWithFrame:CGRectMake(x, y, 40, 30)];
targetView.backgroundColor = [UIColor greenColor];
[self addSubview:targetView];}
Run Code Online (Sandbox Code Playgroud)

请检查我的原始图表

我试着提到了esthepiking,在这里我添加了代码和截图

-(void)addTargetView{

CGFloat endAngle = -90.01f;
radiusCircle = (self.frame.size.height * …
Run Code Online (Sandbox Code Playgroud)

objective-c calayer cashapelayer ios uibezierpath

6
推荐指数
1
解决办法
768
查看次数

如何确保 CAShapeLayer 调整大小以适合 UIView

我目前正在将 MKPolyline 转换为 BezierPath,然后转换为 CAShapeLayer,然后将该图层作为子图层添加到 UIView。目前正在努力确保路径不会绘制在 UIView 的边界之外。我不想遮盖并使部分路径消失,而是确保调整每个点的大小并将其放置在 UIView 的中心。

func addPathToView() {
    guard let path = createPath(onView: polylineView) else { return }
    path.fit(into: polylineView.bounds).moveCenter(to: polylineView.center).fill()
    path.lineWidth     = 3.0
    path.lineJoinStyle = .round

    guard let layer  = createCAShapeLayer(fromBezierPath: path) else { return }
    layer.path       = getScaledPath(fromPath: path, layer: layer)
    layer.frame      = polylineView.bounds
    layer.position.x = polylineView.bounds.minX
    layer.position.y = polylineView.bounds.minY

    polylineView.layer.addSublayer(layer)
}

func createCAShapeLayer( fromBezierPath path: UIBezierPath? ) -> CAShapeLayer? {
    guard let path = path else { print("No Path"); return nil } …
Run Code Online (Sandbox Code Playgroud)

scale polyline cashapelayer uibezierpath swift

6
推荐指数
1
解决办法
2633
查看次数

UIBezierPath - 添加圆角

我用a UIBezierPath画一条曲线.然而,曲线有效,我无法弯曲线的边缘.

在此输入图像描述

如果你看一下曲线的顶端/底端,你可以看到边缘是扁平的,这不是我想要的.有没有办法弯曲边缘?

我用一个简单UIBezierPath的画一个(几乎)半圆.这创建了我想要的曲线形状:

CAShapeLayer *circle = [CAShapeLayer layer];
circle.path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(0, 0) radius:70 startAngle:1.0472 endAngle:5.23599 clockwise:NO].CGPath;
circle.fillColor = [UIColor clearColor].CGColor;
circle.strokeColor = [UIColor colorWithWhite:1.0 alpha:0.2].CGColor;
circle.lineWidth = 18.0;
circle.cornerRadius = 10.0;
circle.position = CGPointMake(100, 100);
circle.anchorPoint = CGPointMake(0.5, 0.5);
[mainView.layer addSublayer:circle];
Run Code Online (Sandbox Code Playgroud)

尽管设置了cornerRadius属性,角落也不会弯曲.我究竟做错了什么?

谢谢你的时间,丹.

objective-c rounded-corners cashapelayer ios uibezierpath

6
推荐指数
1
解决办法
846
查看次数

UIView bounds.applying但有旋转

我想在视图周围创建一个虚线边框,可以移动/旋转/缩放.

这是我的代码:

func addBorder() {        
    let f = selectedObject.bounds.applying(selectedObject.transform)
    borderView.backgroundColor = UIColor(red: 1, green: 0, blue: 0, alpha: 0.5) //just for testing
    borderView.frame = f
    borderView.center = selectedObject.center
    borderView.transform = CGAffineTransform(translationX: selectedObject.transform.tx, y: selectedObject.transform.ty)

    removeBorder() //remove old border

    let f2 = CGRect(x: 0, y: 0, width: borderView.frame.width, height: borderView.frame.height)
    let dashedBorder = CAShapeLayer()
    dashedBorder.strokeColor = UIColor.black.cgColor
    dashedBorder.lineDashPattern = [2, 2]
    dashedBorder.frame = f2
    dashedBorder.fillColor = nil
    dashedBorder.path = UIBezierPath(rect: f2).cgPath
    dashedBorder.name = "border"
    borderView.layer.addSublayer(dashedBorder)
}
Run Code Online (Sandbox Code Playgroud)

它看起来像这样: 在此输入图像描述

这还不错,但我希望边框也可以旋转,因为它可能会误导用户,因为触摸区域仅在图像上.

我试图将旋转应用于变换:

func addBorder() …
Run Code Online (Sandbox Code Playgroud)

uiview cashapelayer ios cgrect swift

6
推荐指数
1
解决办法
226
查看次数