触摸屏幕时,ios应用程序崩溃

时间:2014-03-13 21:59:35

标签: ios iphone objective-c

在我的应用程序中,通过NSTimer每2秒调用一个方法(addCoinTarget)。我想让应用程序在调用UIView(在本例中为Coin)时执行NSLog。但是,每当触摸屏幕时,即使它不在Coin对象上,应用程序也会因错误而崩溃:

  

错误:0x42200000的内存读取失败

这是创建Coin对象的方法:

-(void)addCoinTarget {

    Coin *coinTarget = [[Coin alloc]initWithFrame:CGRectMake(-20, -20, 27, 40)];
    [self.view addSubview:coinTarget];

    // Determine where to spawn the coin along the Y axis
    int minYc = coinTarget.frame.size.width/2;
    int maxYc = screen.size.width - coinTarget.frame.size.width/2;
    int rangeYc = maxYc - minYc;
    int actualYc = (arc4random() % rangeYc) + minYc;


    // Determine speed of the target
    int minDurationc = 2.0;
    int maxDurationc = 4.0;
    int rangeDurationc = maxDurationc - minDurationc;
    int actualDurationc = (arc4random() % rangeDurationc) + minDurationc;



    theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.translation"];
    theAnimation.duration = 3.0;
    theAnimation.repeatCount=1.0;
    theAnimation.autoreverses=NO;
    [theAnimation setFromValue:[NSValue valueWithCGRect:CGRectMake(screen.size.height + (coinTarget.frame.size.height/2), actualYc, 27, 40)]];
    [theAnimation setToValue:[NSValue valueWithCGRect:CGRectMake(-50, actualYc, 27, 40)]];
    theAnimation.delegate = self;
    [CATransaction setCompletionBlock:^{
        [coinTarget removeFromSuperview];
    }];
    [coinTarget.layer addAnimation:theAnimation forKey:@"animations"];

此外,这是我目前的触控代码:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:[touch view]];
    NSLog(@"touched");

}

我做错了什么?

1 个答案:

答案 0 :(得分:1)

被触摸的视图的引用计数为零,因此它已被释放。