過去 4 時間、多くの Stack Overlow ソリューションを試しましたが、問題の解決に役立つものはありませんでした。
ここにあります、
- UIScrollViewがあります
- その ScrollView 内には、1 つのカスタム UILabel と 8 つのカスタム UIImageViewsがあります。
- 長押しを検知したい
このようなものが機能します
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 メソッドを使用してみました。
また、イメージ ビューについては、for ループを使用して、カスタム イメージ ビューごとに新しい UILongPressGestureRecognizer を設定します。
初めての iOS 開発者から、
グラハム
PS 面倒な 2. よりも 1. の解決策を見つけることができれば、本当に好きです
要求に応じたその他のコード:
ビューコントローラーの初期化で
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)
->->->->画像 (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 はまだ機能しません。:(