OStack程序员社区-中国程序员成长平台

标题: 触摸屏幕时ios应用程序崩溃 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 20:13
标题: 触摸屏幕时ios应用程序崩溃

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

error: memory read failed for 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)touchesBeganNSSet *)touches withEventUIEvent *)event {

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

}

我做错了什么?



Best Answer-推荐答案


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

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






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4