Moving two shapes along path iOS? - iphone

I'm new to Objective-C, and I'm trying to create an iPhone application. I'm trying to move two rectangles (tagging along behind each other) along a path (specifically, an oblique straight oval).
With the below code, I am able to get both rectangles to show up, but the problem is that when I change the value of the starting point for their separate animations, one of the rectangles ends up catching up to the other one by the end of the animation.. How do I solve this?
Again, I want to move two shapes along a custom path (a track) and both objects / shapes must be separate from each other and at the same distance of separate distance apart throughout the animation.
My code is below:
#import "track.h"
#import <QuartzCore/QuartzCore.h>
#define degreesToRadian(x) (M_PI * x / 180.0)
#define radiansToDegrees(x) (x * 180 / M_PI);
#implementation track
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
//call three methods once class is istantiated
//pass parameters for UIView, width, color, size, points (start, end)
[self drawTrack];
[self animateFirstRunner];
[self animateSecondRunner];
}
- (void) animateFirstRunner
{
//create an animation and give it certain specs
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:#"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeBackwards;
pathAnimation.removedOnCompletion = NO;
pathAnimation.duration = 5;
pathAnimation.repeatCount = 1000;
//create a path for the object or layer to go on
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, 220, 10);
CGPathAddArc(curvedPath, NULL, 100, 100, 90, -M_PI_2, M_PI_2, 1);
CGPathAddArc(curvedPath, NULL, 220, 100, 90, M_PI_2, -M_PI_2, 1);
//set the path then release
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);
//set size of the layer and get the context
UIGraphicsBeginImageContext(CGSizeMake(20,20));
CGContextRef ctx = UIGraphicsGetCurrentContext();
//set line width and fill color
CGContextSetLineWidth(ctx, 1.5);
CGContextSetFillColorWithColor(ctx, [UIColor blueColor].CGColor);
//add the rectangle to the context (runner)
CGContextAddRect(ctx, CGRectMake(1, 1, 15, 15));
//draw the path
CGContextDrawPath(ctx, kCGPathFillStroke);
UIImage *runner = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *runnerView = [[UIImageView alloc] initWithImage:runner];
//add outline around runner
runnerView.frame = CGRectMake(1, 1, 20, 20);
[self addSubview:runnerView];
[runnerView.layer addAnimation:pathAnimation forKey:#"moveTheSquare"];
}
- (void) animateSecondRunner
{
//create an animation and give it certain specs
CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:#"position"];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeBackwards;
pathAnimation.removedOnCompletion = NO;
pathAnimation.duration = 5;
pathAnimation.repeatCount = 1000;
//create a path for the object or layer to go on
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, 220, 10);
CGPathAddArc(curvedPath, NULL, 100, 100, 90, -M_PI_2, M_PI_2, 1);
CGPathAddArc(curvedPath, NULL, 220, 100, 90, M_PI_2, -M_PI_2, 1);
//set the path then release
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);
//set size of the layer and get the context
UIGraphicsBeginImageContext(CGSizeMake(20,20));
CGContextRef ctx = UIGraphicsGetCurrentContext();
//set line width and fill color
CGContextSetLineWidth(ctx, 1.5);
CGContextSetFillColorWithColor(ctx, [UIColor blueColor].CGColor);
//add the rectangle to the context (runner)
CGContextAddRect(ctx, CGRectMake(1, 1, 15, 15));
//draw the path
CGContextDrawPath(ctx, kCGPathFillStroke);
UIImage *runner = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *runnerView = [[UIImageView alloc] initWithImage:runner];
//add outline around runner
runnerView.frame = CGRectMake(1, 1, 20, 20);
[self addSubview:runnerView];
[runnerView.layer addAnimation:pathAnimation forKey:#"moveTheSquare"];
}
- (void) drawTrack
{
UIGraphicsBeginImageContext(CGSizeMake(320,460));
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(currentContext, 6);
CGContextSetStrokeColorWithColor(currentContext, [UIColor whiteColor].CGColor);
//choost starting point, then add arc (half circle)
//the second parameter of CGContextAddArc is the changes how long the line is (horizontal)
//draw from -PI to PI and the other way around
CGContextMoveToPoint(currentContext, 200, 10);
CGContextAddArc(currentContext, 100, 100, 90, -M_PI_2, M_PI_2, 1);
CGContextAddArc(currentContext, 220, 100, 90, M_PI_2, -M_PI_2, 1);
CGContextClosePath(currentContext);
//draw the path on the context
CGContextDrawPath(currentContext, kCGPathStroke);
//create track
UIImage *track = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//add track to view
UIImageView *trackView = [[UIImageView alloc] initWithImage:track];
trackView.frame = CGRectMake(1, 1, 320, 460);
trackView.backgroundColor = [UIColor clearColor];
[self addSubview:trackView];
}
#end

