2012-11-10 30 views
3

如何更改觸摸觸摸外部方法的距離? 當觸摸位置距離按鈕大約100個像素時,UIButton觸摸外部方法只會觸發,因爲當從大約100像素內部拖動到外部時,我可以看到按鈕的高光變化。 有沒有辦法縮短距離? 謝謝!觸摸範圍外

回答

1

能不能請你

- (IBAction)btnDragged:(id)button withEvent:(UIEvent*)event { 
    UITouch *t = [[event touchesForView:yourButtonView] anyObject]; 
    CGPoint touchLocation = [t locationInView:self.view]; 
    //NSLog(@"%@", NSStringFromCGPoint(touchLocation)); 

    if (your condition, using CGPoint to check for shorten distance, compare your button location and touchLocation) { 
     //fire some stuff 
    } 
} 

希望它能幫助,請給我一個反饋,所以我知道這是怎麼回事,然後我可以編輯我的代碼,以幫助你,祝你好運:)。

顯着:事件將包含座標

UPDATE://按照以下 您的評論,請試試,如果條件下,您需要檢查什麼樣的類的終點的

假設你想縮短到50個像素距離按鈕, 所以條件應該是這樣的。

if (fabsf(yourButton.frame.x - touchLocation.x) <= 50 && fabsf(yourButton.frame.y - touchLocation.y) <= 50) { 

    UIView *v = [self.view hitTest:touchLocation withEvent:nil]; 
    if ([v isKindOfClass:[UIButton class]]) //check that it is button B or not 
    { 
     //do your stuff 
    } 
} 

希望它能幫助:)

+0

感謝您的答覆。實際上,我想在按鈕操作中實現幻燈片;首先我觸摸一個按鈕A,然後按鈕B顯示一些位置,然後我保持觸摸,滑動到按鈕B,當我觸摸B內部時,我想觸發我的方法。問題是,B方法裏面的觸摸不能觸發,我解決問題的唯一方法是檢查按鈕A的觸摸外部方法。但看起來外面的範圍並不是我所期望的。 – kobunketsu

+0

請參閱我的更新 – piam