我已经查看并找到了使用按钮制作d-pad的代码,但您必须不断点击按钮才能使其正常工作。所以,如果你想上去,你必须经常按下。如何在用户保持按下按钮的情况下移动对象。 这是我已经
按钮的代码-(IBAction)moveLeft:(id)sender{
Robot.center = CGPointMake(Robot.center.x-10, Robot.center.y);
[UIView animateWithDuration:0.5 animations:^{
Robot.center = CGPointMake(Robot.center.x, Robot.center.y+10);
}];
}
-(IBAction)moveRight:(id)sender{
Robot.center = CGPointMake(Robot.center.x+10, Robot.center.y);
[UIView animateWithDuration:0.5 animations:^{
Robot.center = CGPointMake(Robot.center.x, Robot.center.y+10);
}];
}
-(IBAction)moveUp:(id)sender{
Robot.center = CGPointMake(Robot.center.x, Robot.center.y-10);
[UIView animateWithDuration:0.5 animations:^{
Robot.center = CGPointMake(Robot.center.x, Robot.center.y+10);
}];
}
-(IBAction)moveDown:(id)sender{
Robot.center = CGPointMake(Robot.center.x, Robot.center.y+10);
[UIView animateWithDuration:0.5 animations:^{
Robot.center = CGPointMake(Robot.center.x, Robot.center.y+10);
}];
}
答案 0 :(得分:1)
您可以使用UIControlEventTouchDown
控件事件来启动方法运行,使用UIControlEventTouchUpInside
或类似事件来检测按钮何时不再被按下"。
设置按钮的操作,例如:
[myButton addTarget:self action:@selector(startButtonTouch:) forControlEvents:UIControlEventTouchDown];
[myButton addTarget:self action:@selector(endButtonTouch:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
(注意上面的内容会导致按钮内外触摸调用endButtonTouch:方法。)
然后添加startButtonTouch:和endButtonTouch方法,例如:
- (void)startButtonTouch:(id)sender {
// start the process running...
}
- (void)endButtonTouch:(id)sender {
// stop the running process...
}
如果您想在用户拖动按钮时结束,请同时添加UIControlEventTouchDragExit
。
答案 1 :(得分:0)
将按钮设置为向上状态:
- (IBAction)touchUpIn_btnUp:(id)sender {
self.buttonUpIsDown = NO;
}
- (IBAction)touchDownIn_btnUp:(id)sender {
self.buttonUpIsDown = YES;
}
- (IBAction)touchUpIn_btnLeft:(id)sender {
self.buttonLeftIsDown = NO;
}
- (IBAction)touchDownIn_btnLeft:(id)sender {
self.buttonLeftIsDown = YES;
}
- (IBAction)touchUpIn_btnRight:(id)sender {
self.buttonRightIsDown = NO;
}
- (IBAction)touchDownIn_btnRight:(id)sender {
self.buttonRightIsDown = YES;
}
- (IBAction)touchUpIn_btnDown:(id)sender {
self.buttonDownIsDown = NO;
}
- (IBAction)touchDownIn_btnDown:(id)sender {
self.buttonDownIsDown = YES;
}
检查循环中的状态
- (void) updateControls {
if (self.buttonUpIsDown) {
// move up
}
if (self.buttonDownIsDown) {
// move down
}
if (self.buttonLeftIsDown) {
// move left
}
if (self.buttonRightIsDown) {
// move right
}
}
用:
调用循环[NSTimer scheduledTimerWithTimeInterval:MAIN_LOOP_TIME target:self selector:@selector(updateControls) userInfo:nil repeats:YES];