我了解如何制作可拖动的图像,但是我无法使类文件中的图像可以从我的主 ViewController.m 文件中拖动。我的班级被称为“炸弹”。每两秒钟,就会创建一个新的炸弹,并带有一个 bombImage(炸弹对象)。炸弹被添加到一个 NSMutableArray (bombArray)。
- (void) newBomb
{
bomb *bomb1 = [[bomb alloc] init];
[bombArray addObject: bomb1];
[bomb1 displayBombOnView:self.view]; //displayBombOnView just makes a new bomb in a random location
}
我正在努力做到这一点,以便用户可以拖动每个“炸弹”。
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event : (bomb *) bomb
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
bomb->bombImage.center = location;
}
-(void) touchesMoved:(NSSet*)touches withEvent:(UIEvent *)event
{
bomb *tempBomb = [[bomb alloc] init];
arrayCount = [bombArray count];
for (int k = 0; k<arrayCount; k++)
{
tempBomb = [bombArray objectAtIndex:k];
CGPoint tappedPt = [[touches anyObject] locationInView: self];
int xPos = tappedPt.x;
int yPos = tappedPt.y;
if ((xPos >= tempBomb->bombImage.center.x - 25 && xPos <= tempBomb->bombImage.center.x + 25) && (yPos >= tempBomb->bombImage.center.y - 25 && xPos <= tempBomb->bombImage.center.y + 25))
{
[self touchesBegan:touches withEvent:event : [bombArray objectAtIndex:k]];
break;
}
}
}
它构建了,但是当我尝试拖动图像时,它崩溃了,说Thread 1: signal SIGABRT
. 任何帮助将不胜感激。