• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 在自定义 UIView 中动画 UIBezierPath 不起作用

[复制链接]
菜鸟教程小白 发表于 2022-12-13 08:57:33 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

当用户触摸我的 View (touchesEnded) 时,我正在尝试在我的自定义 UIView 中为 UIBezierPath 设置动画(从一条路径到另一条路径)。

我的绘图代码:

- (void)drawRectCGRect)rect {
    // Drawing code
    [self createStartPath];
    [self createEndPath];

    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextAddPath(currentContext, _startPath.CGPath);
    CGContextDrawPath(currentContext, kCGPathStroke);

}

- (void) createStartPath
{
    _startPath = UIBezierPath.bezierPath;
    [_startPath moveToPoint: CGPointMake(18, 22.5)];
    [_startPath addCurveToPoint: CGPointMake(18.38, 22.32) controlPoint1: CGPointMake(18.14, 22.5) controlPoint2: CGPointMake(18.29, 22.44)];
    [_startPath addCurveToPoint: CGPointMake(18.32, 21.62) controlPoint1: CGPointMake(18.56, 22.11) controlPoint2: CGPointMake(18.53, 21.79)];
    [_startPath addLineToPoint: CGPointMake(6.78, 12)];
    [_startPath addLineToPoint: CGPointMake(18.32, 2.38)];
    [_startPath addCurveToPoint: CGPointMake(18.38, 1.68) controlPoint1: CGPointMake(18.53, 2.21) controlPoint2: CGPointMake(18.56, 1.89)];
    [_startPath addCurveToPoint: CGPointMake(17.68, 1.62) controlPoint1: CGPointMake(18.21, 1.47) controlPoint2: CGPointMake(17.89, 1.44)];
     [_startPath addLineToPoint: CGPointMake(5.68, 11.62)];
    [_startPath addCurveToPoint: CGPointMake(5.5, 12) controlPoint1: CGPointMake(5.56, 11.71) controlPoint2: CGPointMake(5.5, 11.85)];
    [_startPath addCurveToPoint: CGPointMake(5.68, 12.38) controlPoint1: CGPointMake(5.5, 12.15) controlPoint2: CGPointMake(5.56, 12.29)];
    [_startPath addLineToPoint: CGPointMake(17.68, 22.38)];
    [_startPath addCurveToPoint: CGPointMake(18, 22.5) controlPoint1: CGPointMake(17.77, 22.46) controlPoint2: CGPointMake(17.89, 22.5)];
    [_startPath closePath];

    [self.fillColor setFill];

    [_startPath fill];
}

- (void) createEndPath
{

     _endPath = UIBezierPath.bezierPath;
    [_endPath moveToPoint: CGPointMake(6, 22.5)];
    [_endPath addCurveToPoint: CGPointMake(5.62, 22.32) controlPoint1: CGPointMake(5.86, 22.5) controlPoint2: CGPointMake(5.71, 22.44)];
    [_endPath addCurveToPoint: CGPointMake(5.68, 21.62) controlPoint1: CGPointMake(5.44, 22.11) controlPoint2: CGPointMake(5.47, 21.79)];
    [_endPath addLineToPoint: CGPointMake(17.22, 12)];
    [_endPath addLineToPoint: CGPointMake(5.68, 2.38)];
    [_endPath addCurveToPoint: CGPointMake(5.62, 1.68) controlPoint1: CGPointMake(5.47, 2.21) controlPoint2: CGPointMake(5.44, 1.89)];
    [_endPath addCurveToPoint: CGPointMake(6.32, 1.62) controlPoint1: CGPointMake(5.79, 1.47) controlPoint2: CGPointMake(6.11, 1.44)];
    [_endPath addLineToPoint: CGPointMake(18.32, 11.62)];
    [_endPath addCurveToPoint: CGPointMake(18.5, 12) controlPoint1: CGPointMake(18.44, 11.71) controlPoint2: CGPointMake(18.5, 11.85)];
    [_endPath addCurveToPoint: CGPointMake(18.32, 12.38) controlPoint1: CGPointMake(18.5, 12.15) controlPoint2: CGPointMake(18.44, 12.29)];
    [_endPath addLineToPoint: CGPointMake(6.32, 22.38)];
    [_endPath addCurveToPoint: CGPointMake(6, 22.5) controlPoint1: CGPointMake(6.23, 22.46) controlPoint2: CGPointMake(6.11, 22.5)];
    [_endPath closePath];

    [self.fillColor setFill];

    //[_endPath fill];
}

我从这里开始我的动画(希望将一条路径“变形”到另一条路径):

- (void)touchesEndedNSSet *)touches withEventUIEvent *)event
 {
    [super touchesEnded:touches withEvent:event];

    CAShapeLayer * myLineShapeLayer = [[CAShapeLayer alloc] init];
    
    CABasicAnimation * pathAnimation = [CABasicAnimation animationWithKeyPath"path"];
    pathAnimation.fromValue = (__bridge id)[_startPath CGPath];
    pathAnimation.toValue = (__bridge id)[_endPath CGPath];
    pathAnimation.duration = 3.0f;
    [myLineShapeLayer addAnimation:pathAnimation forKey"animationKey"];
}

我看到 startPath 和我的 touchesEnded 被调用,但没有任何动画显示 endPath



Best Answer-推荐答案


为了让您的动画正常工作,请将您的 myLineShapeLayer 添加为 View 的 layer 的子层:

例如,在 viewDidLoad 中:

[self.view.layer addSublayer:myLineShapeLayer];

为了在动画结束后仍然能够在屏幕上看到 endPath,我们可以先将 endPath 分配给 path<CAShapeLayer 的/code> 属性:

myLineShapeLayer.path = [endPath CGPath];

因此我们在没有 toValue 的情况下制作动画:

CABasicAnimation * pathAnimation = [CABasicAnimation animationWithKeyPath"path"];
pathAnimation.fromValue = (__bridge id)[_startPath CGPath];
pathAnimation.duration = 3.0f;
[myLineShapeLayer addAnimation:pathAnimation forKey"animationKey"];

动画的结束效果会在动画结束后保留​​在屏幕上。

关于ios - 在自定义 UIView 中动画 UIBezierPath 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31287821/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap