開始於touchesBegan
,touchesMoved
,touchesEnded
。在你的UIView子類中重寫這些,你將會學習事件系統。你可以得到像這樣的事件座標:
- (void) touchesBegan:(NSSet *) touches withEvent:(UIEvent *) event
{
float x = [[touches anyObject] locationInView:self].x;
float y = [[touches anyObject] locationInView:self].y;
}
然後有很多東西可以在不同的視圖之間轉換座標等等。一旦你瞭解了這一點,你就可以使用你已經找到的UIGestureRecognizer
東西,這就是你需要的東西。
您將需要一個平移手勢識別器來進行拖放操作。您可以在UIPanGestureRecognizer
中使用locationInView:
選擇器來查找您在任何給定時刻的位置。
你把你的手勢識別,像這樣,不與目標 - 動作的東西你想:
UIPanGestureRecognizer *dragDropRecog = [[UIPanGestureRecognizer alloc] initWithTarget:yourView action:@selector(thingDragged:)];
[yourView addGestureRecognizer:dragDropRecog];
然後你有你的觀點,以實現選擇thingDragged:
:
- (void) thingDragged:(UIPanGestureRecognizer *) gesture
{
CGPoint location = [gesture locationInView:self];
if ([gesture state] == UIGestureRecognizerStateBegan) {
// Drag started
} else if ([gesture state] == UIGestureRecognizerStateChanged) {
// Drag moved
} else if ([gesture state] == UIGestureRecognizerStateEnded) {
// Drag completed
}
}
你將轉換正在拖動的視圖,並處理結束部分的拖放。
用戶必須在viewDidLoad中添加此: UIPanGestureRecognizer *聲像= [[UIPanGestureRecognizer頁頭] initWithTarget:自我行動:@selector(panWasRecognized: )]; [_viewaddGestureRecognizer:panner]; –
或者您可以在故事板或XIB中添加手勢識別器。 –
這是正確的,但當我跟着你的答案我不知道它需要,所以它不適用於我:D –