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 7079729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:42:28+00:00 2026-05-28T06:42:28+00:00

How can I override the drawRect method of UIView such that each time I

  • 0

How can I override the drawRect method of UIView such that each time I call [myView setNeedsDisplay] it will draw a new random circle on the screen with a random color. It should not replace old circles. So, it should just look like a bunch of circles stacking up all over the screen, one after another.

  • 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-28T06:42:28+00:00Added an answer on May 28, 2026 at 6:42 am

    You can’t use drawRect: in this way. It is designed to draw the entire view inside that rect, so you would have to draw all of the balls at once, not just the ball you’re adding. Every time drawRect is called, the area of the screen you are drawing into has already been wiped of it’s previous state.

    My initial thought was to create a view for each individual ball, with a random size and position. But the SonOfSillyBalls example code (which does exactly what you’re trying to do) takes a different approach.

    It’s NSView subclass sets a repeating timer to fire a drawAnother: method, which does [self lockFocus], then draws a random ball, and then [self unlockFocus] to finish drawing. Finally, it calls [self.window flushWindow] to get the new ball onto the screen.

    Since it does exactly what you’re trying to do, perhaps you shouldn’t read it (where’s the challenge in that!), but the sample code for SonOfSillyBalls is here: http://developer.apple.com/library/mac/#samplecode/SonOfSillyBalls/

    You might want to check out the read me for an explanation of the overall problem and why it’s not an ideal fit for modern drawing APIs (note: it’s old, so in places it refers to Mac OS X as “Open Step” and “Rhapsody” (the “X” name hadn’t been invented yet) and Classic Mac OS just as “Mac OS”)

    Silly Balls is one of Apple’s oldest pieces of sample code, originally from back in the 1980’s, but this is the third (i think?) revision, updated only six months ago to use Objective-C 2.0.

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

Sidebar

Related Questions

I created a subclass of UIView that overrides drawRect: and uses AddArcToPoint() to draw
ObjC has a very unique way of overriding methods. Specifically, that you can override
Can you override method in java without using annotations? Because eclipse doesn't support it
My ultimate goal is to have a dry base repository that I can override
I wanted to know, how I can override the jsessionid-cookie setting such as httpoly,
I can override global operator new with different parameters, so for example I can
We can override the paintComponent(Graphics g) method of JComponent . It is called automatically
I know you can override delete and save methods in DJango models, but can
In Kohana 2 you can override the default error page by creating a custom
I wondered if/how I can override the default [Authorize] behavior in ASP.NET MVC. I

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.