• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 20:13:15 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

在我的应用程序中,每 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap