user1625435
user1625435

Reputation: 153

random CGPointMake of uiimageview

How can I change this number "63" in my code?

-(void)up
{


    [UIView animateWithDuration:[self randomFloatBetween:1.0 and:0.3] animations:^{
        m1.center = CGPointMake(63, 210);
    }];


   [self performSelector:@selector(down) withObject:nil afterDelay:[self randomFloatBetween:1.0 and:0.5]];
 }

Number 63 from my CGPointMake should be random. So For number 63 I need to be random these numbers: 160 & 257.

My "m1" image, appear at CGPointMake(63, 210), BUT I want to make Point 63 random, to change from 63 to 160 or 257. RANDOM. Thnaks a lot.

Upvotes: 0

Views: 163

Answers (2)

Mick MacCallum
Mick MacCallum

Reputation: 130222

-(void)up
{
    NSArray *myArray = [NSArray arrayWithObjects:[NSNumber numberWithFloat:63],[NSNumber numberWithFloat:160],[NSNumber numberWithFloat:257], nil];
    [UIView animateWithDuration:[self randomFloatBetween:1.0 and:0.3] animations:^{
        m1.center = CGPointMake([[myArray objectAtIndex:arc4random() % [myArray count]] floatValue], 210);
    }];
    [self performSelector:@selector(down) withObject:nil afterDelay:[self randomFloatBetween:1.0 and:0.5]];
}

Maybe this will help.

Upvotes: 1

Martol1ni
Martol1ni

Reputation: 4702

Add the desired numbers to an NSArray, and select the number with [yourArray objectAtIndex:(arc4random()%yourArray.count)];

Upvotes: 1

Related Questions