[英]UIButton crashes on uitouch event
我有一個UIButton,將draginside事件發送到:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
它通過viewDidLoad中的以下代碼完成此操作:
[colourButton1 addTarget:self action:@selector(touchesBegan:withEvent:) forControlEvents:UIControlEventTouchDown];
在它發送到的方法內,有以下行:
UITouch *myTouch = [touches anyObject];
並且由於某種原因,當在UIButton中拖動時,這會使應用程序崩潰。 有什么想法嗎?
編輯:解決方案。
-(IBAction)buttonDragged:(id)button withEvent:(UIEvent*)event {
NSSet *touches = [event touchesForView:colourButton1];
[self touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event];
}
當您添加目標以進行控制時,您可以通過3種選擇器進行操作-
- (void)action
- (void)action:(id)sender
- (void)action:(id)sender withEvent:(UIEvent*)event
名稱無關緊要,重要的是參數的數量。 如果控件向目標發送一條帶有2個參數的消息,則第一個參數將是控件本身(在您的情況下為UIButton
的實例),第二個參數為UIEvent
一個實例。 但是您希望將NSSet
實例作為第一個參數, anyObject
其發送一條消息, UIButton
無法理解。 這是導致崩潰的原因。
您為什么首先嘗試將事件從UI控件發送到觸摸處理方法touchesMoved:withEvent:
? 它可能會執行與您的意思不同的操作。
更新:
- (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent *)event {
UITouch *t = [touches anyObject];
CGPoint touchLocation = [t locationInView:self.view];
NSLog(@"%@", NSStringFromCGPoint(touchLocation));
}
- (IBAction)buttongDragged:(id)button withEvent:(UIEvent*)event {
NSSet *touches = [event touchesForView:button];
[self touchesMoved:touches withEvent:event];
}
請注意,由於touchesMoved:withEvent:
是UIResponder
的方法,而控制器的視圖是 UIResponder
因此也會在此視圖的觸摸事件上UIResponder
此方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.