tôi nhận được từ Two action methods for an UIButton; next track and seek forward:
Thay đổi nó Theo yêu cầu của bạn.
Cá nhân tôi chỉ cần theo dõi trạng thái của nút bằng số nguyên trên bộ điều khiển chế độ xem của bạn hoặc trong một lớp con nút. Nếu bạn theo dõi những gì nút đang làm bạn có thể kiểm soát những gì mỗi hành động làm. Trong file .h của bạn đưa vào một số nội dung như thế này:
enum {
MyButtonScanning,
MyButtonStalling,
MyButtonIdle
};
@interface YourClass : UIViewController {
NSInteger buttonModeAt;
}
@property (nonatomic) NSInteger buttonModeAt;
-(IBAction)buttonPushedDown:(id)sender;
-(void)tryScanForward:(id)sender;
-(IBAction)buttonReleasedOutside:(id)sender;
-(IBAction)buttonReleasedInside:(id)sender;
@end
Và sau đó trong tập tin ném .m bạn trong một số các công cụ này:
@implementation YourClass
///in your .m file
@synthesize buttonModeAt;
///link this to your button's touch down
-(IBAction)buttonPushedDown:(id)sender {
buttonModeAt = MyButtonStalling;
[self performSelector:@selector(tryScanForward:) withObject:nil afterDelay:1.0];
}
-(void)tryScanForward:(id)sender {
if (buttonModeAt == MyButtonStalling) {
///the button was not released so let's start scanning
buttonModeAt = MyButtonScanning;
////your actual scanning code or a call to it can go here
[self startScanForward];
}
}
////you will link this to the button's touch up outside
-(IBAction)buttonReleasedOutside:(id)sender {
if (buttonModeAt == MyButtonScanning) {
///they released the button and stopped scanning forward
[self stopScanForward];
} else if (buttonModeAt == MyButtonStalling) {
///they released the button before the delay period finished
///but it was outside, so we do nothing
}
self.buttonModeAt = MyButtonIdle;
}
////you will link this to the button's touch up inside
-(IBAction)buttonReleasedInside:(id)sender {
if (buttonModeAt == MyButtonScanning) {
///they released the button and stopped scanning forward
[self stopScanForward];
} else if (buttonModeAt == MyButtonStalling) {
///they released the button before the delay period finished so we skip forward
[self skipForward];
}
self.buttonModeAt = MyButtonIdle;
}
Sau đó chỉ liên kết hành động của nút với những gì tôi đã ghi nhận trong các ý kiến trước khi các IBactions. Tôi đã không thử nghiệm này nhưng nó sẽ làm việc.
Chỉ cần một đoán, làm cho một BOOL và kiểm tra trong cả hai phương pháp này và lật hành động bằng cách: '- (void) removeTarget: action: ' –
Bạn cũng muốn điều ngược lại xảy ra - có nghĩa là, nếu touchDown được kích hoạt, bạn có cần dragInside không xảy ra nếu người dùng bắt đầu kéo không? Cách duy nhất để làm cho công việc này là có được với một sự chậm trễ nhỏ ở đầu để chờ đợi và xem một kéo bắt đầu. Nếu điều đó không được chấp nhận, thì bạn sẽ phải đi với một thiết kế khác. – rdelmar