Objective c iOS-将手指拖动到视图网格上

Objective c iOS-将手指拖动到视图网格上,objective-c,cocoa-touch,uiview,Objective C,Cocoa Touch,Uiview,我有一个带有子视图网格的超级视图。当我从子视图中拖出时,我想改变它的属性(类似于iPhone上的UIKeyboard)。子视图是UIButton子类 我想我需要从superview做一些触摸转发,但我不清楚它是如何工作的。这些方法的正确组合是什么 我认为您可以使用: – touchesBegan:withEvent: – touchesMoved:withEvent: – touchesEnded:withEvent: – touchesCancelled:withEvent: 在你的sup

我有一个带有子视图网格的超级视图。当我从子视图中拖出时,我想改变它的属性(类似于iPhone上的
UIKeyboard
)。子视图是
UIButton
子类

我想我需要从superview做一些触摸转发,但我不清楚它是如何工作的。这些方法的正确组合是什么

我认为您可以使用:

– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:
– touchesCancelled:withEvent:
在你的superview上

一种可能的方法是在
touchsmoved
中识别当前“在触摸下”的子视图(即触摸位置所在的子视图),并相应地更改其状态

 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
 {
     UITouch *touch = [[event allTouches] anyObject];
     CGPoint location = [touch locationInView:touch.view];
     if(CGRectContainsPoint(subview1.frame, location)) {
         ...
     }
 }
触摸开始
触摸结束
在这方面不会起主要作用;它们只在开始和结束您在
touchsmoved
中进行的“跟踪”时有用

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSSet *allTouches = [event allTouches]; 
    <save initial touch if you need it>
}


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    <do whatever>
}
-(void)touchesbeated:(NSSet*)toucheevent:(UIEvent*)event
{
NSSet*AllTouchs=[event AllTouchs];
}
-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event
{
}

感谢您的回复。hitTest是否适合从TouchsMoved获取子视图?当然可以;但是,hitTest横贯所有子视图层次结构。如果你有任何一种规则的子视图布局,你最好用某种数学方法来给出受影响的子视图。。。