ios - 动画贝塞尔曲线就像在 iPhone 屏幕上进行绘图一样

标签 ios objective-c animation uibezierpath

我想画一条泰姬陵陵墓轮廓的贝塞尔曲线。

Q1。我如何在 XCode 中绘制它?我可以在 PS 中轻松做到这一点,但我发现很难在 XCode 中以编程方式做到这一点,尤其是获取控制点的坐标。

Q2。而且,当屏幕尺寸变化时,如何固定点的坐标?

Q3。我如何制作动画,就像一支看不见的铅笔在屏幕上画画一样?

最佳答案

如何在 UIKit 中绘制贝塞尔曲线路径?

创建一个 UIBezierPath 对象,然后使用 various methods on it to construct your path .

我想您最感兴趣的方法是:

例如,像这样的东西将创建一个简单的二次曲线贝塞尔曲线路径:

UIBezierPath* yourPath = [UIBezierPath bezierPath]; // create the bezier path
[yourPath moveToPoint:CGPointMake(100, 100)]; // move to your starting point
[yourPath addQuadCurveToPoint:CGPointMake(300, 500) controlPoint:CGPointMake(500, 350)]; // add a new quad curve to the point

但是,如果您习惯于使用 Photoshop 之类的工具来创建路径,那么您 may be interested in PaintCode ,这可以让您使用“所见即所得”的方法创建贝塞尔曲线路径。

您可以将此 UIBezierPath 添加到 CAShapeLayerpath 属性中,以便在屏幕上显示它。

CAShapeLayer* yourShapeLayer = [CAShapeLayer layer]; // create your shape layer
yourShapeLayer.frame = CGRectMake(0, 0, 300, 500); // assign it's frame
yourShapeLayer.path = yourPath.CGPath; // add your path to the shape layer
yourShapeLayer.fillColor = [UIColor clearColor].CGColor; // prevent the shape layer from filling

[self.view.layer addSublayer:yourShapeLayer]; // add the shape layer to the view

然后您可以轻松地在形状图层上设置 strokeColorlineWidth 属性,以自定义路径的描边方式。

yourShapeLayer.strokeColor = [UIColor redColor].CGColor; // red stroke color
yourShapeLayer.lineWidth = 5.0; // 5 point stroke width

你应该得到这样的结果:

enter image description here


我如何制作动画,就像一支看不见的铅笔在屏幕上画画一样?

您可以轻松地创建一个 CABasicAnimation,它可以按顺序为 CAShapeLayerstrokeStartstrokeEnd 属性设置动画实现您想要的动画效果。

这会导致线条“绘制”到屏幕上的动画效果。

例如,此代码将导致 strokeEnd 属性从 0.0 动画化到 1.0,从而创建您想要的效果:

yourShapeLayer.strokeStart = 0.0; // reset stroke start before animating

CABasicAnimation* strokeAnim = [CABasicAnimation animationWithKeyPath:@"strokeEnd"]; // create animation that changes the strokeEnd property
strokeAnim.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; // give it a nice timing function
strokeAnim.duration = 2.0; // duration of the animation
strokeAnim.fromValue = @(0.0); // provide the start value for the animation, wrapped in an NSNumber literal.
strokeAnim.toValue = @(1.0); // provide the end value for the animation, wrapped in an NSNumber literal.

[yourShapeLayer addAnimation:strokeAnim forKey:@"strokeAnim"]; // add animation to layer

enter image description here


如何缩放此路径以适应不同尺寸的屏幕?

我通常喜欢解决这个问题的方法是在给定的固定框架中定义路径的点(比如 500 x 500 点),然后根据屏幕的大小(在您的情况下,您想要缩放到屏幕的宽度)。

所以我们所要做的就是生成我们的比例因子。

CGFloat sf = self.view.frame.size.width/500.0; // The factor to scale the path by

然后,我们只需将 UIBezierPath 中的所有点乘以这个比例因子即可。比如走我们之前的路径:

UIBezierPath* yourPath = [UIBezierPath bezierPath]; // create the bezier path
[yourPath moveToPoint:CGPointMake(100.0*sf, 100.0*sf)]; // move to your starting point
[yourPath addQuadCurveToPoint:CGPointMake(300.0*sf, 500.0*sf) controlPoint:CGPointMake(500.0*sf, 350.0*sf)];

现在,我们所要做的就是在我们的贝塞尔路径点中编程,就好像我们总是在 500 x 500 的框架中绘制它(我的点已经很好地适应了)。

您还需要更改 CAShapeLayer 的大小,使其呈方形并占据整个屏幕宽度。

yourShapeLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.width);

最后,您希望将 CAShapeLayer 定位到屏幕中央。您可以通过将图层的 position 属性设置为 View 的 center 来轻松完成此操作。

yourShapeLayer.position = self.view.center;

现在您的路径应该始终缩放以占据屏幕的宽度。

enter image description here

(我在层中添加了灰色背景只是为了说明缩放比例)


完整项目(为方便起见):https://github.com/hamishknight/Drawing-Scaling-Animating-UIBezierPaths

关于ios - 动画贝塞尔曲线就像在 iPhone 屏幕上进行绘图一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35363104/

相关文章:

ios - 在 TableView 单元格中显示多行 - IOS

objective-c - 在 iOS 中逐字符读取文件

ios - SWRevealViewController 无法从 appdelegate.m 调用

ios - 移动 imageView 时旋转它

objective-c - Cocos2D无限背景图片

iOS:在对新的 viewController 执行 segue 时添加插页式广告?

IOS:如何检测动画进行中

iphone - UIView animateWithDuration 立即返回

c# - WPF Sprite 表动画

objective-c - 在哪里可以启动MobileSubstrate调整编程?