Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-02-2011, 01:19 PM   #1 (permalink)
Token farm animal
 
sneaky's Avatar
 
Join Date: Apr 2011
Posts: 321
sneaky is on a distinguished road
Default CALayer PacMan shape?

I was watching a iTunesU video from WWDC2011 where they just very quickly had up on the screen a CALayer in the shape of a PacMan. They were animating opening and closing the "mouth" a few times and then quickly moved on.

I want to use this kind of shape for a timer, a full circle and as I'm counting down I reveal what is behind. I have been searching for a bit but I haven't figured out how it's done.

The only way I can think of is using a CAShapeLayer and UIBezierPaths but that seems overly complicated for this job. How would you go about doing it?
sneaky is offline   Reply With Quote
Old 08-03-2011, 10:35 AM   #2 (permalink)
Token farm animal
 
sneaky's Avatar
 
Join Date: Apr 2011
Posts: 321
sneaky is on a distinguished road
Default

I'm able to draw a PacMan shape now
Code:
- (void)drawRect:(CGRect)rect
{
	float from = 0.4;
	float to = 5.8;
	CGPoint center = self.center;
	CGContextRef ctx = UIGraphicsGetCurrentContext();
	CGContextSetFillColor(ctx, CGColorGetComponents( [[UIColor greenColor] CGColor]));
	CGContextMoveToPoint(ctx, center.x, center.y);     
	CGContextAddArc(ctx, center.x, center.y, 100, from, to, 0); 
	CGContextClosePath(ctx); 
	CGContextFillPath(ctx);
}
What I want to do next is animate the from and to of my arc. I haven't been able to do it. The methods I've seen to perform this task clears the context and then draws a brand new pacman with updated from and to values. It feels a bit hack-ish and I don't think that's the right way of doing it.
sneaky is offline   Reply With Quote
Old 08-03-2011, 02:52 PM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by sneaky View Post
I'm able to draw a PacMan shape now
Code:
- (void)drawRect:(CGRect)rect
{
	float from = 0.4;
	float to = 5.8;
	CGPoint center = self.center;
	CGContextRef ctx = UIGraphicsGetCurrentContext();
	CGContextSetFillColor(ctx, CGColorGetComponents( [[UIColor greenColor] CGColor]));
	CGContextMoveToPoint(ctx, center.x, center.y);     
	CGContextAddArc(ctx, center.x, center.y, 100, from, to, 0); 
	CGContextClosePath(ctx); 
	CGContextFillPath(ctx);
}
What I want to do next is animate the from and to of my arc. I haven't been able to do it. The methods I've seen to perform this task clears the context and then draws a brand new pacman with updated from and to values. It feels a bit hack-ish and I don't think that's the right way of doing it.

I'd suggest using a CAShapeLayer. Set up the shape layer using a CGPath with an arc, much like the code you posted. You can then create a CABasicAnimation with a key of "path" and update the path with new from and to values. The shape layer knows how to animate changes in a path. I just learned about this last week, as it happens...
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 08-03-2011, 03:32 PM   #4 (permalink)
Token farm animal
 
sneaky's Avatar
 
Join Date: Apr 2011
Posts: 321
sneaky is on a distinguished road
Default

Back to CAShaprLayer. Yes, I think you're right. The method of redrawing doesn't feel right. Will have to try your approach.
sneaky is offline   Reply With Quote
Old 08-13-2011, 06:19 AM   #5 (permalink)
Token farm animal
 
sneaky's Avatar
 
Join Date: Apr 2011
Posts: 321
sneaky is on a distinguished road
Default

Wow, I've found it really difficult to find good information on this topic. Maybe I'm looking in the wrong places. I have done the following now
Code:
- (void)viewDidLoad
{
    [super viewDidLoad];
	
	rootLayer = [CALayer layer];
	rootLayer.frame = self.view.bounds;
	[self.view.layer addSublayer:rootLayer];
	
	closedPath = CGPathCreateMutable();
	CGPoint center = self.view.center;
	CGPathMoveToPoint(closedPath, nil, center.x, center.y);
	CGPathAddArc(closedPath, NULL, center.x, center.y, 100, 0.1, 5.0, 0);
	CGPathCloseSubpath(closedPath);
	
	openPath = CGPathCreateMutable();
	CGPathMoveToPoint(openPath, nil, center.x, center.y);
	CGPathAddArc(openPath, NULL, center.x, center.y, 100, 3.0, 3.5, 0);
	CGPathCloseSubpath(openPath);
	
	shapeLayer = [CAShapeLayer layer];
	shapeLayer.path = closedPath;
	UIColor *fillColor = [UIColor greenColor];
	shapeLayer.fillColor = fillColor.CGColor;
	shapeLayer.fillRule = kCAFillRuleNonZero;
	[rootLayer addSublayer:shapeLayer];
	[self performSelector:@selector(startAnimation) withObject:nil afterDelay:1.0];
}

-(void)startAnimation {	
	CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"path"];
	animation.duration = 2.0;
	animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
	animation.repeatCount = HUGE_VALF;
	animation.autoreverses = YES;
	animation.fromValue = (id)closedPath;
	animation.toValue = (id)openPath;
	[shapeLayer addAnimation:animation forKey:@"myAnimation"];
}
This code will transfrom the entire shape from a pacman to a pie piece. This is not what I'm after and I have been desperately been searching for how to access the right values. going for animationWithKeyPath:@"path" is probably right but the fromValue and toValue values I specify the entire shape and not just the startAngle and endAngle of the CGPathAddArc.
sneaky is offline   Reply With Quote
Old 11-22-2011, 10:13 PM   #6 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 2
pacphone is on a distinguished road
Thumbs up

If you need an example of pacman to follow you can view the game I provided.
It is written in flash but it has the correct map from the original game.
Hope it helps.
pacphone is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 391
15 members and 376 guests
7twenty7, blasterbr, buggen, chiataytuday, dedeys78, dre, fiftysixty, HemiMG, jimmyon122, jonathandeknudt, LEARN2MAKE, nyoe, pungs, tymex, UMAD
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,669
Threads: 94,121
Posts: 402,902
Top Poster: BrianSlick (7,990)
Welcome to our newest member, dedeys78
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:03 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0