开发者

2D polygon animation in iPhone

开发者 https://www.devze.com 2023-03-29 18:46 出处:网络
I am not sure if \"2d polygon animation\" is the right term what I am trying to achieve, but basically I want animation similar to 3D morphing but with 2d polygons so that I have multiple \"frames\" o

I am not sure if "2d polygon animation" is the right term what I am trying to achieve, but basically I want animation similar to 3D morphing but with 2d polygons so that I have multiple "frames" of the same polygons where vertices are in different locations and interpolate between them to achieve animation.

Before I start to implement it myself, I want to know if there's so开发者_StackOverflow中文版me library or something to achieve this in iOS. At least in Cocos2D I did not find such feature.

An editor to define the polygons would be nice tool also, which exports to XML or something in order to easily import it to my application. There should be a professional editing software to do this, right? This should be a really common thing? I tried to google but I guess I am missing the right keywords.


Core Animation can do it - included with the SDK. I think you need to look at CAShapeLayer.


CAShapeLayer will do that. What you have to do is to create the layer and assign its path property to say path1 which is a CGPathRef object created as follows:

// four-sided polygon
CGMutablePathRef path1 = CGPathCreateMutable(); 
CGPathMoveToPoint(path1, NULL, 160, 290);
CGPathAddLineToPoint(path1, NULL, 170, 300);
CGPathAddLineToPoint(path1, NULL, 160, 310);
CGPathAddLineToPoint(path1, NULL, 150, 300);

CAShapeLayer* bgPoly = [CAShapeLayer layer];
bgPoly.path = path1;
bgPoly.backgroundColor = [UIColor blueColor].CGColor;

CGPathRelease(path1);

Then change the polygon path to a different CGPathRef object, path2, and you will get what you want:

// morphs to a six-sided polygon
CGMutablePathRef path2 = CGPathCreateMutable(); 
CGPathMoveToPoint(path2, NULL, 160, 290);
CGPathAddLineToPoint(path2, NULL, 170, 300);
CGPathAddLineToPoint(path2, NULL, 170, 300);
CGPathAddLineToPoint(path2, NULL, 160, 310);
CGPathAddLineToPoint(path2, NULL, 150, 300);
CGPathAddLineToPoint(path2, NULL, 150, 300);

bgPoly.path = path2;

CGPathRelease(path2);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号