Ios CABasicAnimation不围绕中心缩放
我想执行不透明和规模的影响,同时我的动画工作完美,但它的位置是不正确的。我想在中心执行动画。 这是我的密码Ios CABasicAnimation不围绕中心缩放,ios,iphone,swift,core-animation,cabasicanimation,Ios,Iphone,Swift,Core Animation,Cabasicanimation,我想执行不透明和规模的影响,同时我的动画工作完美,但它的位置是不正确的。我想在中心执行动画。 这是我的密码 btn.backgroundColor = UIColor.yellowColor() let stroke = UIColor(red:236.0/255, green:0.0/255, blue:140.0/255, alpha:0.8) let pathFrame = CGRectMake(24, 13, btn.bounds.size.height/2,
btn.backgroundColor = UIColor.yellowColor()
let stroke = UIColor(red:236.0/255, green:0.0/255, blue:140.0/255, alpha:0.8)
let pathFrame = CGRectMake(24, 13, btn.bounds.size.height/2, btn.bounds.size.height/2)
let circleShape1 = CAShapeLayer()
circleShape1.path = UIBezierPath(roundedRect: pathFrame, cornerRadius: btn.bounds.size.height/2).CGPath
circleShape1.position = CGPoint(x: 2, y: 2)
circleShape1.fillColor = stroke.CGColor
circleShape1.opacity = 0
btn.layer.addSublayer(circleShape1)
circleShape1.anchorPoint = CGPoint(x: 0.5, y: 0.5)
let scaleAnimation = CABasicAnimation(keyPath: "transform.scale")
scaleAnimation.fromValue = NSValue(CATransform3D: CATransform3DIdentity)
scaleAnimation.toValue = NSValue(CATransform3D: CATransform3DMakeScale(2.0, 2.0, 1))
let alphaAnimation = CABasicAnimation(keyPath: "opacity")
alphaAnimation.fromValue = 1
alphaAnimation.toValue = 0
CATransaction.begin()
let animation = CAAnimationGroup()
animation.animations = [scaleAnimation, alphaAnimation]
animation.duration = 1.5
animation.repeatCount = .infinity
animation.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
circleShape1.addAnimation(animation, forKey:"Ripple")
CATransaction.commit()
您需要创建具有{0,0}位置的路径帧,并为层设置正确的帧:
let pathFrame = CGRectMake(0, 0, btn.bounds.size.height/2, btn.bounds.size.height/2)
....
circleShape1.frame = CGRect(x: 0, y: 0, width: pathFrame.width, height: pathFrame.height)
circleShape1.position = CGPoint(x: 2, y: 2)
若你们想用{13,24}位置创建路径,你们需要改变层的宽度和高度。你们的形状应该在图层的中心 我从来没有使用过子层,所以我会使用子视图,这样代码就容易多了:
btn.backgroundColor = UIColor.yellow
let circleShape1 = UIView()
circleShape1.frame.size = CGSize(width: btn.frame.height / 2, height: btn.frame.height / 2)
circleShape1.center = CGPoint(x: btn.frame.width / 2, y: btn.frame.height / 2)
circleShape1.layer.cornerRadius = btn.frame.height / 4
circleShape1.backgroundColor = UIColor(red:236.0/255, green:0.0/255, blue:140.0/255, alpha:0.8)
circleShape1.alpha = 1
btn.addSubview(circleShape1)
UIView.animate(withDuration: 1,
delay: 0,
options: [.repeat, .curveLinear],
animations: {
circleShape1.transform = CGAffineTransform(scaleX: 5, y: 5)
circleShape1.alpha = 0.4
}, completion: nil)
问题是您没有正确地处理帧。你说:
let circleShape1 = CAShapeLayer()
但是你忘了给圆形1
一帧!因此,它的大小为零,当你为它设置动画时,会发生非常奇怪的事情。下一行的工作应该是分配一个帧。例如:
circleShape1.frame = pathFrame
可能是也可能不是正确的框架;可能不是。但你需要弄清楚这一点
然后,需要根据形状层的边界固定贝塞尔路径的框架:
这里的一个关键问题是,您无法在一次移动中创建子层、将其添加到界面并设置动画。只有当层已经在界面中时,才能执行动画。感谢您回答我的问题,但我希望继续动画,但您的回答动画只有一次。执行。@jigneshVadadoriya我的答案是一个继续动画,它在UIView中有.repeat as选项。animate我有检查,但它在给予时不执行继续动画。repeat as选项。谢谢回答我的问题。我试图设置帧,但动画看起来像从左到右下角。我想要动画执行中心。
circleShape1.path = UIBezierPath(roundedRect: circleShape1.bounds // ...