2014-02-05 49 views
1

我想编码图像视图的控制器来使用按钮移动。创建控制板

内线补按钮代码:

-(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]; 

} 
+0

你是否用'touchUpInside'连接了按钮? –

+0

是@SimonMcLoughlin – shaideru

回答

1

编辑:

你需要什么有2个事件,一个用于当按钮被按下并另一个用于当按钮被释放时如此:

[aButton addTarget:self action:@selector(holdDown) forControlEvents:UIControlEventTouchDown]; 
[aButton addTarget:self action:@selector(holdRelease) forControlEvents:UIControlEventTouchUpInside]; 


- (void)holdDown 
    { 
    NSLog(@"hold Down"); 
    } 

    - (void)holdRelease 
    { 
     NSLog(@"hold release"); 

    } 

让你的按住功能开始一个循环,你的holdRelease停止循环。

EDIT 2:

一个简单的方法(但也许不是最好的方法)来实现这个循环是使用NSTImer scheduledTimerWithTimeInterval保持内部的时候,在释放法而无效。

所有这些东西都已经完成了,请尝试使用Google搜索这些东西。举个例子,这是一个stackoverlfow问题:ios scheduledTimerWithTimeInterval for amount of time

+0

我已经看到了这个链接,我无法弄清楚如何在按住按钮的同时移动图片。 – shaideru

+0

@frustratediOSdeveloper'touchDownRepeat'被多次调用,将其设置为该值,并且当您按住手指时,它将继续调用并运行代码 –

+0

@frustratediOSdeveloper请编辑您的问题并在其中添加代码,那很难读 –