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

标签 ios iphone objective-c

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

error: memory read failed for 0x42200000

这是创建硬币对象的方法:

-(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");

}

我做错了什么?

最佳答案

被触及的 View 的引用计数为零,因此已被释放。

关于触摸屏幕时 ios 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22391893/

相关文章:

android - Paypal 移动 SDKS(自适应支付)

iphone - 为什么会出现 NSException 错误?

ios - 我在 init 中有一些泄漏。我看没人漏

ios - 设置 UITextField 的最大字符长度

ios - 当我在 xCode 5 中验证我的应用程序时,出现错误的广告标识符 [IDFA] 使用情况

ios - iOS Swift 格式正确的 ."UserInfo={NSDebugDescription=Garbage at end ---> Data cannot be read because it isn' t 末尾的垃圾

ios - 如何有选择地选择与Xcode 7.5 beta兼容的设备

iphone - 将 UIGestureRecognizer 从一个 View Controller 转发到另一个 View Controller

iphone - UITextField 在编辑时更改字体

ios - 获取不可见的 UITableViewCell