You should start by parameterising the one function rather than writing two almost identical functions. Then (at the very least) you won't have people scratching their heads, wondering how they differ.
I'm presenting this as an answer rather than just a comment because, in the process of figuring what parameters to pass and how to use them, you'll probably discover all by yourself what you're doing wrong.

Related

CGContextClearRect with circle

I am creating an app in which i am trying to clear the rect of UIImageView. I have achieved this with CGContextClearRect, but the problem is that it is clearing rect in square shape and i want to achieve this effect in round shape.
What i have tried so far:
UITouch *touch2 = [touches anyObject];
CGPoint point = [touch2 locationInView:img];
UIGraphicsBeginImageContextWithOptions(img.bounds.size, NO, 0.0f);
[img.image drawInRect:CGRectMake(0, 0, img.frame.size.width, img.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect cirleRect = CGRectMake(point.x, point.y, 40, 40);
CGContextAddArc(context, 50, 50, 50, 0.0, 2*M_PI, 0);
CGContextClip(context);
CGContextClearRect(context,cirleRect);
//CGContextClearRect(context, CGRectMake(point.x, point.y, 30, 30));
img.image =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
I'm not sure exactly what you are doing, but your clipping arc isn't being created correctly. You are creating it at a fixed position which is why it doesn't really work - except it does, if you click in the top left corner.
If you want to see it working try this:
- (IBAction)clearCircle:(UITapGestureRecognizer *)sender {
UIImageView *imageView = self.imageView;
UIImage *currentImage = imageView.image;
CGSize imageViewSize = imageView.bounds.size;
CGFloat rectWidth = 40.0f;
CGFloat rectHeight = 40.0f;
CGPoint touchPoint = [sender locationInView:imageView];
UIGraphicsBeginImageContextWithOptions(imageViewSize, YES, 0.0f);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[currentImage drawAtPoint:CGPointZero];
CGRect clippingEllipseRect = CGRectMake(touchPoint.x - rectWidth / 2, touchPoint.y - rectHeight / 2, rectWidth, rectHeight);
CGContextAddEllipseInRect(ctx, clippingEllipseRect);
CGContextClip(ctx);
CGContextClearRect(ctx, clippingEllipseRect);
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
Which creates a clipping ellipse (in this case a circle) in the rect 40 x 40 centred at the touch point.
You can see this in an example project on Bitbucket which you can download for yourself to try

Two-color background for UILabel

I got a requirements to display a UILabel with background split between two colors, like in this image:
(colors here are black at the bottom and 50% gray at the top - but this is not important). I tried setting the label's background colour to 50% grey in the interface builder and then do this in the code:
CALayer *sl1 = [[[CALayer alloc] init] autorelease];
sl1.frame = CGRectMake(0, lbl.frame.size.height / 2, lbl.frame.size.width, score1.frame.size.height/2);
sl1.backgroundColor = [[UIColor blackColor] CGColor];
[lbl.layer insertSublayer:sl1 atIndex:0];
Unfortunately, this resulted in the black part being drawn over the text, so the label looks like this:
which is, needless to say, is not something I need. So how can I get this background without turning to custom images? The issue is I need to have UILabel's like this in several places, different sizes - so I would need to create multiple versions of the background image.
Any ideas? Thanks.
this works:
UILabel* myLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 50)];
myLabel.text = #"Ciao";
myLabel.textColor = [UIColor greenColor];
UIGraphicsBeginImageContext(CGSizeMake(100, 50));
CGContextRef context = UIGraphicsGetCurrentContext();
// drawing with a gray fill color
CGContextSetRGBFillColor(context, 0.4, 0.4, 0.4, 1.0);
// Add Filled Rectangle,
CGContextFillRect(context, CGRectMake(0.0, 0.0, 100, 50));
// drawing with a black fill color
CGContextSetRGBFillColor(context, 0., 0., 0., .9);
// Add Filled Rectangle,
CGContextFillRect(context, CGRectMake(0.0, 25, 100, 25));
UIImage* resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
myLabel.backgroundColor = [UIColor colorWithPatternImage:resultingImage];
[self.view addSubview:myLabel];
Use UIColor's +colorWithPatternImage:. Pass in a 1px by the UILabel's height image and it will be "tiled" across the the width of the view.
myLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"label-background.png"]];
first you have to subclass the UILabel and override it's drawRect: method like this for gradient background
- (void)drawRect:(CGRect)rect
{
//////////////GET REFERENCE TO CURRENT GRAPHICS CONTEXT
CGContextRef context = UIGraphicsGetCurrentContext();
//////////////CREATE BASE SHAPE WITH ROUNDED CORNERS FROM BOUNDS
CGRect activeBounds = self.bounds;
CGFloat cornerRadius = 10.0f;
CGFloat inset = 6.5f;
CGFloat originX = activeBounds.origin.x + inset;
CGFloat originY = activeBounds.origin.y + inset;
CGFloat width = activeBounds.size.width - (inset*2.0f);
CGFloat height = activeBounds.size.height - (inset*2.0f);
CGRect bPathFrame = CGRectMake(originX, originY, width, height);
CGPathRef path = [UIBezierPath bezierPathWithRoundedRect:bPathFrame cornerRadius:cornerRadius].CGPath;
//////////////CREATE BASE SHAPE WITH FILL AND SHADOW
CGContextAddPath(context, path);
CGContextSetFillColorWithColor(context, [UIColor colorWithRed:210.0f/255.0f green:210.0f/255.0f blue:210.0f/255.0f alpha:1.0f].CGColor);
CGContextSetShadowWithColor(context, CGSizeMake(0.0f, 1.0f), 6.0f, [UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:1.0f].CGColor);
CGContextDrawPath(context, kCGPathFill);
//////////////CLIP STATE
CGContextSaveGState(context); //Save Context State Before Clipping To "path"
CGContextAddPath(context, path);
CGContextClip(context);
//////////////DRAW GRADIENT
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
size_t count = 3;
CGFloat locations[3] = {0.0f, 0.57f, 1.0f};
CGFloat components[12] =
{ 0.0f/255.0f, 0.0f/255.0f, 0.0f/255.0f, 1.0f, //1
5.0f/255.0f, 5.0f/255.0f, 5.0f/255.0f, 1.0f, //2
10.0f/255.0f, 10.0f/255.0f, 10.0f/255.0f, 1.0f}; //3
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, count);
CGPoint startPoint = CGPointMake(activeBounds.size.width * 0.5f, 0.0f);
CGPoint endPoint = CGPointMake(activeBounds.size.width * 0.5f, activeBounds.size.height);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGColorSpaceRelease(colorSpace);
CGGradientRelease(gradient);
}
This will draw black to white gradient background
May this will help you
Happy Codding :)
the above code is from this site http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-uialertview-custom-graphics/

