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

  • Home
  • SEARCH
  • 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 8651057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T13:58:09+00:00 2026-06-12T13:58:09+00:00

I’m creating a simple app where when the user presses a button, a series

  • 0

I’m creating a simple app where when the user presses a button, a series of lines will be drawn on the screen and the user will be able to see these lines drawn in real time (almost like an animation).

My code looks something like this (has been simplified):

UIGraphicsBeginImageContext(CGSizeMake(300,300));
CGContextRef context = UIGraphicsGetCurrentContext();

for (int i = 0; i < 100; i++ ) {
    CGContextMoveToPoint(context, i, i);
    CGContextAddLineToPoint(context, i+20, i+20);
    CGContextSetStrokeColorWithColor(context, [[UIColor blackColor] CGColor]);
    CGContextStrokePath(context);
}

UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

My problem is that:

1) As soon as the user presses the button, the UIThread blocks until the drawing is done.

2) I can’t get the lines to be drawn on the screen one at a time – I’ve tried setting the UIImage directly inside the loop and also tried setting a layer content inside the loop.

How do I get around these problems?

  • 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-06-12T13:58:11+00:00Added an answer on June 12, 2026 at 1:58 pm

    You say “just like an animation”. Why not do an actual animation, a la Core Graphics’ CABasicAnimation? Do you really need to show it as a series of lines, or is a proper animation ok?

    If you want to animate the actual drawing of the line, you could do something like:

    #import <QuartzCore/QuartzCore.h>
    
    - (void)drawBezierAnimate:(BOOL)animate
    {
        UIBezierPath *bezierPath = [self bezierPath];
    
        CAShapeLayer *bezier = [[CAShapeLayer alloc] init];
    
        bezier.path          = bezierPath.CGPath;
        bezier.strokeColor   = [UIColor blueColor].CGColor;
        bezier.fillColor     = [UIColor clearColor].CGColor;
        bezier.lineWidth     = 5.0;
        bezier.strokeStart   = 0.0;
        bezier.strokeEnd     = 1.0;
        [self.view.layer addSublayer:bezier];
    
        if (animate)
        {
            CABasicAnimation *animateStrokeEnd = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
            animateStrokeEnd.duration  = 10.0;
            animateStrokeEnd.fromValue = [NSNumber numberWithFloat:0.0f];
            animateStrokeEnd.toValue   = [NSNumber numberWithFloat:1.0f];
            [bezier addAnimation:animateStrokeEnd forKey:@"strokeEndAnimation"];
        }
    }
    

    Then all you have to do is create the UIBezierPath for your line, e.g.:

    - (UIBezierPath *)bezierPath
    {
        UIBezierPath *path = [UIBezierPath bezierPath];
    
        [path moveToPoint:CGPointMake(0.0, 0.0)];
    
        [path addLineToPoint:CGPointMake(200.0, 200.0)];
    
        return path;
    }
    

    If you want, you can patch a bunch of lines together into a single path, e.g. here is a roughly sine curve shaped series of lines:

    - (UIBezierPath *)bezierPath
    {
        UIBezierPath *path = [UIBezierPath bezierPath];
        CGPoint point = self.view.center;
    
        [path moveToPoint:CGPointMake(0, self.view.frame.size.height / 2.0)];
    
        for (CGFloat f = 0.0; f < M_PI * 2; f += 0.75)
        {
            point = CGPointMake(f / (M_PI * 2) * self.view.frame.size.width, sinf(f) * 200.0 + self.view.frame.size.height / 2.0);
            [path addLineToPoint:point];
        }
    
        return path;
    }
    

    And these don’t block the main thread.

    By the way, you’ll obviously have to add the CoreGraphics.framework to your target’s Build Settings under Link Binary With Libraries.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
Seemingly simple, but I cannot find anything relevant on the web. What is the
I need a function that will clean a strings' special characters. I do NOT
I am writing an app with both english and french support. The app requests
I am using Paperclip to handle profile photo uploads in my app. They upload

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.