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?
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?
You can use the arc4random_uniform(upper_bound) function to generate random numbers within the bounds or frame (however you are using it) of your rectangle.
Would this work for randomizing button locations?
Yes - you would just get some random numbers within whatever range you are wanting and use them for the location of your buttons/views/whatever.
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);
The buttons position is relative to the view that contains it.
See bounds in the NSView class reference.
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.
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.
thanks for the help but i think there might be something wrong with my file because everytime i change the coordinates for example to (0,0) and it appears in top left. And if i change to it to (10,10) it stays in the same place.
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
The UIView coordinate system starts with 0,0 at the top left. Depending on how big the button is, changing the button center to 10,10 might not be enough to move it away from the edge (not sure what it does with stuff outside the view). You might also check to see if any constraints are getting in the way.
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
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
got it
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
// 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];
}
Once I removed all the formatting I was able to post the code shown above.
Something to do with the formatting, jive doesn;t like colors I guess!
arc4random