iphone – touchesbegan,touchesmoved,touchesended issue

由于各种原因,我已将这些方法从UIView子类移动到我的viewcontroller.除了一件事,我终于得到了它.我不仅可以拖动我以编程方式创建的UI
Imageviews,而且实际的视图控制器视图也是可拖动的.创造这种非常不受欢迎的效果.我猜这是事实,它触及任何对象,背景本身就是一个对象.我只是不确定如何排除背景.我认为它需要“启用UserInteraction”,但我猜不是吗?我只想让它使UIImageViews可拖动.请原谅我的noobness.我还在学习.

我想要一个名为“letterDictionary”的NSMutableDictionary中我想要“触摸”的所有图像视图.是否可以只触摸适用于字典中的内容?

http://imgur.com/W08dI

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    UITouch *touch = [touches anyObject];
    touchPoint = [touch locationInView:self.view];

    movingLetter = [touch view];

    CGPoint pointInside = [touch locationInView:[touch view]];
    if ([movingLetter pointInside:pointInside withEvent:event]) touchedInside = YES;

}


- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    if (touchedInside) {
        UITouch *touch = [touches anyObject];
        CGPoint newPoint = [touch locationInView:self.view];  // get the new touch location
        movingLetter.center = CGPointMake(movingLetter.center.x + newPoint.x - touchPoint.x, movingLetter.center.y + newPoint.y - touchPoint.y);         
        touchPoint = newPoint;      
    }
}


- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    if (touchedInside) {
        UITouch *touch = [touches anyObject];
        CGPoint newPoint = [touch locationInView:self.view];

        movingLetter.center = CGPointMake(movingLetter.center.x + newPoint.x - touchPoint.x, movingLetter.center.y + newPoint.y - touchPoint.y);

        if (CGRectIntersectsRect([movingLetter frame], [placeHolder frame]))
            {
                movingLetter.center = placeHolder.center;
            }

    }   
    touchedInside = NO;
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    touchedInside = NO;
}

最佳答案 你有被触动的观点,

    UITouch *touch = [touches anyObject];
    touchPoint = [touch locationInView:self.view];
    movingLetter = [touch view];

只是测试它是否是你正在寻找的类(例如UIImageView)然后返回

    UITouch *touch = [touches anyObject];
    if (![[touch view] isKindOfClass:[UIImageView class]])
    {
       return;
    }
点赞