【问题标题】:iPhone/xCode - EXC_BAD_ACCESS on 4th run of Animation CodeiPhone/xCode - 第 4 次运行动画代码时出现 EXC_BAD_ACCESS
【发布时间】:2014-07-03 06:21:07
【问题描述】:

此代码在前 3 次有效,每次都没有失败,它在第 4 次运行时崩溃,并带有 Thread 1: EXC_BAD_ACCESS (code=1, address=0x4)。研究了一下午,还是什么都没有,是不是我没有发布什么?

- (void)animateImageView:(UIImageView *)imageViewForAnimation
{
        imageViewForAnimation.alpha = 1.0f;
        CGRect imageFrame = imageViewForAnimation.frame;
        //Your image frame.origin from where the animation need to get start
        CGPoint viewOrigin = imageViewForAnimation.frame.origin;
        viewOrigin.y = viewOrigin.y + imageFrame.size.height / 2.0f;
        viewOrigin.x = viewOrigin.x + imageFrame.size.width / 2.0f;

        imageViewForAnimation.frame = imageFrame;
        imageViewForAnimation.layer.position = viewOrigin;
        [self.view addSubview:imageViewForAnimation];

        // Set up fade out effect
        CABasicAnimation *fadeOutAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
        [fadeOutAnimation setToValue:[NSNumber numberWithFloat:0.3]];
        fadeOutAnimation.fillMode = kCAFillModeForwards;
        fadeOutAnimation.removedOnCompletion = NO;

        // Set up scaling
        CABasicAnimation *resizeAnimation = [CABasicAnimation animationWithKeyPath:@"bounds.size"];
        [resizeAnimation setToValue:[NSValue valueWithCGSize:CGSizeMake(40.0f, imageFrame.size.height * (40.0f / imageFrame.size.width))]];
        resizeAnimation.fillMode = kCAFillModeForwards;
        resizeAnimation.removedOnCompletion = NO;

        // Set up path movement
        CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
        pathAnimation.calculationMode = kCAAnimationPaced;
        pathAnimation.fillMode = kCAFillModeForwards;
        pathAnimation.removedOnCompletion = NO;
        //Setting Endpoint of the animation
        CGPoint endPoint = CGPointMake(250.0f, 10.0f);
        //to end animation in last tab use
        //CGPoint endPoint = CGPointMake( 320-40.0f, 480.0f);
        CGMutablePathRef curvedPath = CGPathCreateMutable();
        CGPathMoveToPoint(curvedPath, NULL, viewOrigin.x, viewOrigin.y);
        CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, viewOrigin.y, endPoint.x, viewOrigin.y, endPoint.x, endPoint.y);
        pathAnimation.path = curvedPath;
        CGPathRelease(curvedPath);

        CAAnimationGroup *group = [CAAnimationGroup animation];
        group.fillMode = kCAFillModeForwards;
        group.removedOnCompletion = NO;
        [group setAnimations:[NSArray arrayWithObjects:fadeOutAnimation, pathAnimation, resizeAnimation, nil]];
        group.duration = 0.7f;
        group.delegate = self;
        //[group setValue:@"groupAnimation" forKey:@"animationName"];
        [group setValue:imageViewForAnimation forKey:@"imageViewBeingAnimated"];

        [imageViewForAnimation.layer addAnimation:group forKey:@"savingAnimation"];


        [imageViewForAnimation release];

}

【问题讨论】:

  • 删除版本。更好的是,使用 ARC
  • 非常感谢,我刚刚在那一行上浪费了 4 个小时。我很感兴趣,为什么发布会这样做。以及我将如何实现 ARC
  • ARC 您只需转到 Xcode 中的菜单栏,编辑 > 重构 > 转换为 Objective-C ARC... 然后仔细进行操作。它将从 Obj-C 的东西中删除保留和释放。
  • 我的建议有效,因为对象 imageViewForAnimation 是您的方法的参数,并且您的方法中没有任何内容向对象添加保留。因此,imageViewForAnimation 对象的保留计数被不必要地减少是有道理的。仅在您保留后才释放。将它们保持在同一范围内。

标签: iphone objective-c c xcode animation


【解决方案1】:

删除版本。 我的建议有效,因为对象 imageViewForAnimation 是您的方法的参数,而您的方法中没有任何内容向对象添加保留。因此,imageViewForAnimation 对象的保留计数被不必要地减少是有道理的。仅在您保留后才释放。将它们保持在同一范围内。

更好的是,使用 ARC ARC 您只需转到 Xcode 中的菜单栏,编辑 > 重构 > 转换为 Objective-C ARC... 然后仔细进行操作。它将从 Obj-C 的东西中删除保留和释放。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    相关资源
    最近更新 更多