ios - 暂停和恢复圆形动画
<p><p>我用这段代码来画圆。圆圈在 30 秒内绘制。有什么办法可以暂停和恢复画圈吗?我可以暂停时间,但我找不到暂停和恢复画圈的方法。</p>
<pre><code> int radius = 30;
circle = ;
// Make a circular shape
circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 100, 2.0*radius, 2.0*radius)
cornerRadius:radius].CGPath;
// Center the shape in self.view
circle.position = CGPointMake(CGRectGetMidX(self.view.frame)-radius,
CGRectGetMidY(self.view.frame)-radius);
// Configure the apperence of the circle
circle.fillColor = .CGColor;
circle.strokeColor = .CGColor;
circle.lineWidth = 10;
// Add to parent layer
;
// Configure animation
drawAnimation = ;
drawAnimation.duration = 30.0; // "animate over 10 seconds or so.."
drawAnimation.repeatCount = 1.0;// Animate only once..
drawAnimation.removedOnCompletion = NO; // Remain stroked after the animation...
// Animate from no part of the stroke being drawn to the entire stroke being drawn
drawAnimation.fromValue = ;
drawAnimation.toValue = ;
// Experiment with timing to get the appearence to look the way you want
drawAnimation.timingFunction = ;
// Add the animation to the circle
;
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您是否看过 Apple 关于暂停动画的技术说明:<a href="https://developer.apple.com/library/ios/qa/qa1673/_index.html" rel="noreferrer noopener nofollow">https://developer.apple.com/library/ios/qa/qa1673/_index.html</a> </p>
<p>所以,对于您的项目,这样的事情可以解决问题:</p>
<pre><code>- (void)viewDidLoad
{
;
int radius = 30;
_circle = ;
// Make a circular shape
_circle.path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 100, 2.0*radius, 2.0*radius)
cornerRadius:radius].CGPath;
// Center the shape in self.view
_circle.position = CGPointMake(CGRectGetMidX(self.view.frame)-radius,
CGRectGetMidY(self.view.frame)-radius);
// Configure the apperence of the circle
_circle.fillColor = .CGColor;
_circle.strokeColor = .CGColor;
_circle.lineWidth = 10;
_circle.strokeEnd = 0.0f;
// Add to parent layer
;
}
- (IBAction)didTapPlayPauseButton:(id)sender
{
if (!_drawAnimation) {
;
} else if(_circle.speed == 0){
;
} else {
;
}
}
- (void)addAnimation
{
// Configure animation
_drawAnimation = ;
_drawAnimation.duration = 30.0; // "animate over 10 seconds or so.."
_drawAnimation.repeatCount = 1.0;// Animate only once..
_drawAnimation.removedOnCompletion = NO; // Remain stroked after the animation...
// Animate from no part of the stroke being drawn to the entire stroke being drawn
_drawAnimation.fromValue = ;
_drawAnimation.toValue = ;
// Experiment with timing to get the appearence to look the way you want
_drawAnimation.timingFunction = ;
// Add the animation to the circle
;
}
- (void)pauseLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = ;
layer.speed = 0.0;
layer.timeOffset = pausedTime;
}
- (void)resumeLayer:(CALayer*)layer
{
CFTimeInterval pausedTime = ;
layer.speed = 1.0;
layer.timeOffset = 0.0;
layer.beginTime = 0.0;
CFTimeInterval timeSincePause = - pausedTime;
layer.beginTime = timeSincePause;
}
</code></pre>
<p>我把它扔到 Github 上的一个项目中看看它是否可以工作:<a href="https://github.com/perlmunger/PauseAnimation.git" rel="noreferrer noopener nofollow">https://github.com/perlmunger/PauseAnimation.git</a> </p></p>
<p style="font-size: 20px;">关于ios - 暂停和恢复圆形动画,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/20570961/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/20570961/
</a>
</p>
页:
[1]