我想编码图像视图的控制器来使用按钮移动。创建控制板
内线补按钮代码:
-(IBAction)upButton:(id)sender{
mainChac.center = CGPointMake(mainChac.center.x, mainChac.center.y - 5);
}
代码工作正常,但我必须点击它反复不断地移动,我想知道调用哪个方法,来移动它,按住向上按钮。
编辑:
-(void)touchDownRepeat{
mainChac.center = CGPointMake(mainChac.center.x, mainChac.center.y - 5);
}
-(IBAction)upButton:(id)sender{
[upButton addTarget:self action:@selector(touchDownRepeat) forControlEvents:UIControlEventTouchDownRepeat];
}
编辑2:解决
-(void)moveUp{
mainChac.center = CGPointMake(mainChac.center.x, mainChac.center.y - 5);
}
- (void)holdDown
{
NSLog(@"hold Down");
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(moveUp) userInfo:nil repeats:YES];
}
- (void)holdRelease
{
NSLog(@"hold release");
[timer invalidate];
}
-(IBAction)upButton:(id)sender{
[upButton addTarget:self action:@selector(holdDown) forControlEvents:UIControlEventTouchDown];
[upButton addTarget:self action:@selector(holdRelease) forControlEvents:UIControlEventTouchUpInside];
}
你是否用'touchUpInside'连接了按钮? –
是@SimonMcLoughlin – shaideru