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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:12:38+00:00 2026-05-23T10:12:38+00:00

well here is my code : – (CGPoint)randomPointSquare { CGRect frame = [[self view]

  • 0

well here is my code :

- (CGPoint)randomPointSquare {
CGRect frame = [[self view] frame];

//CGFloat rand_x = ((float)arc4random() / (float)UINT_MAX) * (float)_window.frame.size.width;

NSInteger side = arc4random() / (UINT_MAX/4);
CGFloat offset = 0;

switch(side) {
    case 0: /* top */
        offset = ((float)arc4random() / (float)UINT_MAX) * (float)frame.size.width;
        return CGPointMake(offset, -10);

    case 1: /* bottom */
        offset = ((float)arc4random() / (float)UINT_MAX) * (float)frame.size.width;
        return CGPointMake(offset, frame.size.height-150);

    case 2: /* left */
        offset = ((float)arc4random() / (float)UINT_MAX) * (float)frame.size.height;
        return CGPointMake(-10, offset);

    default:
    case 3: /* right */
        offset = ((float)arc4random() / (float)UINT_MAX) * (float)frame.size.height;
        return CGPointMake(frame.size.width+170, offset);
}
}



-(void)createNewImageView {
UIImage * image = [UIImage imageNamed:@"abouffer_03.png"];
imageView = [[UIImageView alloc] initWithImage:image];


// Add it at a random point
[imageView setCenter:[self randomPointSquare]];
[[self view] addSubview:imageView];

// Animate it into place
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:8.0f];
[imageView setCenter:CGPointMake(240, 160)];
[UIView commitAnimations];
 [imageView release];
}

SO as you can see there is a UIView animation, but now I want to change and replace it by a Timer. But I don’t know how to do it . Well, if you know how to do it, don’t forget that I want that the imageView is created at a random position and then it moves to the center of the screen. thank you

  • 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-23T10:12:39+00:00Added an answer on May 23, 2026 at 10:12 am

    Rather than writing all the code for you, I’ll give you some pointers that might help you change it (the best way of learning, I think).

    First, when you create the position in the call to randomPointSqaure, store the point created as an instance variable.

    theLocation = [self randomPointSquare];
    

    Then set the location of the imageView to be this

    [imageView setCenter:theLocation];
    

    Calculate how far the object is from the point you want it to be (240, 160) and divide that by the amount of time you want it to move over. For example, if your location is (0, 160) then it will be travelling 240 pixels. If you want it over 10 seconds, it will be 24 pixels per second. Save this in an instance variable as the “delta”

    Now create a timer that calls a method to update the position

    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(updatePosition) userInfo:nil repeats:YES];
    

    And then create the updatePosition method

    - (void)updatePosition {
        ...
        // Calculate the new position
        // delta is divided by 100 as the timer runs every 0.01 second
        // and delta was calculated previously as being distance per second
        theLocation = theLocation + (delta / 100);
        ...
        [imageView setCenter:theLocation];
    
        // Check if the location is at the "collision point" and do whatever if it is
        if (theLocation.x == 240 && theLocation.y == 160) {
           // We've hit the "collision point"
        }
    }
    

    This should hopefully give you some ideas on how you can do it with a Timer instead of animations. This is how a game would perform the same thing – you just update the positions of the objects in small little steps and then check for collisions etc each time.

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

Sidebar

Related Questions

Well here is the API I'm trying to use: http://www.hotelscombined.com/api/LiveRates.asmx?op=HotelSearch Here is the code
Well here is my code to do it. But as you can see, its
I have a code that works well here: #!/usr/bin/perl use strict; use warnings; use
Title should make my problem well described.Here goes my code. <div id=adiv><text>Some text</text></div> <script
Can someone please correct my code here. It works well in most browsers but
I followed the code here: http://drumcoder.co.uk/blog/2010/apr/09/django-reset-password/ as well as here: http://shrenikp.webs.com/apps/blog/entries/show/7133721-implement-forgot-password-on-customer-ui- and here: http://blog.montylounge.com/2009/07/12/django-forgot-password/
Well here's my problem I have three tables; regions, countries, states. Countries can be
Well, here's a nice obscure one. I'm trying to compile the latest open transport
Here's one from the No question's too dumb department: Well, as the subject says:
Well, this is my first post here and really enjoying the site. I have

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.