兩個意見:
我通常使用手勢識別爲這樣的東西,而不是各種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");
}
}
來源
2012-08-12 03:25:27
Rob
您描述了一些低級別的行爲(如果按下並將您的手指拖動到另一個按鈕),但是您沒有描述您要解決的更廣泛的問題,所以它會使您的問題有點回答強硬。你想做什麼?只需檢測一個按鈕的觸摸並觸摸另一個按鈕?或者像Swype一樣的打字方案中的整個系列?建議的答案可能取決於你想要做的事情。 – Rob 2012-08-12 03:39:18
我也不明白你的最後一點,關於不快速枚舉框架不工作(因爲如果你擔心不同的子視圖類型,你只需檢查'isKindOfClass'知道什麼是正確的行爲)。看到我的答案。 – Rob 2012-08-12 03:39:55
我認爲你的意思是UIImageView而不是UIImage – WolfLink 2012-08-12 06:24:36