A UIImage created in coregraphics isn't repeating

I have this code, which should repeat the same UIImage:
UIView *paperMiddle = [[UIView alloc] initWithFrame:CGRectMake(0, 34, 320, rect.size.height - 34)];
UIImage *paperPattern = paperBackgroundPattern(context);
paperMiddle.backgroundColor = [UIColor colorWithPatternImage:paperPattern];
[self addSubview:paperMiddle];
And this is the paperBackgroundPattern method:
UIImage *paperBackgroundPattern(CGContextRef context) {
CGRect paper3 = CGRectMake(10, -15, 300, 16);
CGRect paper2 = CGRectMake(13, -15, 294, 16);
CGRect paper1 = CGRectMake(16, -15, 288, 16);
//Shadow
CGContextSetShadowWithColor(context, CGSizeMake(0,0), 10, [[UIColor colorWithWhite:0 alpha:0.5]CGColor]);
CGPathRef path = createRoundedRectForRect(paper3, 0);
CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]);
CGContextAddPath(context, path);
CGContextFillPath(context);
//Layers of paper
CGContextSaveGState(context);
drawPaper(context, paper3);
drawPaper(context, paper2);
drawPaper(context, paper1);
CGContextRestoreGState(context);
UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 1), NO, 0);
UIImage *paperImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return paperImage;
}
It isn't repeating the image. A result of having the image there though is that it shows as the top pixel of the screen (which isn't the frame i've given it).
Any ideas why?
I don't know what the context is that you're passing in, but whatever it is, you shouldn't be drawing into it. And you aren't drawing anything into the context that you made with UIGraphicsBeginImageContextWithOptions.
If you want to generate an image, you don't need to pass in a context, just use the one that UIGraphicsBeginImageContextWithOptions makes for you.
UIImage *paperBackgroundPattern() {
UIGraphicsBeginImageContextWithOptions(CGSizeMake(320, 1), NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
// draw into context, then...
UIImage *paperImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return paperImage;
}
Also -- are you really trying to make an image that's 320 pt wide and 1 high? It seems odd that you are drawing such elaborate stuff into such a tiny image.

iOS - How to draw a transparent triangle in view

I am trying to do a custom tab bar with a transparent triangle that points into the tab bar's view.
Right now I am drawing a linear gradient and a gloss in the drawRect method for the background of this tab bar. I just need to add the transparent triangle on there. I know how to draw a triangle. I just need to know how to make it transparent to show the background beneath the tab bar view.
Anyone know how to do this?
Update
Here is the current code:
void drawGlossAndGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor)
{
drawLinearGradient(context, rect, startColor, endColor);
CGColorRef glossColor1 = [UIColor colorWithRed:1.0 green:1.0
blue:1.0 alpha:0.35].CGColor;
CGColorRef glossColor2 = [UIColor colorWithRed:1.0 green:1.0
blue:1.0 alpha:0.1].CGColor;
CGRect topHalf = CGRectMake(rect.origin.x, rect.origin.y,
rect.size.width, rect.size.height/2);
drawLinearGradient(context, topHalf, glossColor1, glossColor2);
}
void drawLinearGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor)
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[] = { 0.0, 1.0 };
NSArray *colors = [NSArray arrayWithObjects:(__bridge id)startColor, (__bridge id)endColor, nil];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);
CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
CGContextSaveGState(context);
CGContextAddRect(context, rect);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGContextRestoreGState(context);
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
}
- (void)drawTriangle
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGPoint pt1 = CGPointMake(0.0f, 0.0f);
CGPoint pt2 = CGPointMake(10.0f, 10.0f);
CGPoint pt3 = CGPointMake(20.0f, 0.0f);
CGPoint vertices[] = {pt1, pt2, pt3, pt1};
CGContextBeginPath(context);
CGContextAddLines(context, vertices, 3);
CGContextClosePath(context);
CGContextClip(context);
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGColorRef lightColor = [UIColor colorWithRed:65.0f/255.0f green:64.0f/255.0f
blue:66.0f/255.0f alpha:1.0].CGColor;
CGColorRef darkColor = [UIColor colorWithRed:37.0/255.0 green:31.0/255.0
blue:32.0/255.0 alpha:1.0].CGColor;
[self drawTriangle];
CGRect viewRect = self.bounds;
drawGlossAndGradient(context, viewRect, lightColor, darkColor);
}
I added the clip suggested below but that just made my background with the gradient and the gloss dissappear and the triangle become gray. Does anyone know what I am doing wrong here?
If you draw this gradient in drawRect: method just add clipping path before it.
Example:
-(void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGPoint vertices[] = {coordinates of vertices};
CGContextBeginPath(ctx);
CGContextAddLines(ctx, vertices, sizeof(vertices)/sizeof(CGPoint));
CGContextClosePath(ctx);
CGContextClip(ctx);
// draw the gradient
}
Vertices - is an array with 7 points. 1 point per each corner of self.bounds and 3 points which are define the triangle.
For example:
(0) (1) (3) (4)
_____ ________________
| \ / |
| V |
| (2) |
(6) |_______________________|(5)
CGPoint vertices[7] = {CGPointZero, // origin
p1, p2, p3, // vertices of the triangle
{self.bounds.size.width, 0},
{self.bounds.size.width, self.bounds.size.height},
{0, self.bounds.size.height}
}
If anybody needs, this is a code to draw a colored triangle in a UITableView without using UIImageView. This method is good because, the size of triangle can vary and it is drawn programmatically. Also, you can probably change colors and make triangle transparent.
#implementation RRTriangleView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, NULL, 0.0, 0.0);
CGPathAddLineToPoint(path, NULL, - self.bounds.size.height / 2.0, self.bounds.size.height / 2.0);
CGPathAddLineToPoint(path, NULL, 0.0, self.bounds.size.height);
CGPathAddLineToPoint(path, NULL, 0.0f, 0.0f);
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
[shapeLayer setPath:path];
[shapeLayer setFillColor:[COLOR_CUSTOM_LIGHT_BLUE CGColor]];
[shapeLayer setStrokeColor:[COLOR_CUSTOM_LIGHT_BLUE CGColor]];
[shapeLayer setPosition:CGPointMake(self.bounds.size.width, 0.0f)];
[[self layer] addSublayer:shapeLayer];
CGPathRelease(path);
}
return self;
}
Init this TriangleView and add it to your cell, when it is selected :
- (RRTriangleView *)triangleView
{
if (! _triangleView) {
_triangleView = [[RRTriangleView alloc] initWithFrame:self.bounds];
_triangleView.layer.backgroundColor = [[UIColor clearColor] CGColor];
_triangleView.clipsToBounds = NO;
}
return _triangleView;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
//[super setSelected:selected animated:animated];
if (selected) {
[self addSubview:self.triangleView];
}
else {
[self.triangleView removeFromSuperview];
}
}
The size of triangleView is like your cell's view, it is transparent and it is drawn above.
I beleive that
[view setBackgroundColor:[UIColor clearColor]];
[view setOpaque:NO];
will make your view transparent.

