如何在touchesMoved中找到最低的视图:?

时间:2013-01-09 03:44:41

标签: ios cocoa-touch uiview

我有3 UIViewsneedToDragViewsuperViewself.view。当我使用touchesMoved:仅拖动needToDragView时(3视图中最低)。但它可以拖动所有3View。如何检测最低的UIView?

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
        UIView *dragView = [[touches anyObject] view];
        CGPoint newCenter = [[touches anyObject] locationInView:dragView.superview];
        dragView.center = newCenter;
}

1 个答案:

答案 0 :(得分:3)

试试这个: 确保为needToDragView设置UserInteractionEnabled = YES;

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    {
         UITouch *touch = [touches anyObject];
         if ([touch view]== needToDragView) {

         // Drag Image Here

         }
         else {

         // NSLog("Wrong");

         } 
    }

<强>更新

除此之外,你还可以像这样使用UIPanGestureRecognizer

UIPanGestureRecognizer pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(function:)];
    [pan setMinimumNumberOfTouches:1];
    [pan setMaximumNumberOfTouches:2];
    [needsToDragView addGestureRecognizer:pan];
    [needsToDragView setUserInteractionEnabled:YES];

- (void)function:(UIPanGestureRecognizer *)recognizer
{
    NSLog(@"Moving View");

    mainView= recognizer.view;
    CGPoint translatedPoint = [recognizer translationInView:self.view];

    if([recognizer state] == UIGestureRecognizerStateBegan)
    {
        firstX = [mainView center].x;
        firstY = [mainView center].y;

    }

    translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);

    [mainView setCenter:translatedPoint];
}