iOS - 检测UIView中的触摸?
问题描述:
所以我有一个UIView的子类,假设检测触摸。仅当触摸在当前视图内开始时,视图检测才会触摸。当触摸开始于视图之外并且它们在我的自定义视图中移动时touchesMoved不会被调用。检测当前视图中尚未启动的移动触摸的任何解决方案?iOS - 检测UIView中的触摸?
@implementation MycustomView
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// This only gets called if touches have started in the current View
}
@end
答
以下解决方案工作。我有多个MyCustomView实例;作为触摸移动我想检测正在接触
我结束了从MyCustomView移动触摸检测到它的父的意见,所以下面的代码不再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))
{
}
}
}
答
这应该修复它:
- (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
}
}
}
答
的一种方式要做到这一点(尽管可能有其他方法)是禁用子视图的用户交互,并使其父视图跟踪移动(使用hitTest
方法来确定触摸当前处于哪个视图)。
答
试试这个....
-(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;
}
}
这是记录和预期的行为。也许如果你对一些人有所了解*你想完成什么,可以帮助你完成* how *。 – NJones 2012-04-01 23:14:50
我在屏幕上有多个自定义视图我想检测UIViews作为触摸移动它们 – aryaxt 2012-04-01 23:16:18