2013-03-27 25 views
5

Tôi muốn xử lý sự kiện xảy ra khi Kết nối UIButton kết thúc. Tôi biết rằng UIControl có một số sự kiện thực hiện các liên lạc (UIControlEventTouchDown, UIControlEventTouchCancel, vv). Nhưng tôi không thể xem bất kỳ quảng cáo nào ngoại trừ UIControlEventTouchDownUIControlEventTouchUpInside.Cách phát hiện sự kiện kết thúc cảm ứng cho UIButton?

Nút của tôi là chế độ xem phụ của một số UIView. Điều đó UIView có userInteractionEnabled thuộc tính được đặt thành YES.

Có vấn đề gì?

Trả lời

21

Bạn có thể đặt 'mục tiêu hành động' cho nút của bạn theo ControlEvents

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents; 

Ví dụ:

[yourButton addTarget:self 
      action:@selector(methodTouchDown:) 
forControlEvents:UIControlEventTouchDown]; 

[yourButton addTarget:self 
      action:@selector(methodTouchUpInside:) 
forControlEvents: UIControlEventTouchUpInside]; 

-(void)methodTouchDown:(id)sender{ 

    NSLog(@"TouchDown"); 
} 
-(void)methodTouchUpInside:(id)sender{ 

    NSLog(@"TouchUpInside"); 
} 
+0

Oh, nó hoạt động tuyệt vời. Sự không chú ý của tôi đã chơi một trò đùa tàn nhẫn với tôi. Cảm ơn – Brain89

3

Bạn cần phải tạo lớp tùy chỉnh của riêng bạn mà kéo dài UIButton. tệp tiêu đề của bạn sẽ trông giống như thế này.

@interface customButton : UIButton 
{ 
} 

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; 

sau đó làm cho tập tin thực thi của bạn

+0

Thật không may, lập trình viên cao cấp của tôi đã cấm tạo một lớp học mới. Nhưng giải pháp này cũng hoạt động đúng cách – Brain89

0

tôi nghĩ rằng đó là dễ dàng hơn

UILongPressGestureRecognizer *longPressOnButton = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressOnButton:)]; 
longPressOnButton.delegate = self; 
btn.userInteractionEnabled = YES; 
[btn addGestureRecognizer:longPressOnButton]; 



- (void)longPressOnButton:(UILongPressGestureRecognizer*)gesture 
{ 
    // When you start touch the button 
    if (gesture.state == UIGestureRecognizerStateBegan) 
    { 
     //start recording 
    } 
    // When you stop touch the button 
    if (gesture.state == UIGestureRecognizerStateEnded) 
    { 
     //end recording 
    } 
} 
0

Chỉ cần thêm IBOutlets cho UIButton với sự kiện TouchDown: và hành động Chính Triggered:

- (IBAction)touchDown:(id)sender { 
    NSLog(@"This will trigger when button is Touched"); 
} 

- (IBAction)primaryActionTriggered:(id)sender { 
    NSLog(@"This will trigger Only when touch end within Button Boundary (not Frame)"); 
} 
0

@ Ramshad của được chấp nhận answer trong Swift 3.0 cú pháp sử dụng phương pháp UIControl lớp

open func addTarget(_ target: Any?, action: Selector, for controlEvents: UIControlEvents) 

Ví dụ sau đây:

myButton.addTarget(self, action: #selector(MyViewController.touchDownEvent), for: .touchDown) 
myButton.addTarget(self, action: #selector(MyViewController.touchUpEvent), for: [.touchUpInside, .touchUpOutside]) 

func touchDownEvent(_ sender: AnyObject) { 
    print("TouchDown") 
} 

func touchUpEvent(_ sender: AnyObject) { 
    print("TouchUp") 
} 
0

Swift 3.0 phiên bản:

let btn = UIButton(...) 

btn.addTarget(self, action: #selector(MyView.onTap(_:)), for: .touchUpInside) 

func onTap(_ sender: AnyObject) -> Void { 

} 
Các vấn đề liên quan