2 つの観察:
私は通常、さまざまな方法ではなく、このようなものにジェスチャ認識機能を使用しtouchUp...
ます。
サブビューを高速に列挙したくないと言ったのは知っていますが、なぜですか? 一部のクラスが他のクラスと異なるという理由だけで、問題はありません (メソッドをテストするだけでよいためisKindOfClass
)。(余談ですが、なぜボタンになるものとそうでないものがあるのかはわかりません。) 並べていないからといって、実際には何の違いもありません。
とにかく、このようなものについては、ジェスチャ認識機能を共有の親ビュー (通常はビュー コントローラーのビューなど) に頻繁に配置し、サブビューを列挙して現在の CGPoint が含まれている場所を確認します。
CGPoint location = [sender locationInView:self.view];
for (UIView *subview in self.view.subviews)
{
if (CGRectContainsPoint(subview.frame, location))
{
// do your appropriate highlighting
if ([subview isKindOfClass:[UIButton class]])
{
// something for buttons
}
else if ([subview isKindOfClass:[UIImageView class]])
{
// something for imageviews
}
return;
}
}
これがあなたにとって意味があるかどうかはわかりませんが、私には最も簡単に思えます。意図を明確にしていただければ、回答をさらに絞り込むことができるかもしれません。
実用的な例として、クリックされた最初と最後のサブビューを追跡する継続的なジェスチャ認識エンジンを定義できます (サブビューでジェスチャを開始しない場合、ジェスチャは生成されません。サブビューでジェスチャを停止すると、すべてがキャンセルされます)、例:
@interface SubviewGestureRecognizer : UIGestureRecognizer
@property (nonatomic,strong) UIView *firstSubview;
@property (nonatomic,strong) UIView *currentSubview;
@end
@implementation SubviewGestureRecognizer
- (id) initWithTarget:(id)target action:(SEL)action
{
self = [super initWithTarget:target action:action];
if (self)
{
self.firstSubview = nil;
self.currentSubview = nil;
}
return self;
}
// you might want to tweak this `identifySubview` to only look
// for buttons and imageviews, or items with nonzero tag properties,
// or recursively navigate if it encounters container UIViews
// or whatever suits your app
- (UIView *)identifySubview:(NSSet *)touches
{
CGPoint location = [[touches anyObject] locationInView:self.view];
for (UIView *subview in self.view.subviews)
{
if (CGRectContainsPoint(subview.frame, location))
{
return subview;
}
}
return nil;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
if ([touches count] != 1)
{
self.state = UIGestureRecognizerStateFailed;
return;
}
self.firstSubview = [self identifySubview:touches];
self.currentSubview = self.firstSubview;
if (self.firstSubview == nil)
self.state = UIGestureRecognizerStateFailed;
else
self.state = UIGestureRecognizerStateBegan;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
if (self.state == UIGestureRecognizerStateFailed) return;
self.currentSubview = [self identifySubview:touches];
self.state = UIGestureRecognizerStateChanged;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesEnded:touches withEvent:event];
self.currentSubview = [self identifySubview:touches];
if (self.currentSubview != nil)
self.state = UIGestureRecognizerStateEnded;
else
self.state = UIGestureRecognizerStateFailed;
}
- (void)reset
{
[super reset];
self.firstSubview = nil;
self.currentSubview = nil;
}
次に、これを次のように設定できますviewDidLoad
。
SubviewGestureRecognizer *recognizer = [[SubviewGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouches:)];
[self.view addGestureRecognizer:recognizer];
次に、ハンドラーをそのように定義します (これはボタンと画像ビューで機能し、両方が をサポートしているという事実を利用しますsetHighlighted
):
- (void)handleTouches:(SubviewGestureRecognizer *)sender
{
static UIControl *previousControl = nil;
if (sender.state == UIGestureRecognizerStateBegan || sender.state == UIGestureRecognizerStateChanged)
{
if (sender.state == UIGestureRecognizerStateBegan)
previousControl = nil;
UIView *subview = sender.currentSubview;
if (previousControl != subview)
{
// reset the old one (if any)
[previousControl setHighlighted:NO];
// highlight the new one
previousControl = (UIControl *)subview;
[previousControl setHighlighted:YES];
}
}
else if (sender.state == UIGestureRecognizerStateEnded)
{
if (previousControl)
{
[previousControl setHighlighted:NO];
NSLog(@"successfully touchdown on %@ and touchup on %@", sender.firstSubview, sender.currentSubview);
}
}
else if (sender.state == UIGestureRecognizerStateCancelled || sender.state == UIGestureRecognizerStateFailed)
{
[previousControl setHighlighted:NO];
NSLog(@"cancelled/failed gesture");
}
}