Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在代码中移动UI按钮_Objective C_Ios_Uibutton - Fatal编程技术网

Objective c 在代码中移动UI按钮

Objective c 在代码中移动UI按钮,objective-c,ios,uibutton,Objective C,Ios,Uibutton,我有一段代码,当点击一个按钮时,下面的按钮会向下移动。我想知道,当再次单击按钮时,下面的按钮是否可以移回其原始位置 .h @interface ViewController : UIViewController { IBOutlet UIButton *button; } -(IBAction)buttonClicked:(id)sender; -(IBAction)buttonClicked1:(id)sender; @end .m - (void)buttonClicked:(id

我有一段代码,当点击一个按钮时,下面的按钮会向下移动。我想知道,当再次单击按钮时,下面的按钮是否可以移回其原始位置

.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;
}

您可以使用
BOOL
来跟踪状态,并存储用于将按钮向后移动的startRect

h

m

@interface ViewController : UIViewController  {
BOOL buttonShifted;
CGRect startRect;
IBOutlet UIButton *button;
}

-(IBAction)buttonClicked:(id)sender;
-(IBAction)buttonClicked1:(id)sender;

@end
// 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;
}