CABasicAnimation无法在设备上运行(iPhone 5s)

时间:2014-03-31 23:12:43

标签: ios cabasicanimation

我有一个CABasicAnimation,可以在图像上创建虹膜擦除效果。简而言之,动画在模拟器上运行良好,但设备上没有任何乐趣。定时器仍然正确触发,并且动画完成的块被调用但是没有可见的动画。

以下是让虹膜擦拭工作的代码:

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

}

在使用CAAnimations等时,我应该注意iOS7中的某些内容吗?或者代码中是否有错误?

请注意,此代码来自:How do you achieve a "clock wipe"/ radial wipe effect in iOS?

1 个答案:

答案 0 :(得分:1)

我认为问题(或至少部分问题)可能就是这一行:

[_resultsImage setFrame:_imageView.bounds];

应该阅读

[_resultsImage setBounds:_imageView.bounds];

相反。如果将FRAME设置为图像视图的边界,则会在其超视图中将图像视图移动到0.0。

我也不清楚_imageView是什么,而不是_resultsImage。

我会在调试器中单步执行代码,查看为图像视图和遮罩层设置的框架矩形,以及计算的所有其他值。