在Swift中使用iAction但不使用viewDidLoad的函数

在Swift中使用iAction但不使用viewDidLoad的函数,swift,animation,swift3,geometry,viewdidload,Swift,Animation,Swift3,Geometry,Viewdidload,我想在我的项目中添加一个圆形动画。我找到了 这段代码在我的主页viewDidLoad上工作,但在另一个页面上不工作。 但是,如果我在所需页面上放置一个测试按钮,它就会工作。当我在viewDidLoad中调用该函数时,动画已经结束,即使我放置了10秒或60秒 代码如下: 进口基金会 导入UIKit 类CircleView:UIView{ var circleLayer:现金支付者! 重写initframe:CGRect{ super.initframe:frame self.background

我想在我的项目中添加一个圆形动画。我找到了

这段代码在我的主页viewDidLoad上工作,但在另一个页面上不工作。 但是,如果我在所需页面上放置一个测试按钮,它就会工作。当我在viewDidLoad中调用该函数时,动画已经结束,即使我放置了10秒或60秒

代码如下:

进口基金会 导入UIKit 类CircleView:UIView{ var circleLayer:现金支付者! 重写initframe:CGRect{ super.initframe:frame self.backgroundColor=UIColor.clear //使用UIBezierPath作为创建层的CGPath的简单方法。 //路径应该是整个圆。 设circlePath=UIBezierPatharcCenter:CGPointx:frame.size.width/2.0,y:frame.size.height/2.0,半径:frame.size.width-10/2,StartTangle:0.0,endAngle:CGFloatM_PI*2.0,顺时针:true //使用路径、颜色和线宽设置CAShapeLayer circleLayer=CAShapeLayer circleLayer.path=circlePath.cgPath circleLayer.fillColor=UIColor.clear.cgColor circleLayer.strokeColor=UIColor.white.cgColor circleLayer.lineWidth=2.0; //最初不要画圆圈 circleLayer.strokeEnd=0.0 //将circleLayer添加到视图图层的子图层 layer.addSublayerClarLayer } 必需的初始化?编码器aDecoder:NSCoder{ fatalErrorinitcoder:尚未实现 } func animateCircle_uu持续时间:时间间隔{ //我们希望为circleLayer的strokeEnd属性设置动画 让动画=CABasicAnimationkeyPath:strokeEnd //适当设置动画持续时间 animation.duration=持续时间 //从0无圆到1整圆设置动画 animation.fromValue=1 animation.toValue=0 //执行线性动画,即动画的速度保持不变 animation.timingFunction=CAMediaTimingFunctionname:kCAMediaTimingFunctionLinear //现在将circleLayer的strokeEnd属性设置为1.0,使其成为 //动画结束时的右键值。 circleLayer.strokeEnd=0.0 //做真实的动画 circleLayer.addanimation,forKey:animateCircle }
} 实例化UIViewController时调用viewDidLoad。显示UIViewController时调用ViewDidDisplay。您需要将动画移动到ViewDidDisplay

是否尝试使用ViewDidDisplay而不是viewDidLoad?