所以我有一个UIView的子类,假设检测触摸。仅当触摸在当前视图内开始时,视图检测才会触摸。当触摸从视图外部开始并且它们在我的自定义视图中移动时,touchesMoved不会被调用。有哪些解决方案可以检测当前视图中尚未启动的移动触摸?
@implementation MycustomView
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// This only gets called if touches have started in the current View
}
@end
答案 0 :(得分:20)
以下解决方案有效。我有MyCustomView的多个实例;当触摸移动时,我想要检测正在触摸的视图
我最终将MyCustomView的触摸检测移动到其superView,因此以下代码不再出现在MyCustomView类中:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.contentView];
for (UIView *view in self.contentView.subviews)
{
if ([view isKindOfClass:[MyCustomView class]] &&
CGRectContainsPoint(view.frame, touchLocation))
{
}
}
}
答案 1 :(得分:1)
这应该解决它:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
for (UIView* subView in self.subviews)
{
if([subView pointInside:[self convertPoint:touch toView:subView] withEvent:event])
{
//do your code here
}
}
}
答案 2 :(得分:0)
执行此操作的一种方法(尽管可能有其他方法)是禁用子视图的用户交互并使其父视图跟踪移动(使用hitTest
方法确定触摸当前所在的视图)。
答案 3 :(得分:0)
试试这个......
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
for(UITouch *touch in touches)
{
CGPoint touchPointFirstBtn = [touch locationInView:self.ChordView];
if(CGRectContainsPoint(_btnC.frame, touchPointFirstBtn))
{
if (!_btnC.isHighlighted)
{
if(!Boolean)
{
title = @"C";
[_tlbView reloadData];
NSLog(@"%@",@"touches C");
}
[_btnC setHighlighted:YES];
Boolean = YES;
}
}
else
{
[_btnC setHighlighted:NO];
Boolean = NO;
}
}