- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我为自己的公司https://github.com/busycm/BZYStrokeTimer构建了这个代码,在构建过程中,我注意到了一个有趣的“错误”,使用UIBezierPath
似乎无法缓解。就在动画开始时,路径会向前(或向后,取决于逆时针方向)跳跃一定数量的像素,而不是从平滑的增量动画开始。我发现真正有趣的是,路径向前跳跃了多少,实际上是CAShaperLayer
的线宽值。
因此,例如,如果我的贝塞尔曲线路径开始于CGRectGetMidX(self.bounds)
并且线为35,则动画实际上是从CGRectGetMidX(self.bounds)+35
开始的,并且线宽越大,跳转越明显。有什么方法可以摆脱这种情况,以便从起点平滑地发出动画?
这是第一帧的图片。这是动画刚开始后的样子。
然后,当我恢复动画并再次暂停时,移动的距离约为您在图片中看到的距离的1/100。
这是我的贝塞尔曲线路径代码:
- (UIBezierPath *)generatePathWithXInset:(CGFloat)dx withYInset:(CGFloat)dy clockWise:(BOOL)clockwise{
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(CGRectGetMidX(self.bounds)+dx/2, dy/2)];
[path addLineToPoint:CGPointMake(CGRectGetMaxX(self.bounds)-dx/2, dy/2)];
[path addLineToPoint:CGPointMake(CGRectGetMaxX(self.bounds)-dx/2, CGRectGetMaxY(self.bounds)-dy/2)];
[path addLineToPoint:CGPointMake(dx/2, CGRectGetMaxY(self.bounds)-dy/2)];
[path addLineToPoint:CGPointMake(dx/2, dy/2)];
[path closePath];
return clockwise ? path : [path bezierPathByReversingPath];
}
CABasicAnimation *wind = [self generateAnimationWithDuration:self.duration == 0 ? kDefaultDuration : self.duration fromValue:@(self.shapeLayer.strokeStart) toValue:@(self.shapeLayer.strokeEnd) withKeypath:keypath withFillMode:kCAFillModeForwards];
wind.timingFunction = [CAMediaTimingFunction functionWithName:self.timingFunction];
wind.removedOnCompletion = NO;
self.shapeLayer.path = [self generatePathWithXInset:self.lineWidth withYInset:self.lineWidth clockWise:self.clockwise].CGPath;
[self.shapeLayer addAnimation:wind forKey:@"strokeEndAnimation"];
CAShapeLayer
的方法。
- (CAShapeLayer *)shapeLayer {
return !_shapeLayer ? _shapeLayer = ({
CAShapeLayer *layer = [CAShapeLayer layer];
layer.lineWidth = kDefaultLineWidth;
layer.fillColor = UIColor.clearColor.CGColor;
layer.strokeColor = [UIColor blackColor].CGColor;
layer.lineCap = kCALineCapSquare;
layer.frame = self.bounds;
layer.strokeStart = 0;
layer.strokeEnd = 1;
layer;
}) : _shapeLayer;
}
最佳答案
我认为在这里发生的是,在动画的这一帧中,您正在绘制一条包含单个点的线。由于线具有与之关联的粗细,并且线帽的类型为kCALineCapSquare
,因此将其渲染为高度和宽度等于线宽的正方形。
您可以认为它就像在绘制带有正方形标记的线,然后将拖动标记的中点,使其穿过指定曲线中的每个点。对于该行中的第一个点,好像标记在该点着陆,在后面留下一个正方形。
Here's直观表示不同的线帽类型,希望可以使其更加直观。您可能应该将线帽样式更改为kCALineCapButt
。
旁注:
进行更改后,在此代码行中[path moveToPoint:CGPointMake(CGRectGetMidX(self.bounds)+dx/2, dy/2)];
您可能不必再将x
坐标偏移dx/2
。
关于ios - UIBezierPath和CAShapeLayer初始动画跳转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31486711/
在 Swift 中,我有两个半透明圆圈,它们都是 CAShapeLayer。由于它们是半透明的,因此它们之间的任何重叠都变得可见,如下所示: 相反,我希望它们在视觉上“合并”在一起。我尝试过的解决方案
我注意到大多数人在初始化 CAShapeLayer 时使用: CAShapeLayer *shapeLayer = [CAShapeLayer layer]; 而不是使用初始化器: CAShapeLa
我有一个 CAShapeLayer,它必须在用户手指的引导下完成在屏幕上移动的简单任务。 问题是 Action 太慢了。图层确实移动了,但有延迟,感觉很慢。 我有另一个测试应用程序,其中 UIImag
有人有将渐变应用于 CAShapeLayer 的经验吗? CAShapeLayer 是一个很棒的图层类,但它似乎只支持纯色填充着色,而我希望它具有渐变填充(实际上是一个可设置动画的渐变)。 与 CAS
我正在使用核心动画层制作“行进的 Ant ”选择框架,如本文所示:MARCHING ANTS WITH CORE ANIMATION 。但我唯一不喜欢的是 CAShapeLayer 中的线宽。我需要宽
我目前正在使用 CAShapeLayer对于我的要求之一。我成功地实现了 fillcolor、strokecolor 等...但是我想更改该 CAShapeLayer 外部的颜色。我尝试用 backg
使用该代码,我从第一次触摸(触摸开始)和最后一次触摸(触摸结束)创建一行。但为了使这个形状动态(调整大小和移动),我需要在触摸它时选择它。我怎样才能做到这一点? shape.opacity =
我一直在尝试向我的 View 和按钮添加一个图层,但不知何故该图层根本没有出现。我错过了什么? class ViewController: UIViewController { var but
我正在尝试在嵌套在 UIView 层中的 CAShapeLayer 内渲染静态图像。形状的所有装饰(即边框、颜色、路径等)都会在 View 中正确渲染。 但是,为CALayer.contents属性提
我尝试在 UIImageView 中居中 CAShapeLayer 但没有成功,我找到了解决方案,将其(CAShapeLayer)添加到 UIVIew,然后将 UIVIew 添加到 ImageView
我正在为 CAShapeLayer 的结束笔画设置动画,但位置不正确,我无法弄清楚我做错了什么。框架是正确的,但是当我尝试调整位置时,我无法让它“填充” View 框架的宽度。我试图调整 _maskL
我正在尝试相对于特定 anchor 旋转 CAShapeLayer。但是当我申请firstLayer.transform = CATransform3DMakeRotation(CGFloat(M_2
我有一个简单的循环进度 View ,当下载文件时该 View 会被填充。大多数时候它会按预期工作,但有时它会“重置” - 它会填充到某个部分,通常填充到 100% 之前的最后一部分,然后突然变为 0%
我的 UIView 子类中有以下代码的三个变体。 局部变量 - (void)setupLayer { CAShapeLayer *faucet = [CAShapeLayer layer];
我正在使用 UIPickerView,它从具有十六进制颜色代码列表的数组中读取,然后用于用所有不同的颜色填充选择器 View 的每一行。我正在使用一种将十六进制代码转换为 RGB 代码的方法,以及一个
我在屏幕上总共画了 21 条线作为测量工具。这些线条在 UIViewController.view 上绘制为图层。我正在尝试按如下方式删除这些行。 NSLog 声明确认我得到了我正在寻找的所有 21
我正在开发 iPhone 应用程序,我使用下一个代码来圆我层的两个角: CAShapeLayer *backgroundMaskLayer = [CAShapeLayer layer]; UIBezi
我正在尝试围绕其中心点旋转 CAShapeLayer。我对边界、框架、 anchor 和中心点感到非常困惑。 我在 UIView 中创建了一个圆形 CAShape,并希望围绕其中心旋转它。我只包含了部
我正在尝试创建一个动画,其中两条线在用户拖动 UIView 时淡出并在用户释放拖动时淡入。 因此,我有两个函数 undrawLines(在平移手势开始时调用)和 redrawLines(在平移手势结束
我想知道为什么当我尝试使用基本动画为 CAShapeLayer 的 path 属性设置动画时它可以工作,但当我尝试使用事务来执行此操作时却不起作用。 我已经使用事务成功地为其他 animatable
我是一名优秀的程序员,十分优秀!