2012-11-22 102 views
3

我試圖讓我的某個視圖上的某些UIButton實例被觸摸並在屏幕上拖動(最終伴隨着動量,但那是爲了後來!)。我的工作方式非常簡單,如下所示,但問題是,通過觸摸按鈕開始拖動它,它會附着在手指上,並且通過擡起手指,觸發「Touch Up Inside」事件,這是我實際點擊按鈕時要執行的代碼。觸摸並拖動UIButton,但釋放手指時不觸發它

簡而言之:我如何區分水龍頭和拖放?我是否需要將點擊更改爲短按手勢識別器或類似的功能?代碼:

在viewDidLoad中:

[firstButton addTarget: self action: @selector(wasDragged: withEvent:) forControlEvents: UIControlEventTouchDragInside]; 

而且我wasDragged方法:

- (void)wasDragged:(UIButton *)button withEvent:(UIEvent *)event 
{ 
    if (button == letter1Button) { 
     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; 

     button.center = CGPointMake(button.center.x + delta_x, button.center.y + delta_y); 
    } 
} 

回答

15

你可以使用一個UIPanGestureRecognizer並告訴它取消觸摸鑑於...

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    UIPanGestureRecognizer *panRecognizer; 
    panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self 
                  action:@selector(wasDragged:)]; 
    // cancel touches so that touchUpInside touches are ignored 
    panRecognizer.cancelsTouchesInView = YES; 
    [[self draggableButton] addGestureRecognizer:panRecognizer]; 

} 

- (void)wasDragged:(UIPanGestureRecognizer *)recognizer { 
    UIButton *button = (UIButton *)recognizer.view; 
    CGPoint translation = [recognizer translationInView:button]; 

    button.center = CGPointMake(button.center.x + translation.x, button.center.y + translation.y); 
    [recognizer setTranslation:CGPointZero inView:button]; 
} 

- (IBAction)buttonWasTapped:(id)sender { 
    NSLog(@"%s - button tapped",__FUNCTION__); 
} 
+0

其工作正常謝謝 – nivritgupta

+0

感謝這個偉大的提示。 ***請注意,***奇怪的是,我有一種情況:通過故事板進行:您實際上必須在故事板上設置取消觸摸視圖***關閉***:我不知道爲什麼。 – Fattie

1

使用UIButton的UIControlEventTouchDragInside和UIControlEventTouchUpInside事件,如

// add drag listener 
[button addTarget:self action:@selector(wasDragged:withEvent:) forControlEvents:UIControlEventTouchDragInside]; 
// add tap listener 
[button addTarget:self action:@selector(wasTapped:) forControlEvents:UIControlEventTouchUpInside]; 

您可以使用HBDraggableButton控制..

+0

我有一個問題,當我的UIButton被設置爲突出顯示,當用戶從他們拖走(不觸發行動)。我發現這種情況下屬於UIControlEventTouchCancel。你的回答讓我意識到這一點;謝謝。 – vikzilla

1

對於像我這樣的初學者,我試圖UIPanGestureRecognizer按照以上建議,但沒有奏效。所以,這裏是我的簡單的解決辦法: 首先,添加事件偵聽器的建議通過貝格:

// add drag listener 
[button addTarget:self action:@selector(wasDragged:withEvent:) forControlEvents:UIControlEventTouchDragInside]; 
// add tap listener 
[button addTarget:self action:@selector(wasTapped:) forControlEvents:UIControlEventTouchUpInside]; 

兩個阻力和自來水都將觸發UIControlEventTouchUpInside,所以在wasDragged:withEvent:添加一個標誌是這樣的:

-(IBAction)wasDragged: (id)sender withEvent: (UIEvent *) event { 
    was_dragged = YES; 
    UIButton *selected = (UIButton *)sender; 
    selected.center = [[[event allTouches] anyObject] locationInView:self.view]; 
} 

- (IBAction)buttonWasTapped:(id)sender { 
    if(!was_dragged) 
     NSLog(@"button tapped"); 
    else{ 
     was_dragged = NO; 
     NSLog(@"button dragged"); 
    } 
} 

Voila。完成。