iPhone / xCode - 第四轮动画代码上的EXC_BAD_ACCESS

时间:2014-07-03 06:21:07

标签: iphone objective-c c xcode animation

此代码适用于前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];

}

1 个答案:

答案 0 :(得分:1)

删除版本。 我的建议是有效的,因为对象imageViewForAnimation是你的方法的参数,你的方法中没有任何东西是为对象添加一个保留。所以有意义的是,imageViewForAnimation对象的保留计数不必要地减少了。只有在保留后才会发布。保持这些范围相同。

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