ios - 将 GestureRecogniser 附加到多个 ImageView
<p><p>我今天在将同一个手势识别器附加到多个 ImageView 时遇到了一些奇怪的事情。它只附加到最后一个,换句话说,它只能附加到一个 View ! </p>
<p>我必须创建多个手势识别器来满足我的要求。 </p>
<p>以下是我所做的。我做得对吗?这是将识别器附加到多个 ImageView 的唯一方法吗? </p>
<p>请注意,我不想使用 UITableView 或 UIVIew 并将所有 ImageView 放入其中并将手势识别器仅附加到 UITableView 或 UIVIew。我把所有图像都分散了,我必须检测哪个图像被拖动。谢谢。</p>
<pre><code>;
;
;
;
;
;
;
;
;
;
;
;
//Attach gesture recognizer to each imagviews
gestureRecognizer1 = [ initWithTarget:self action:@selector(gestureHandler:)];
gestureRecognizer1.delegate = self;
gestureRecognizer2 = [ initWithTarget:self action:@selector(gestureHandler:)];
gestureRecognizer2.delegate = self;
gestureRecognizer3 = [ initWithTarget:self action:@selector(gestureHandler:)];
gestureRecognizer3.delegate = self;
gestureRecognizer4 = [ initWithTarget:self action:@selector(gestureHandler:)];
gestureRecognizer4.delegate = self;
gestureRecognizer5 = [ initWithTarget:self action:@selector(gestureHandler:)];
gestureRecognizer5.delegate = self;
gestureRecognizer6 = [ initWithTarget:self action:@selector(gestureHandler:)];
gestureRecognizer6.delegate = self;
;
;
;
;
;
;
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>是的,每个手势识别器一个 View 。因此,如果您只想要一个识别器,请将其放在 superview 上,例如:</p>
<pre><code>UILongPressGestureRecognizer *gestureRecognizer = [ initWithTarget:self action:@selector(gestureHandler:)];
;
</code></pre>
<p>然后,在您的处理程序中,您可以:</p>
<pre><code>- (void)handleLongPress:(UILongPressGestureRecognizer *)sender
{
CGPoint location = ;
if (sender.state == UIGestureRecognizerStateBegan)
{
for (UIView *view in self.view.subviews)
{
if (] && CGRectContainsPoint(view.frame, location))
{
UIImageView *image = (UIImageView *) view;
// ok, now you know which image you received your long press for
// do whatever you wanted on it at this point
return;
}
}
}
}
</code></pre>
<p>顺便说一句,如果你这样做了,你也不必担心在图像上启用用户交互。</p>
<p>最后,您无需担心指定手势识别器的委托(delegate),除非您要遵守 <code>UIGestureRecognizerDelegate</code>,但事实并非如此。另请注意,我使用本地 var 作为识别器,因为没有理由坚持使用它。</p>
<p><strong>更新:</strong></p>
<p>虽然上面的代码可以正常工作,但如果长按没有发生在图像上,自定义长按手势识别器可能会更好(这样,如果您有其他在您的 View 中发生的手势识别器)。所以:</p>
<pre><code>#import <UIKit/UIGestureRecognizerSubclass.h>
@interface ImageLongPressGestureRecognizer : UILongPressGestureRecognizer
@property (nonatomic, weak) UIImageView *imageview;
@end
@implementation ImageLongPressGestureRecognizer
@synthesize imageview = _imageview;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
self.imageview = nil;
;
CGPoint location = ;
for (UIView *view in self.view.subviews)
{
if (] && CGRectContainsPoint(view.frame, location))
{
self.imageview = (UIImageView *)view;
return;
}
}
self.state = UIGestureRecognizerStateFailed;
}
@end
</code></pre>
<p>然后使用这个新的子类相应地创建您的手势识别器:</p>
<pre><code>ImageLongPressGestureRecognizer *gestureRecognizer = [ initWithTarget:self action:@selector(handleLongPress:)];
;
</code></pre>
<p>然后,作为这个子类的一个不错的小好处,您的主要手势识别器被简化了,即:</p>
<pre><code>- (void)handleLongPress:(ImageLongPressGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateBegan)
{
// you can now do whatever you want with sender.imageview, e.g. this makes it blink for you:
[UIView animateWithDuration:0.5
animations:^{
sender.imageview.alpha = 0.0;
} completion:^(BOOL finished){
[UIView animateWithDuration:0.5
animations:^{
sender.imageview.alpha = 1.0;
}
completion:nil];
}];
}
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 将 GestureRecogniser 附加到多个 ImageView ,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/11330738/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/11330738/
</a>
</p>
页:
[1]