我的代码显示单击按钮时,下面的按钮会向下移动。我想知道当再次点击按钮时是否可以这样做,下面的按钮会移回原来的位置。
·H
@interface ViewController : UIViewController {
IBOutlet UIButton *button;
}
-(IBAction)buttonClicked:(id)sender;
-(IBAction)buttonClicked1:(id)sender;
@end
的.m
- (void)buttonClicked:(id)sender {
CGRect frame = button.frame;
frame.origin.x = 65; // new x coordinate
frame.origin.y = 130; // new y coordinate
button.frame = frame;
}
答案 0 :(得分:1)
您可以使用BOOL
跟踪状态并存储startRect以便将按钮移回
·H
@interface ViewController : UIViewController {
BOOL buttonShifted;
CGRect startRect;
IBOutlet UIButton *button;
}
-(IBAction)buttonClicked:(id)sender;
-(IBAction)buttonClicked1:(id)sender;
@end
的.m
// make sure to set the original button frame when view loads
// ie startRect = button.frame;
- (void)buttonClicked:(id)sender {
if (buttonShifted) {
button.frame = startRect;
} else {
CGRect frame = button.frame;
frame.origin.x = 65; // new x coordinate
frame.origin.y = 130; // new y coordinate
button.frame = frame;
}
buttonShifted = !buttonShifted;
}