1

This should be easy.

I've got a method to move a view up and down and while doing so I move a UIButton over and down... then move the button back to its original position when the method runs again.

I thought I could get the original position of the button with float originalCenterX = topSubmitButton.center.x and float originalCenterY = topSubmitButton.center.y but of course those are overwritten with the center of the button when the method is hit for the second time.

How does one preserve a variable over multiple iterations of a method?

-(IBAction)scrollForComment { 

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5]; 

CGRect rect = self.view.frame;

NSLog(@"button center x = %f y = %f",topSubmitButton.center.x,topSubmitButton.center.y);
float originalCenterX = topSubmitButton.center.x;
float originalCenterY = topSubmitButton.center.y;

if (commentViewUp) {
    rect.origin.y = self.view.frame.origin.y + 80;// move down view by 80 pixels
    commentViewUp = NO;

    CGPoint newCenter = CGPointMake( 57.0f , 73.0f); // better to calculate this if you are going to rotate to landscape

    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.5f];
    topSubmitButton.center = newCenter;
    [UIView commitAnimations];

} else { // toggling the view's location
    rect.origin.y = self.view.frame.origin.y - 80;// move view back up 80 pixels
    commentViewUp = YES;


    CGPoint newCenter = CGPointMake(160 , 74.0f + topSubmitButton.center.y);// would like to calculate center
    [UIView beginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.5f];
    topSubmitButton.center = newCenter;
    [UIView commitAnimations];
  }

self.view.frame = rect;

[UIView commitAnimations];

}

Bonus if you can tell me how to put the center of a view in a CGPoint's x value.

Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
Michael Morrison
  • 1,323
  • 1
  • 18
  • 30
  • I'm guessing you'll want to store the value in a member variable or global variable (depending on if you have a member function, or global function). I don't really know Objective-C, tho, so I won't post this as an answer. – Merlyn Morgan-Graham Jul 17 '11 at 21:55
  • Also, you can get the "center" of any sort of rectangle by taking the width, dividing by two, and adding to the x-offset of the left-hand side of the rectangle (e.g. if it is shifted some number of units to the right). – Merlyn Morgan-Graham Jul 17 '11 at 21:57

1 Answers1

1

You can use a static variable to preserve its contents between method calls. However, the initializer must be constant, so you cannot call a method to initialize it. What you can do is make the initial value invalid, and test for this before setting the variable.

-(IBAction)scrollForComment {
    static float originalCenterX = −1, originalCenterY = −1; // Assuming −1 would be an invalid value
    if(originalCenterX == −1 && originalCenterY == −1) {
        CGPoint temp = topSubmitButton.center;
        originalCenterX = temp.x;
        originalCenterY = temp.y;
    }
    ...

Bonus if you can tell me how to put the center of a view in a CGPoint's x value.

I am not sure what you meant by this. If you want to be able to set only the x coordinate of the center, you will need to get the current center, change the x coordinate, and save the new point.

CGPoint temp = topSubmitButton.center;
temp.x = newXValue;
topSubmitButton.center = temp;
ughoavgfhw
  • 39,734
  • 6
  • 101
  • 123
  • Thanks. By "put the center of a view in a CGPoint's x value" I mean that this is invalid CGPoint newCenter = CGPointMake( self.view.frame center , 73.0f); "Bad receiver type 'CGRect'. I am moving the UIButton to the center of the screen and I'm using 160 now, which works but if I enable rotate to landscape that won't work. – Michael Morrison Jul 17 '11 at 23:54
  • 1
    @Michael You just need to use the center property and get the x coordinate of the result. `CGPoint newCenter = CGPointMake(self.view.center.x, 73.0f)`. Alternatively, you can add half of the frame's width to its x coordinate. `CGPoint newCenter = CGPointMake(self.view.frame.origin.x + self.view.frame.size.width / 2, 73.0f)`. – ughoavgfhw Jul 18 '11 at 02:32
  • The static didn't work however but I am accepting the answer because I assume this is the correct way and I must be doing something else wrong. – Michael Morrison Jul 18 '11 at 15:14
  • static will keep the value between method calls, but won't prevent you from modifying it in future calls. Make sure you never change the variable without checking if it was already set. – ughoavgfhw Jul 18 '11 at 22:25