Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

arc4random

Is it possible in Xcode to set an area of the user interface to only show the image at random? for example I have a large square on my interface and i want the dot to only randomize in that specific square instead of all over the screen?

Posted on May 29, 2014 1:53 PM

Reply
15 replies

May 29, 2014 4:14 PM in response to red_menace

Quick question: Are the points inside the xValue based on screen size or what? I'm probably not doing this right cause my dot is still scattered all over the screen instead of in the rectangle area.


-(IBAction)random:(id)sender {


int xValue = arc4random_uniform(100);

int yValue = arc4random_uniform(127);


button.center = CGPointMake(xValue, yValue);

counter = counter +1;

scoreLabel.text = [NSString stringWithFormat:@"Score %i", counter];

}


-(IBAction)start:(id)sender {


int xValue = arc4random_uniform(320);

int yValue = arc4random_uniform(354);

button.center = CGPointMake(xValue, yValue);

May 29, 2014 4:30 PM in response to TDIFF

The center property is for the button frame, so that would be relative to the containing view's coordinate system. If your rectangle area is just some arbitrary location and not a view, you would need to adjust your rectangle coordinates to their relative location within the superview. You might get a better idea of what is happening by making your coordinate values smaller.

May 29, 2014 4:46 PM in response to TDIFF

Red-menace is right use smaller values to start.


0,0 is (normally) the lower left hand corner of the view. If you start there and walk by a value of say 10,10 you should see the button move up to the upper right hand corner,


When it disappears you've gone to the upper limits of the view.


my dot is still scattered all over the screen instead of in the rectangle area.

Not sure what you are saying here. Any drawing you do should be confined to the area of the window of the program. You shouldn't be able to draw outside of that.

May 29, 2014 6:06 PM in response to TDIFF

0,0 being in the upper left hand corner indicates the view is flipped. Are you overriding the isFlipped method in your code?


At this point without seeing the code and knowing more about what you are doing (looked back over your old posts, seems to be some confusion with the program) I can;t really say what's going on.


good luck


regards


Message was edited by: Frank Caggiano

May 29, 2014 8:04 PM in response to TDIFF

IOS, I should have known 😊


Ok this method, lifted almost entirely from Learn Cocoa on the Mac will move the button around randomly on the screen


User uploaded file

Tested in the 7.1 iPhone simulator.


Message was edited by: Frank Caggiano - Hate to post a screen shot of the code but the system would;t let me add the text of the code to the post. Kept getting errors when I tried

May 30, 2014 11:25 AM in response to TDIFF

As with most programming problems there are multiple way to do things. The code I posted works but is more of an OS X solution. I haven;t done to much with IOS so this was a good excuse to play around with it.


The following code (the idea for which came from the book Tap, Shake, Move), takes moving the button and adds a few things that might be helpful.


(lets see if I can post code today)


To use this make a button and connect its action to bmtButtonPressed and its outlet to bmtButton.


Also drag a view out and size it to something like 20x20 and call its outlet puck. Make it black so you can see it against the white screen.


When you press the button two things will happen the button will move to a random spot and the puck will start moving around the screen. Pressing the button again will stop it.


enjoy


//

// BMTViewController.m

// ButtonMove

//

// Created by Frank Caggiano on 5/29/14.

// Copyright (c) 2014 Frank Caggiano. All rights reserved.

//


#import "BMTViewController.h"


@interface BMTViewController ()


@end


@implementation BMTViewController

{

float dx;

float dy;

float speed;

NSTimer *timer;

}


- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

[self reset];

}


- (void) reset

{

// ramdomly select beginning directions

if ((arc4random() %2) == 0)

dx = -1;

else

dx = 1;

if ((arc4random() %2) ==0)

dy = -1;

else

dy = 1;

// Place button and puck at random locations along middle of screen

self.bmtButton.center = CGPointMake(15 + arc4random() % (320 - 30), 240);

self.puck.center = CGPointMake(15 + arc4random() % (320 - 30), 240);

//set the speed of the puck

speed = 2;


}


- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}


- (IBAction)bmtButtonPressed:(id)sender {

//when the button is pressed move button to random spot on screen

self.bmtButton.center = CGPointMake(15 + arc4random() % (320 - 30), 15 + arc4random() % (320 - 30));

// and if the timer is nil start us else stop us.

if (timer == nil)

[self start];

else {

[self stop];

[self reset];

}

}


- (void) animate

{

// move the puck according to the direction and speed we are going.

self.puck.center = CGPointMake(self.puck.center.x + dx * speed, self.puck.center.y + dy * speed);

// check to see if we have hit either the

// Side Walls

[self checkPuckCollision:CGRectMake(-10, 0, 20, 480) dirX:fabsf(dx) dirY:0];

[self checkPuckCollision:CGRectMake(310, 0, 20, 480) dirX:-fabs(dx) dirY:0];

// or the Top or Bottom walls

[self checkPuckCollision:CGRectMake(0, -10, 320, 20) dirX:0 dirY:fabs(dy)];

[self checkPuckCollision:CGRectMake(0, 480, 320, 20) dirX:0 dirY:-fabs(dy)];

}


- (void) start

{

if (timer == nil ) {

timer = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(animate) userInfo:NULL repeats:YES];

}

}


-(void) stop

{

if (timer != nil) {

[timer invalidate];

timer = nil;

}

}


// The return value isn't used here but this seems like it should

- (BOOL) checkPuckCollision: (CGRect) rect dirX: (float) x dirY: (float) y

{

// check to see if the puck has hit the walls and if so reverse direction

if (CGRectIntersectsRect(self.puck.frame, rect)) {

if (x != 0)

dx = x;

if (y != 0)

dy = y;

return TRUE;

}

return FALSE;

}

@end


May 30, 2014 10:17 PM in response to Frank Caggiano

// just testing, if it is possible to post this code in this thread


- (IBAction)bmtButtonPressed:(id)sender {

CGRect senderFrame = [sender frame];

CGRect superBounds = [[sender superview] bounds];

senderFrame.origin.x = (superBounds.size.width - senderFrame.size.width) * drand48();

senderFrame.origin.y (superBounds.size.height - senderFrame.size.height) * drand48();

[sender setFrame:senderFrame];

}

arc4random

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.