CABasicAnimation not working on device (iPhone 5s)

2019-09-19 08:29发布

I have a CABasicAnimation that creates an iris wipe effect on an image. In short, the animation works fine on the simulator but there is no joy on device. The timers still fire correctly and the animationCompleted block gets called however there is no visible animation.

Here is the code to get the iris wipe working:

- (void)irisWipe
{
animationCompletionBlock theBlock;

_resultsImage.hidden = FALSE;//Show the image view
[_resultsImage setImage:[UIImage imageNamed:@"logoNoBoarder"]];
[_resultsImage setBackgroundColor:[UIColor clearColor]];
[_resultsImage setFrame:_imageView.bounds];

//Create a shape layer that we will use as a mask for the waretoLogoLarge image view
CAShapeLayer *maskLayer = [CAShapeLayer layer];

CGFloat maskHeight = _resultsImage.layer.bounds.size.height;
CGFloat maskWidth = _resultsImage.layer.bounds.size.width;

CGPoint centerPoint;
centerPoint = CGPointMake( maskWidth/2, maskHeight/2);

//Make the radius of our arc large enough to reach into the corners of the image view.
CGFloat radius = sqrtf(maskWidth * maskWidth + maskHeight * maskHeight)/2;
//  CGFloat radius = MIN(maskWidth, maskHeight)/2;

//Don't fill the path, but stroke it in black.
maskLayer.fillColor = [[UIColor clearColor] CGColor];
maskLayer.strokeColor = [[UIColor blackColor] CGColor];

maskLayer.lineWidth = radius; //Make the line thick enough to completely fill the circle we're drawing
// maskLayer.lineWidth = 10; //Make the line thick enough to completely fill the circle we're drawing

CGMutablePathRef arcPath = CGPathCreateMutable();

//Move to the starting point of the arc so there is no initial line connecting to the arc
CGPathMoveToPoint(arcPath, nil, centerPoint.x, centerPoint.y-radius/2);

//Create an arc at 1/2 our circle radius, with a line thickess of the full circle radius
CGPathAddArc(arcPath,
             nil,
             centerPoint.x,
             centerPoint.y,
             radius/2,
             3*M_PI/2,
             -M_PI/2,
             NO);

maskLayer.path = arcPath;

//Start with an empty mask path (draw 0% of the arc)
maskLayer.strokeEnd = 1.0;

CFRelease(arcPath);

//Install the mask layer into out image view's layer.
_resultsImage.layer.mask = maskLayer;

//Set our mask layer's frame to the parent layer's bounds.
_resultsImage.layer.mask.frame = _resultsImage.layer.bounds;

//Create an animation that increases the stroke length to 1, then reverses it back to zero.
CABasicAnimation *swipe = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
swipe.duration = 1;
swipe.delegate = self;
[swipe setValue: theBlock forKey: kAnimationCompletionBlock];

swipe.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
swipe.fillMode = kCAFillModeForwards;
swipe.removedOnCompletion = NO;
swipe.autoreverses = NO;

swipe.toValue = [NSNumber numberWithFloat: 0];

//Set up a completion block that will be called once the animation is completed.
theBlock = ^void(void)
{
    NSLog(@"completed");
};

[swipe setValue: theBlock forKey: kAnimationCompletionBlock];

// doingMaskAnimation = TRUE;
[maskLayer addAnimation: swipe forKey: @"strokeEnd"];

}

Is there something in iOS7 I should be aware of when working with CAAnimations etc? OR is there a error in the code?

Note this code was sourced from: How do you achieve a "clock wipe"/ radial wipe effect in iOS?

1条回答
ゆ 、 Hurt°
2楼-- · 2019-09-19 09:21

I think the problem (or at least part of it) may be this line:

[_resultsImage setFrame:_imageView.bounds];

That should read

[_resultsImage setBounds:_imageView.bounds];

Instead. If you set the FRAME to the bounds of the image view, you're going to move the image view to 0.0 in its superview.

I'm also not clear what _imageView is, as opposed to _resultsImage.

I would step through your code in the debugger, looking at the frame rectangles that are being set for the image view and mask layer, and all the other values that are calculated.

查看更多
登录 后发表回答