- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
当用户触摸我的 View (touchesEnded) 时,我正在尝试在我的自定义 UIView
中为 UIBezierPath
(从一条路径到另一条路径)设置动画。
我的绘图代码:
- (void)drawRect:(CGRect)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)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)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
也没有显示。
最佳答案
要使您的动画正常工作,请添加您的 myLineShapeLayer
作为 View 的子层 layer
:
例如,在 viewDidLoad
中:
[self.view.layer addSublayer:myLineShapeLayer];
为了能够看到endPath
动画结束后仍然持续出现在屏幕上,我们可以先分配 endPath
到 path
CAShapeLayer
的属性(property):
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/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!