1
我有一個按鈕,當按下按鈕時應向下移動2個像素,並在按下按鈕時跳回到原始位置。我試過touchesBegan
和touchesEnded
,但那些按鈕不起作用。我也嘗試用IUView交換按鈕,並在touchesEnded
中調用按鈕的方法,但在向下移動之前給了按鈕一個奇怪的延遲。有任何想法嗎?按下時更改按鈕的位置
我有一個按鈕,當按下按鈕時應向下移動2個像素,並在按下按鈕時跳回到原始位置。我試過touchesBegan
和touchesEnded
,但那些按鈕不起作用。我也嘗試用IUView交換按鈕,並在touchesEnded
中調用按鈕的方法,但在向下移動之前給了按鈕一個奇怪的延遲。有任何想法嗎?按下時更改按鈕的位置
您是否嘗試使用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);
}
我會建議使用CGRectOffset代替CGRectMake爲此,語法要短得多:) –
或者說,是的。無論哪種方式應該工作:) – fbrozovic