【发布时间】:2011-10-07 07:22:54
【问题描述】:
这应该很容易。
我有一种上下移动视图的方法,在此过程中,我上下移动了一个 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 值中,那就太好了。
【问题讨论】:
-
我猜你会想要将值存储在成员变量或全局变量中(取决于你是否有成员函数或全局函数)。我真的不了解 Objective-C,所以我不会将其作为答案发布。
-
此外,您可以通过取宽度、除以二并添加到矩形左侧的 x 偏移量来获得任何类型矩形的“中心”(例如,如果它向右移动了一些单位)。
标签: objective-c ios animation uibutton cgpoint