簡體   English   中英

iPhone iOS UITapGestureRecognizer是否可以用某個接觸點初始化識別器?

[英]iPhone iOS UITapGestureRecognizer is it possible to initialize a recognizer with a certain touch point?

我使用點擊手勢識別器在另一個UIView中放置帶有標簽的子視圖。 當用戶點擊視圖時,標簽將填充有用戶點擊的組件的標題,子視圖位於點擊位置的中心。

我需要確保我的手勢識別器的初始位置與情節提要中定義的子視圖的中心匹配(在用戶點擊視圖之前),但是似乎無法找到一種方法來傳遞此點轉到手勢識別器。 有沒有一種方法可以用視圖中的特定點初始化我的點擊手勢識別器?

我不太確定你在問什么。 手勢識別沒有“起點”。 它們為給定視圖內的觸摸接收各種觸摸類型,並允許您唯一地處理每個視圖。

如果要模擬加載時的感覺(可能是您想要的聲音),請將代碼重新組織為以下形式:

- (void)viewDidLoad
{
     //simulate touch here
     [self touchedAtLocation:CGPointMake(100, 100)];
}

//Your delegate method
- (void)handleTap:(UITapGestureRecogizer *)recognizer
{
    [self touchedAtLocation:[recognizer locationInView:self.view]];
}

- (void)touchedAtLocation:(CGPoint)location
{
    //perform action based on location of touch
}

在此示例中,您可以基於在100,100觸摸時的狀態來開始子視圖的位置/數據。

注意:我遺漏了配置手勢識別器的代碼,因為聽起來您已經控制了該部分。 如果沒有,我可以發布更多代碼。

有幾件事情可能會有所幫助:

手勢識別器可以附加到視圖層次結構中的任何視圖。 因此,如果您希望一些小的子視圖識別出該拍子,則可以將GestureRecognizer添加到該視圖。

識別手勢后,您可以在決定對其進行任何操作之前測試手勢的位置(以及其狀態的其他方面)。 例如,假設您只希望手勢在用戶點擊視圖內很小的空間時起作用。

- (void)handleTap:(UITapGestureRecogizer *)recognizer {

    // get the location relative to the subview to which this recognizer is attached
    CGPoint location = [recognizer locationInView:recognizer.view];

    // tiny rect to test, also in the recognizer's view's coordinates
    CGRect someSmallerRect = CGRectInset(recognizer.view.bounds, 10, 10);

    if (CGRectContainsPoint(someSmallerRect, location)) {
        // do whatever the touch should do
    }
    // otherwise, it's like it never happened
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM