LodgeApps
LodgeApps

Reputation: 344

Moving a UIbutton to a random position with timer

I am making a simple app in Objective C where every time a button is clicked, the button changes location on the screen. The first time the button is clicked, a timer begins.

The issue is every second the timer goes down, the button returns to its original position. How do I make it so that the timer does not effect the button moving?

Here is my code in my ViewController.m file:

Note: I cannot figure this out at all. I will probably need someone to really spell it out for me to tell me what to do. Thanks!

Note: My UIButton is "blackDot"

-(IBAction) mainClickButton{


int xmin = ([blackDot frame].size.width)/2;
int ymin = ([blackDot frame].size.height)/2;

int x = xmin + arc4random_uniform(self.view.frame.size.width - blackDot.frame.size.width);
int y = ymin + arc4random_uniform(self.view.frame.size.height - blackDot.frame.size.height);

[blackDot setCenter:CGPointMake(x, y)];


if (timerBeginInt < 1) {

    [timerLabel setText:@"Time : 0:10"];
    [self.view addSubview:timerLabel];
    currMinute=0;
    currSeconds=10;
    [self start];

}

timerBeginInt = timerBeginInt + 1;


}





-(void)start
{
    timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];

}
-(void)timerFired
{
    if((currMinute>0 || currSeconds>=0) && currMinute>=0)
    {
        if(currSeconds==0)
        {
            currMinute-=1;
            currSeconds=59;
        }
        else if(currSeconds>0)
        {
            currSeconds-=1;
        }
        if(currMinute>-1)
            [timerLabel setText:[NSString stringWithFormat:@"%@%d%@%02d",@"Time : ",currMinute,@":",currSeconds]];
    }
    else
    {
        [timer invalidate];
        timerBeginInt = 0;
    }
}

Any help is appreciated! Thanks!

Upvotes: 1

Views: 96

Answers (2)

Henson Fang
Henson Fang

Reputation: 1207

LodgeApps.

i copy your code, run it with my own Xcode,and i find your problem which confuses you.

short solution is here:

open your storyboard or xib file,keep this off. enter image description here

if you want to keep Autolayout on,

[self.view removeConstraints:blackDot.constraints];

NSLayoutConstraint* leftConstraint = [NSLayoutConstraint constraintWithItem:blackDot attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0f constant:x];

NSLayoutConstraint* topConstraint = [NSLayoutConstraint constraintWithItem:blackDot attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:y];

leftConstraint.active = YES;
topConstraint.active = YES;

replace the setCenter

for deeper reason:

with autolayout property on,you can not change the frame of IBOutlet,every attempt to modify frame or center is illegal.You can only change the constraints to move the IBOutlet.

what confuses me:

According to the code,click the button,the button will move at once.but it won't.Because method like setCenter or setFrame won't work in the Autolayout. Autolayout needs constraints to put the UI(IBOutlets) in the right place.If you keep Autolayout on,and then you do not make constraints on IBs,anything could happens which i can't explain.

Upvotes: 1

Burhan Ahmad
Burhan Ahmad

Reputation: 728

Replace this [blackDot setCenter:CGPointMake(x, y)]; with this

[blackDot setFrame:CGRectMake(x,y,blackDot.frame.size.width,blackDot.frame.size.height)];

Also put breakpoints and log the value of x, y,xmin,ymin to confirm that the new position is different than the old one. I hope this will help you out.

Upvotes: 0

Related Questions