2010-12-22 53 views

回答

50

事件處理程序添加到按鈕,類似於以下,

[myButton addTarget:self action:@selector(dragBegan:withEvent:) forControlEvents: UIControlEventTouchDown]; 
[myButton addTarget:self action:@selector(dragMoving:withEvent:) forControlEvents: UIControlEventTouchDragInside]; 
[myButton addTarget:self action:@selector(dragEnded:withEvent:) forControlEvents: UIControlEventTouchUpInside | UIControlEventTouchUpOutside]; 

處理事件,如下,您可以從事件EV如下,獲得觸摸點

- (void)dragBegan:(UIControl *)c withEvent:ev { 
    NSLog(@"dragBegan......"); 
    UITouch *touch = [[ev allTouches] anyObject]; 
    CGPoint touchPoint = [touch locationInView:self.view]; 
    NSLog(@"Touch x : %f y : %f", touchPoint.x, touchPoint.y); 
} 

- (void)dragMoving:(UIControl *)c withEvent:ev { 
    NSLog(@"dragMoving......"); 
} 

- (void)dragEnded:(UIControl *)c withEvent:ev { 
    NSLog(@"dragEnded......"); 
} 

這不是我自己的答案。我從http://sree.cc/iphone/handling-touche-events-for-uibuttons-in-iphone得到了這段代碼,並做了一些小改動。

+0

這工作得很好!非常感謝!! – 2010-12-22 06:38:15

0

嘗試覆蓋UIButton類和touchesBegan方法調用[super touchesBegan ..]。

相關問題