How to know the current position of circle while it is rotating on custom route path?

I have used this code to animate the circle along a fixed custom route path along two buttons using their x & y coordinates.
Now I want to know the exact location of circle at each route while it is rotating or animating how could I know this?
- (void) animateCicleAlongPath:(NSInteger)pt1:(NSInteger)pt2
{
UIButton *animatebtn1 = (UIButton *)[self.view viewWithTag:pt1];
UIButton *animatebtn2 = (UIButton *)[self.view viewWithTag:pt2];
NSLog(#"%#",animatebtn1);
NSLog(#"%#",animatebtn2);
NSLog(#"%#",NSStringFromCGRect(circleView.frame));
int a1;
int a2;
int b1;
int b2;
a1=animatebtn1.frame.origin.x;
a2=animatebtn1.frame.origin.y;
b1=animatebtn2.frame.origin.x;
b2=animatebtn2.frame.origin.y;
pathAnimation = [CAKeyframeAnimation animationWithKeyPath:#"position"];
[UIView setAnimationDidStopSelector:#selector(animationDidStart:)];
pathAnimation.calculationMode = kCAAnimationPaced;
pathAnimation.fillMode = kCAFillModeForwards;
pathAnimation.removedOnCompletion = YES;
CGMutablePathRef curvedPath = CGPathCreateMutable();
CGPathMoveToPoint(curvedPath, NULL, b1, b2);
NSLog(#"%#",[finalpath description]);
for (int i=0; i<finalpath.count; i++) {
int j=[[finalpath objectAtIndex:i]intValue];
UIButton *animatebtn = (UIButton *)[self.view viewWithTag:j];
int p1=animatebtn.frame.origin.x;
int p2=animatebtn.frame.origin.y;
CGPathAddLineToPoint(curvedPath, NULL, p1,p2);
}
pathAnimation.path = curvedPath;
CGPathRelease(curvedPath);
UIGraphicsBeginImageContext(CGSizeMake(20,20));
// CGContextRef ctx = UIGraphicsGetCurrentContext();
//Set context variables
CGContextSetLineWidth(context, 1.5);
CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
//Draw a circle - and paint it with a different outline (white) and fill color (green)
CGContextAddEllipseInRect(context, CGRectMake(1, 1, 18, 18));
CGContextDrawPath(context, kCGPathFillStroke);
UIImage *circle = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
circleView = [[UIImageView alloc] initWithImage:circle];
[imageScrollView addSubview:circleView];
[imageScrollView bringSubviewToFront:circleView];
[circleView setHidden:NO];
circleView.alpha=1;
pathAnimation.speed=0.5;
pathAnimation.duration=10;
circleView.animationDuration=10;
[circleView.layer addAnimation:pathAnimation forKey:#"moveTheSquare"];
[circleView release];
}
CALayer provides a method, -presentationLayer, which returns a layer with the current animation state applied. So long as you have the layer being animated, you can inspect the current position of animation through the presentation layer.