2

我有 3 UIViews: needToDragView, 它的superView, 和self.view. 当我使用touchesMoved:只拖动我的needToDragView(3Views 中最低的)时。但它可以拖动所有3View。如何检测最低的UIView?

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

1 回答 1

3

试试这个:确保你设置UserInteractionEnabled = YES;了needToDragView。

-(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];
}
于 2013-01-09T04:42:28.597 回答