2011-08-21 115 views
1

我有一个按钮,当按下按钮时应向下移动2个像素,并在按下按钮时跳回到原始位置。我试过touchesBegantouchesEnded,但那些按钮不起作用。我也尝试用IUView交换按钮,并在touchesEnded中调用按钮的方法,但在向下移动之前给了按钮一个奇怪的延迟。有任何想法吗?按下时更改按钮的位置

回答

1

您是否尝试使用Touch Down和Touch Up事件?尝试在Touch Down上移动按钮位置并在Touch Up(内部和外部)上将其恢复到其初始值。

编辑:这里是一个代码示例:

- (IBAction)moveButtonDown:(id)sender 
{ 
    UIButton *btn = (UIButton *)sender; 
    btn.frame = CGRectMake(btn.frame.origin.x, btn.frame.origin.y + 2, btn.frame.size.width, btn.frame.size.height); 
} 

- (IBAction)moveButtonUp:(id)sender 
{ 
    UIButton *btn = (UIButton *)sender; 
    btn.frame = CGRectMake(btn.frame.origin.x, btn.frame.origin.y - 2, btn.frame.size.width, btn.frame.size.height); 
} 

分配moveButtonDown:到按钮“向下触摸”事件和moveButtonUp:它的两个“润色内部”和“润色外”事件。经过测试,至少工作,在这里;)

编辑:谢谢fichek,这里与CGRectOffset代替CGRectMake的解决方案:

- (IBAction)moveButtonDown:(id)sender 
{ 
    UIButton *btn = (UIButton *)sender; 
    btn.frame = CGRectOffset(btn.frame, 0, 2); 
} 

- (IBAction)moveButtonUp:(id)sender 
{ 
    UIButton *btn = (UIButton *)sender; 
    btn.frame = CGRectOffset(btn.frame, 0, -2); 
} 
+0

我会建议使用CGRectOffset代替CGRectMake为此,语法要短得多:) –

+0

或者说,是的。无论哪种方式应该工作:) – fbrozovic