触摸方法崩溃EXC_BAD_ACCESS

时间:2017-03-15 11:56:15

标签: ios objective-c xcode

我目前正在开发一款游戏,在访问涉及触摸某个项目的某段代码时似乎会崩溃。

以下是代码崩溃的部分;

 drawOn = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, currentDrawingImage.size.width/2, currentDrawingImage.size.height/2)];

完整的方法就在这里;

 - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
    {
        BOOL foundExtra = NO;
        UITouch *touch = [[event allTouches] anyObject];
        for(int i = 0; i < [extrasArray count]; i++)
        {
            if([[[extrasArray objectAtIndex:i] objectForKey:@"tag"] intValue] == [touch view].tag)
            {
                touchedExtra = YES;
                lastTouchedExtra = (int)[touch view].tag;
                foundExtra = YES;
            }
        }

        if(foundExtra == NO)
        {
            if(movingStopped == NO)
            {
                drawOn = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, currentDrawingImage.size.width/2, currentDrawingImage.size.height/2)];
                drawOn.image = currentDrawingImage;
                drawOn.tag = lastTag + 1;
                lastTag++;

                CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
                drawOn.center = CGPointMake(touchPoint.x, touchPoint.y);
                [bigView addSubview:drawOn];
                 lastTouchedPoint = touchPoint;
            }
        }

}

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

        int indexIngr = 0;
        for(int i = 0; i < [extrasArray count]; i++)
        {
            if([[[extrasArray objectAtIndex:i] objectForKey:@"tag"] intValue] == [touch view].tag)
            {
                indexIngr = i;
                [touch view].center = touchPoint;
            }
        }

        NSMutableDictionary *md = [[NSMutableDictionary alloc] init];
        [md setObject:[[extrasArray objectAtIndex:indexIngr] objectForKey:@"pic"] forKey:@"pic"];
        [md setObject:NSStringFromCGRect([touch view].frame) forKey:@"frame"];
        [md setObject:[NSString stringWithFormat:@"%d", (int)[touch view].tag] forKey:@"tag"];

        [extrasArray replaceObjectAtIndex:indexIngr withObject:md];


        //        if([touch view].tag > 0)
        //        {
        //            [touch view].center = touchPoint;
        //
        //            int tagLabel = [touch view].tag;
        //            NSMutableDictionary *md = [[NSMutableDictionary alloc] init];
        //            [md setObject:[[ingredients objectAtIndex:tagLabel - 1] objectForKey:@"pic"] forKey:@"pic"];
        //            [md setObject:NSStringFromCGRect([touch view].frame) forKey:@"frame"];
        //
        //            [ingredients replaceObjectAtIndex:tagLabel - 1 withObject:md];
        //        }
    }
    else if(lastDecoration == 1)//draw on apple
    {
        if(movingStopped == NO)
        {
            CGPoint touchPoint = [[touches anyObject] locationInView:self.view];
            if([self isFarEnoughFrom:touchPoint])
            {
                drawOn = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, currentDrawingImage.size.width/2, currentDrawingImage.size.height/2)];
                drawOn.image = currentDrawingImage;
                drawOn.tag = lastTag;


                drawOn.center = CGPointMake(touchPoint.x, touchPoint.y);
                [bigView addSubview:drawOn];
                //[self.view insertSubview:drawOn belowSubview:backButton];

                lastTouchedPoint = touchPoint;
            }
        }
    }
}

- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
{
    touchedExtra = NO;
    //movingStopped = YES;
    lastTouchedExtra = -1;
}

也使用drawOnChosen方法;

- (void)drawOnChosen:(UIButton*)sender
{
    [(AppDelegate*)[[UIApplication sharedApplication] delegate] playSoundEffect:1];

    currentDrawingImage = [UIImage imageNamed:[NSString stringWithFormat:@"drawon%d.png", (int)sender.tag]];
    movingStopped = NO;
    lastDecoration = 1;

    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^
     {
         decorationsView.frame = CGRectMake(0, -[[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
     } completion:^(BOOL finished)
     {
         [decorationsView removeFromSuperview];
     }];
}

0 个答案:

没有答案