2013-05-17 6 views
9

Tôi muốn chạy một phương thức nhất định và lặp lại chính nó miễn là ngón tay của ai đó được nhấn xuống trên một nút. Tôi muốn phương pháp đó ngừng lặp lại khi ngón tay không còn trên nút nữaNhấn và giữ nút UIButton - lặp lại thao tác cho đến khi cho phép

Có cách nào để kiểm tra xem touchDown vẫn đang diễn ra trong khi triển khai phương pháp không? Cứu giúp!

+2

thể trùng lặp của [Way để thực hiện một UIButton liên tục bắn trong một tình huống bấm và giữ?] (http://stackoverflow.com/questions/9 03114/way-to-make-a-uibutton-liên tục-cháy-trong-một-bấm-và-giữ-tình hình) – rmaddy

+0

cách này làm việc! cảm ơn bạn. Tôi đã không hoàn toàn hiểu nó khi tôi đọc nó trước khi gửi câu hỏi này - lần thứ hai là một sự quyến rũ. – dokun1

Trả lời

12

Bạn có thể sử dụng sự kiện kiểm soát UIControlEventTouchDown để bắt đầu phương thức đang chạy và UIControlEventTouchUpInside hoặc tương tự để phát hiện khi nút không còn được "nhấn" nữa.

Thiết lập các hành động để nút, ví dụ:

[myButton addTarget:self action:@selector(startButtonTouch:) forControlEvents:UIControlEventTouchDown]; 
[myButton addTarget:self action:@selector(endButtonTouch:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside]; 

(. Lưu ý ở trên sẽ gây cảm ứng bên trong và bên ngoài vào nút để gọi phương thức endButtonTouch:)

Sau đó, thêm startButtonTouch:endButtonTouch phương pháp, ví dụ:

- (void)startButtonTouch:(id)sender { 
    // start the process running... 
} 

- (void)endButtonTouch:(id)sender { 
// stop the running process... 
} 
+0

Cách tiếp cận này sẽ không hoạt động nếu người dùng chạm vào nút và di chuyển ngón tay ra ngoài giới hạn của nút. – danypata

+1

Sử dụng cả hai 'UIControlEventTouchUpInside' và' UIControlEventTouchUpOutside'. Đã thêm vào câu trả lời. Nếu bạn muốn kết thúc nó khi người dùng kéo ra ngoài nút, hãy thêm 'UIControlEventTouchDragExit'. – bobnoble

0

Vẽ theo câu trả lời của bobnoble đây là chế độ xem trợ giúp

#import <UIKit/UIKit.h> 

@interface YOIDCAutorepeatingButton : UIButton 

// you COULD pinch pennies switching to nonatomic, but consider 
// how much time it would take to debug if some day some moron decides without checking this spec 
// to alter this prop off another thread 
@property (atomic) NSTimeInterval delayUntilAutorepeatBegins; 
@property NSTimeInterval delayBetweenPresses; 

@property (weak) id<NSObject> recipient; 
@property SEL touchActionOnRecipient; 

@end 

-------------> .m

#import "YOIDCAutorepeatingButton.h" 

@interface YOIDCAutorepeatingButton() 
{ 
NSTimeInterval _pressStartedAt; 
} 

@end 

@implementation YOIDCAutorepeatingButton 


- (id)initWithCoder:(NSCoder *)aDecoder 
{ 
self = [super initWithCoder:aDecoder]; 
if (self) { 
    [self addTarget:self action:@selector(startButtonTouch:) forControlEvents:UIControlEventTouchDown]; 
    [self addTarget:self action:@selector(endButtonTouch:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside]; 

    self.delayUntilAutorepeatBegins = .250; 
    self.delayBetweenPresses = .080; 
} 
return self; 
} 

-(void)killLastCharacter:(id)sender 
{ 
[self.recipient performSelector:self.touchActionOnRecipient withObject:sender]; 
} 

- (void)performAutorepeat:(id)sender 
{ 
if(!self.delayBetweenPresses) { 
    // bail 
    return; 
} 
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.delayBetweenPresses * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
    if(_pressStartedAt) { 
     [self killLastCharacter:sender]; 
     [self performAutorepeat:sender]; 
    } 
}); 
} 

- (void)startButtonTouch:(id)sender { 

[self killLastCharacter:sender]; 
NSTimeInterval now = [NSDate timeIntervalSinceReferenceDate]; 
_pressStartedAt = now; 
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.delayUntilAutorepeatBegins * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 
    if(_pressStartedAt) { 
     [self killLastCharacter:sender]; 
     [self performAutorepeat:sender]; 
    } 
}); 
} 

- (void)endButtonTouch:(id)sender { 
_pressStartedAt = 0; 
} 

@end 

sử dụng mẫu -------- -------

- (IBAction)killLastDigit:(id)sender { 

.....

- (void)viewDidLoad 
{ 
assert(self.backSpace); 
[YOIDCAutorepeatingButton class]; // if xib is in a bundle other than main gottal load the class 
// otherwise you'd get -[UIButton setRecipient:]: unrecognized selector sent to instance 
// on setRecipient: 
self.backSpace.recipient = self; 
self.backSpace.touchActionOnRecipient = @selector(killLastDigit:);