简体   繁体   English

iPhone开发-基本绘图和动画问题

[英]iPhone Development - Basic drawing and animation question

I have a case where i'm drawing shapes, eg a Triangle (Points A, B, C). 我遇到的情况是绘制图形,例如三角形(点A,B,C)。 Now i want to animate it so that Points A, B, C move towards Point X, Y, Z. (Lets just say on button click) 现在我要设置动画,以便点A,B,C向点X,Y,Z移动。(让我们说一下按钮单击即可)

I'm using a drawRect: method in my custom view for drawing. 我在自定义视图中使用drawRect:方法进行绘制。 I don't want my view to move, i rather want my drawing to move (because i have multiple drawings). 我不想移动视图,而希望移动我的图形(因为我有多个图形)。

Any pointers? 有指针吗? Any relative articles? 有相关文章吗?

Regards, Mustafa 问候,穆斯塔法

Core Animation is a good solution for this type of thing. 核心动画是此类事情的一个很好的解决方案。 You have CAShapeLayer that allows you to draw shapes according to a path and you can animate using a basic animation or a keyframe animation. 您具有CAShapeLayer,它允许您根据路径绘制形状,并且可以使用基本动画或关键帧动画进行动画处理。 You can do something like this in your button click: 您可以在按钮单击中执行以下操作:

CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
[animation setFromValue:[NSValue valueWithCGPoint:CGPointMake(0.0, 0.0)]];
[animation setToValue:[NSValue valueWithCGPoint:CGPointMake(320.0, 480.0)]];
[animation setDuration:2.0f];
[shapeLayer addAnimation:animation forKey:@"positionAnimation"];

This will animate the layer from point 0.0, 0.0 (upper left hand corner) to 320.0, 480.0 (lower right hand corner) over the course of two seconds. 这将在两秒钟的过程中使该图层从点0.0、0.0(左上角)到320.0、480.0(右下角)进行动画处理。 When you add the animation to your layer, it will start playing immediately. 将动画添加到图层时,它将立即开始播放。 If you are looking to rotate the animation (wasn't sure from the post), you can do this: 如果您想旋转动画(从帖子中不确定),则可以执行以下操作:

CABasicAnimation *rotationAnimation;
rotationAnimation = [CABasicAnimation 
             animationWithKeyPath:@"transform.rotation.z"];

[rotationAnimation setFromValue:DegreesToNumber(0)];
[rotationAnimation setToValue:DegreesToNumber(360)];
[rotationAnimation setDuration:2.0f];
[rotationAnimation setRepeatCount:10000]; // keep spinning

[shapeLayer addAnimation:rotationAnimation forKey:@"rotate"];

The DegreesToNumber is a helper function that converts degrees to radians and returns an NSNumber object: DegreesToNumber是一个辅助函数,可将度转换为弧度并返回NSNumber对象:

CGFloat DegreesToRadians(CGFloat degrees)
{
    return degrees * M_PI / 180;
}

NSNumber* DegreesToNumber(CGFloat degrees)
{
    return [NSNumber numberWithFloat:
            DegreesToRadians(degrees)];
}

There are lots of articles on the web about Core Animation, but this should get you started. Web上有很多关于Core Animation的文章,但这应该可以帮助您入门。 Let me know if you need clarification. 让我知道您是否需要澄清。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM