Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6783361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:51:16+00:00 2026-05-26T16:51:16+00:00

– (void)drawRect:(CGRect)rect { CGContextRef s = UIGraphicsGetCurrentContext(); CGContextSetRGBFillColor(s, 250, 5, 5, 1); CGContextFillRect(s, CGRectMake(2,

  • 0
- (void)drawRect:(CGRect)rect {
    CGContextRef s = UIGraphicsGetCurrentContext();
    CGContextSetRGBFillColor(s, 250, 5, 5, 1);
    CGContextFillRect(s, CGRectMake(2, 2, 2, 2));

    [UIView beginAnimations:nil context:s];
    [UIView setAnimationDuration: .5];
    [UIView setAnimationDelegate:self];
    //animation goes here
    vector<CGPoint>::iterator L = dp.dPoints->begin();
    while(L != dp.dPoints->end()){
        CGContextAddLineToPoint(s, (*L).x, (*L).y);
        ++L;
    }
    [UIView commitAnimations];
}

What i would like to do is take a rectangle made in CGContext and with the use of UIViews animation, it trace (in an endless Animation) a stroke line created with CGContext.
I am rather unsure how i would go about tracing my line…
Any help, links or comments would be great thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:51:17+00:00Added an answer on May 26, 2026 at 4:51 pm

    http://blog.devedup.com/index.php/2010/03/03/iphone-animate-an-object-along-a-path/

    draw a curved line

        //This draws a quadratic bezier curved line right across the screen
        - ( void ) drawACurvedLine {
            //Create a bitmap graphics context, you will later get a UIImage from this
            UIGraphicsBeginImageContext(CGSizeMake(320,460));
            CGContextRef ctx = UIGraphicsGetCurrentContext();
    
            //Set variables in the context for drawing
            CGContextSetLineWidth(ctx, 1.5);
            CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);
    
            //Set the start point of your drawing
            CGContextMoveToPoint(ctx, 10, 10);
            //The end point of the line is 310,450 .... i'm also setting a reference point of 10,450
            //A quadratic bezier curve is drawn using these coordinates - experiment and see the results.
            CGContextAddQuadCurveToPoint(ctx, 10, 450, 310, 450);
            //Add another curve, the opposite of the above - finishing back where we started
            CGContextAddQuadCurveToPoint(ctx, 310, 10, 10, 10);
    
            //Draw the line
            CGContextDrawPath(ctx, kCGPathStroke);
    
            //Get a UIImage from the current bitmap context we created at the start and then end the image context
            UIImage *curve = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
    
            //With the image, we need a UIImageView
            UIImageView *curveView = [[UIImageView alloc] initWithImage:curve];
            //Set the frame of the view - which is used to position it when we add it to our current UIView
            curveView.frame = CGRectMake(1, 1, 320, 460);
            curveView.backgroundColor = [UIColor clearColor];
            [self addSubview:curveView];
        }
    

    keyframe animation, and a path that is the same as the line we just drew. We will also draw a circle, and animate it along that path:

    - (void) animateCicleAlongPath {
        //Prepare the animation - we use keyframe animation for animations of this complexity
        CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        //Set some variables on the animation
        pathAnimation.calculationMode = kCAAnimationPaced;
        //We want the animation to persist - not so important in this case - but kept for clarity
        //If we animated something from left to right - and we wanted it to stay in the new position,
        //then we would need these parameters
        pathAnimation.fillMode = kCAFillModeForwards;
        pathAnimation.removedOnCompletion = NO;
        pathAnimation.duration = 5.0;
        //Lets loop continuously for the demonstration
        pathAnimation.repeatCount = 1000;
    
        //Setup the path for the animation - this is very similar as the code the draw the line
        //instead of drawing to the graphics context, instead we draw lines on a CGPathRef
        CGPoint endPoint = CGPointMake(310, 450);
        CGMutablePathRef curvedPath = CGPathCreateMutable();
        CGPathMoveToPoint(curvedPath, NULL, 10, 10);
        CGPathAddQuadCurveToPoint(curvedPath, NULL, 10, 450, 310, 450);
        CGPathAddQuadCurveToPoint(curvedPath, NULL, 310, 10, 10, 10);
    
        //Now we have the path, we tell the animation we want to use this path - then we release the path
        pathAnimation.path = curvedPath;
        CGPathRelease(curvedPath);
    
        //We will now draw a circle at the start of the path which we will animate to follow the path
        //We use the same technique as before to draw to a bitmap context and then eventually create
        //a UIImageView which we add to our view
        UIGraphicsBeginImageContext(CGSizeMake(20,20));
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        //Set context variables
        CGContextSetLineWidth(ctx, 1.5);
        CGContextSetFillColorWithColor(ctx, [UIColor greenColor].CGColor);
        CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);
        //Draw a circle - and paint it with a different outline (white) and fill color (green)
        CGContextAddEllipseInRect(ctx, CGRectMake(1, 1, 18, 18));
        CGContextDrawPath(ctx, kCGPathFillStroke);
        UIImage *circle = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        UIImageView *circleView = [[UIImageView alloc] initWithImage:circle];
        circleView.frame = CGRectMake(1, 1, 20, 20);
        [self addSubview:circleView];
    
        //Add the animation to the circleView - once you add the animation to the layer, the animation starts
        [circleView.layer addAnimation:pathAnimation forKey:@"moveTheSquare"];
    }
    

    To get all this running, you can use this init method:

    - (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self drawACurvedLine];
        [self animateCicleAlongPath];
        }
        return self;
    }
    

    and use something like this in your ViewController

    - (void)viewDidLoad {
        UIView *customView = [[Canvas alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
        customView.backgroundColor = [UIColor blackColor];
        [self.view addSubview:customView];
        [customView release];
        [super viewDidLoad];
    }
    

    Also…don’t forget to add your Quartz import:

    #import <QuartzCore/QuartzCore.h>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
My NSXMLParser breaks on this string: <title>AAA &#8211; BCDEFGQWERTYUIO</title> I parsed it in this
I've got the following login script.. <?php $name = $_POST[name]; $password = $_POST[password]; $query
So to start, I have an array of XML files. These files need to
Have a webpage that will be viewed by mainly IE users, so CSS3 is
I'm trying to craft a Java regular expression to split strings of the general
The problem with unsigned char. I am reading a PPM image file which has
I am currently running into a problem where an element is coming back from
i have this code: <?php $valid_ext = array(pdf, doc); $args = array( 'post_type' =>
i have a text file with the following data: Calculated Concentrations 30.55 73.48 298.25

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.