这应该很容易。
我有一种上下移动视图的方法,在此过程中,我上下移动了一个 UIButton ......然后当该方法再次运行时,将按钮移回其原始位置。
我以为我可以使用 float originalCenterX = topSubmitButton.center.x 和 float originalCenterY = topSubmitButton.center.y 获得按钮的原始位置,但是当第二次点击该方法时,这些按钮当然会被按钮的中心覆盖.
如何在方法的多次迭代中保留变量?
-(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];
}
如果您能告诉我如何将视图的中心放置在 CGPoint 的 x 值中,那就太好了。