iOS - 将手指拖过视图网格

时间:2011-11-17 21:53:18

标签: objective-c cocoa-touch uiview

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

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

– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:
– touchesCancelled:withEvent:

hitTest:withEvent:

1 个答案:

答案 0 :(得分:1)

我认为你可以使用:

– touchesBegan:withEvent:
– touchesMoved:withEvent:
– touchesEnded:withEvent:
– touchesCancelled:withEvent:

在您的超级视图上。

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

 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
 {
     UITouch *touch = [[event allTouches] anyObject];
     CGPoint location = [touch locationInView:touch.view];
     if(CGRectContainsPoint(subview1.frame, location)) {
         ...
     }
 }

touchesBegantouchesEnded不会在此方面发挥重要作用;它们只对开始和结束您在touchesMoved中执行的“跟踪”非常有用。

-(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>
}