简体   繁体   中英

ios app crashes when screen is touched

In my app, a method (addCoinTarget) is called every 2 seconds via NSTimer. I want to have the app perform an NSLog when the UIView (Coin in this case) is called. However, whenever the screen is touched even if its not on a Coin object, the app crashes with the error:

error: memory read failed for 0x42200000

This is the method that creates a Coin object:

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

Also, this is my touch code so far:

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

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

}

What have I done wrong?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM