簡體   English   中英

iPhone開發:同時使用內部觸摸和向上觸摸

[英]iphone development: using touch down and touch up inside simultaneosly

在我的應用程序中,我使用一個按鈕,並為它們分配了兩種方法,一種方法是當您按下(按鈕圖像已更改)時起作用,另一種方法是當您在內部觸摸時(另一視圖打開)。 簡而言之,如果您想打開一個視圖,請按按鈕,但是當您觸摸該按鈕時,圖像會更改,並且在您舉起手指后,會打開另一個視圖。 我的問題是,如果您按下按鈕,則圖像會發生變化,但是如果您將手指移到遠離按鈕的某個位置,則內部的觸摸無法正常工作。 但是問題在於圖像會保留其過高的版本,因為觸地觸發一次。 我該怎么辦? 謝謝

您可以在控件狀態下touchDragOutsidetouchDragExit具體取決於您要執行的操作。 使用touchDragOutside您可以檢測到用戶何時在按鈕內按下並拖動手指而不會離開按鈕的可觸摸范圍, touchDragExit可以檢測到用戶何時拖動到按鈕的可觸摸范圍之外。

[button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchDragExit];
[button addTarget:self action:@selector(someMethod:) forControlEvents:UIControlEventTouchDragOutside];

我確實建議您使用UIButton對象的此方法來更改圖像。

- (void)setImage:(UIImage *)image forState:(UIControlState)state

您可以在此處http://developer.apple.com/library/ios/#documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html中查看該狀態的所有選項

我將狀態UIControlStateNormal和UIControlStateHighlighted用於目標。

我自己已經遇到了這個問題,大部分情況下我們使用以下事件:

//此事件正常運行並觸發

[按鈕addTarget:self動作:@selector(holdDown)forControlEvents:UIControlEventTouchDown];

//這根本不會觸發

[按鈕addTarget:self動作:@selector(holdRelease)forControlEvents:UIControlEventTouchUpInside];

解:-

使用長按手勢識別器:-

 UILongPressGestureRecognizer *btn_LongPress_gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleBtnLongPressgesture:)];
[button addGestureRecognizer:btn_LongPress_gesture];

手勢的實現:-

- (void)handleBtnLongPressgesture:(UILongPressGestureRecognizer *)recognizer{


//as you hold the button this would fire

if (recognizer.state == UIGestureRecognizerStateBegan) {

    [self someMethod];
}

//as you release the button this would fire

if (recognizer.state == UIGestureRecognizerStateEnded) {

    [self someMethod];
}
}

暫無
暫無

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

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