2
我的窗口上有兩個UIViews:一個用來保存玩家分數,(側邊欄)和一個主要玩區域。它們都適合UIWindow,既不滾動。用戶可以在主要遊戲區域拖動UIButtons - 但目前,他們可以將它們拖放到側邊欄上。一旦他們這樣做了,他們就不能再拖拽它們,大概是因爲你在第二個視圖上敲擊,而第二個視圖不包含有問題的按鈕。防止在UIView之外拖動UIButton
我想阻止主視圖內的任何內容移動到側欄視圖中。我管理好了這一點,但是如果玩家的手指離開視圖,我需要釋放拖動。使用下面的代碼,按鈕會隨着手指一直移動,但不會超過視圖的X座標。我怎麼去解決這個問題?
[firstButton addTarget: self action: @selector(wasDragged: withEvent:) forControlEvents: UIControlEventTouchDragInside];
這種方法:拖動時,使用此調用啓用
- (void) wasDragged: (UIButton *) button withEvent: (UIEvent *) event
{
if (button == firstButton) {
UITouch *touch = [[event touchesForView:button] anyObject];
CGPoint previousLocation = [touch previousLocationInView:button];
CGPoint location = [touch locationInView:button];
CGFloat delta_x = location.x - previousLocation.x;
CGFloat delta_y = location.y - previousLocation.y;
if ((button.center.x + delta_x) < 352)
{
button.center = CGPointMake(button.center.x + delta_x, button.center.y + delta_y);
} else {
button.center = CGPointMake(345, button.center.y + delta_y);
}
}
}