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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:48:49+00:00 2026-06-05T07:48:49+00:00

The following files load a series of shapes into a UIViewController. Each shape is

  • 0

The following files load a series of shapes into a UIViewController. Each shape is placed randomly on the screen. I can use the following code to change the shape of the image horizontally, but I cannot move the x and y coordinates of the image on the UIView. How can I move the shape to a different location on screen? The following changes the width of the UIView:

 [UIView animateWithDuration:0.5f animations:^{[[timer userInfo] setBounds:CGRectMake(0, 0, 100, 5)];}];

ViewController.h

#import <UIKit/UIKit.h>
#import "Shape.h"

@interface ViewController : UIViewController

@end

ViewController.m

#import "ViewController.h"

@implementation ViewController

UIView *box;
int screenHeight;
int screenWidth;
int x;
int y;
Shape * shape;
- (void)viewDidLoad
{
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    screenHeight = screenRect.size.height;
    screenWidth = screenRect.size.width;
    box = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 5)];     
    [self.view addSubview:box];
    for (int i = 0; i<3; i++) {
        x = arc4random() % screenWidth;
        y = arc4random() % screenHeight;
        shape =[[Shape alloc] initWithX:x andY:y];
        [box addSubview:shape];     
        [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(moveTheShape:) userInfo:shape repeats:YES];     
    }
}
-(void) moveTheShape:(NSTimer*)timer
{
    //[UIView animateWithDuration:0.5f animations:^{[[timer userInfo] setBounds:CGRectMake(100, 0, 100, 5)];}];
    [UIView animateWithDuration:0.5f animations:^{[[timer userInfo] setBounds:CGRectMake(0, 0, 100, 5)];}];
}
@end

Shape.h

#import <UIKit/UIKit.h>

@interface Shape : UIView; 

- (id) initWithX: (int)xVal andY: (int)yVal;

@end

Shape.m

#import "Shape.h"

@implementation Shape 

- (id) initWithX:(int )xVal andY:(int)yVal {
    self = [super initWithFrame:CGRectMake(xVal, yVal, 5, 5)];  
    self.backgroundColor = [UIColor redColor];  
    return self;
}

@end
  • 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-05T07:48:52+00:00Added an answer on June 5, 2026 at 7:48 am

    In your moveTheShape method, you need to set the frame, not the bounds, and set the x and y values in the CGRectMake to something other than 0.

    You can get your original x and y values in the moveTheShape method like this:

     -(void) moveTheShape:(NSTimer*)timer {
            CGRect frame = [timer.userInfo frame];
            float frameX = frame.origin.x;
            float frameY = frame.origin.y;
            NSLog(@"X component is:%f   Y component is:%f",frameX,frameY);
            [UIView animateWithDuration:0.5f animations:^{[[timer userInfo] setFrame:CGRectMake(200, 100, 5, 5)];}];
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use the following code # LOAD XML FILE $XML = new
The following code: with open(J:\\python\\.data) as data: self.data=pickle.load(data) generated the following error: File J:\python\code.py,
The following two lines of code load an image from an SVG file and
If I load a txt file into my flash movie using the following... var
I've got the following code files = di.GetFiles(*.jpg); for (int i = 0; i
I have implemented the ability to load 3DS files into an OpenGL program of
I have included the following header files in a C code using openssl libraries.
Im trying to load series of external Swfs into a Movieclip on stage. The
I used DataSet to load a schema from following xml file; <node id=0> <node
When running rspec I get the following error: no such file to load --

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.