[英]Using UILongPressGestureRecognizer For Subviews of UIScrollview
在過去的四個小時里,我嘗試了很多Stack Overlow解決方案,但沒有一個能幫我解決問題。
這里是,
像這樣的東西有效
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressDidFire:)];
longPress.minimumPressDuration = 0.5; [scroll addGestureRecognizer:longPress]; //scroll defined elsewhere
但是,如果我將滾動替換為滾動的任何子視圖,則長按事件永遠不會觸發。
- 如何檢測長按滾動視圖的子視圖?
- 然而,這是一個非常混亂的黑客,因為我可以檢測到滾動視圖的長按,有什么方法可以檢測印刷機的位置,以便我可以看到正在按下哪個特定的子視圖。
另外, (insert subview here).userInteractionEnabled = YES
,我為滾動視圖的所有子視圖設置了此屬性,因此這應該不是問題。
我也嘗試過使用touchesBegan和touchesEnded方法,如Stack Overflow中其他地方所建議的那樣。
此外,對於圖像視圖,我使用for循環為每個自定義圖像視圖設置新的UILongPressGestureRecognizer,因為我知道每個手勢識別器規則的1個視圖 。
來自第一次iOS開發者,
格雷厄姆
PS我真的更喜歡我能找到1的解決方案,而不是凌亂的2。
更多代碼要求:
在Init視圖控制器中
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressDidFire:)];
longPress.minimumPressDuration = 0.5;
[self.titleLabel addGestureRecognizer:longPress]; //titleLabel property initialized elsewhere
[mainView addSubview:self.titleLabel];
在“加載圖像”方法中
for (NSData * dataImg in results) {
//Does some work turning data into an image, into an image view called img
img.delegate = self;
img.userInteractionEnabled = YES;
UILongPressGestureRecognizer *aLongPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressDidFire:)];
aLongPress.minimumPressDuration = 0.5;
[img addGestureRecognizer:aLongPress];
[imgContainer addSubview:img];
}
更多代碼+筆記
self.view(UIView)
- >滾動(UIScrollView)
- > - > mainView(UIView)
- > - > - > titleLabel(UILabel)
- > - > - > imgContainer(UIView)
- > - > - > - > images(UIImageViews)
[self.view addSubview:scroll];
[scroll addSubview:mainView];
[mainView addSubview:self.titleLabel];
[mainView addSubview:imgContainer];
[imgContainer addSubview:img]; //done 8x via for loop
感謝@ RegularExpression的回答,我現在知道mainView正在被按下,但不是它的子視圖,所以我需要找到一種方法來顯示它上面的mainView的子視圖。 :)
另一個更新,titleLabel工作。 ImageViews仍然無法正常工作。 :(
我知道這有點晚了,已經選擇了一個答案,但是如果你有iOS7,別人想要一個很好的簡單解決方案。
在你的UILongPressGestureRecognizer委托中,實現gestureRecognizer:shouldRequireFailureOfGestureRecognizer:otherGestureRecognizer選擇器
檢查otherGestureRecognizer是否為UIPanGestureRecognizer並返回YES,否則返回NO
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
if ([otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]]) {
return YES;
}
return NO;
}
滾動視圖實際上會產生一個UIScrollViewPanGestureRecognizer,它是私有API的一部分,但它是UIPanGestureRecognizer的子類,所以上面的工作正常。
為了支持iOS6的或以下,那么你會通過的UIScrollView的gestureRecognizers需要循環,檢測其中一個是UIPanGestureRecognizer並在您UILongPressGestureRecogizer與執行requireGestureRecognizerToFail選擇。
你的代碼似乎很好,它應該工作我認為。我使用下面的代碼,它的工作正常。
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
longPress.delegate = (id)self;
longPress.minimumPressDuration=0.05;
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:longPress];
和它的方法,
- (IBAction)handleLongPress:(UILongPressGestureRecognizer *)sender {
NSLog(@"detected");
if (sender.state == UIGestureRecognizerStateEnded){
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Alert" message:@"YES" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
}
正如你所說,我把imageView作為scrollview的子視圖
由於您的UIScrollView是常見的父級,這可能是您的手勢識別器需要的位置。 您可以通過查看操作中提供的點的位置來確定正在按下哪個子視圖。 因此,各個子視圖不需要手勢識別器。
所以,你會做這樣的事情:
- (void)longPressDidFire:(UILongPressGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded)
CGPoint point = [sender locationInView:scroll];
UIView *tappedView = [scroll hitTest:point withEvent:nil];
那么,你就有了長期以來的觀點。
其他可能導致操作無法觸發的事情將是委托問題,或者如果滾動包含在另一個攔截觸摸的視圖中。
HTH
代替
[scroll addGestureRecognizer:longPress];
在您聲明它們之后以及將它們添加到scrollview之前,在子視圖上添加手勢
[subview addGestureRecognizer:longPress];
哇哇它有效!
問題是:
imgContainer是一個帶有空框架的UIView,其中有幾個UIImageViews作為子視圖
我的印象是,當我向imgContainer添加子視圖時,imgContainer會擴展 。
事實並非如此 。
我必須將imgContainer的框架設置為與滾動視圖相同的內容框架 ,然后一切都變得正常。
我希望這個答案可以幫助像我這樣的未來iOS第一次定時器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.