[英]CABasicAnimation not scaling around center
I want to perform opacity And Scale effect at same time my animation work perfect but it's position is not proper. 我想同时执行不透明度和缩放效果,但我的动画效果完美,但是位置不合适。 i want to perform animation on center.
我想在中心执行动画。 This is my code.
这是我的代码。
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()
The problem is that you are not grappling with frames correctly. 问题是您没有正确地抓取框架。 You say:
你说:
let circleShape1 = CAShapeLayer()
But you have forgotten to give circleShape1
a frame! 但是您忘了给
circleShape1
一个框架! Thus, its size is zero, and very weird things happen when you animate it. 因此,它的大小为零,并且在设置动画时会发生非常奇怪的事情。 Your job in the very next line should be to assign
circleShape1
a frame. 在下一行中,您的工作应该是为
circleShape1
分配一个框架。 Example: 例:
circleShape1.frame = pathFrame
That may or may not be the correct frame; 那可能是正确的框架,也可能不是正确的框架。 it probably isn't.
可能不是。 But you need to figure that out.
但您需要弄清楚。
Then, you need to fix the frame of your Bezier path in terms of the shape layer's bounds : 然后,您需要根据形状图层的边界来固定Bezier路径的框架:
circleShape1.path = UIBezierPath(roundedRect: circleShape1.bounds // ...
You need to create path frame with {0,0} position, and set correct frame to the Layer: 您需要使用{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)
If you want to create path with {13,24} position you need to change width and height in layer. 如果要使用{13,24}位置创建路径,则需要更改图层的宽度和高度。 Your shape should be in center of layer.
您的形状应位于图层的中心。
I have never worked with sublayers so I would make it with a subview instead, makes the code a lot easier: 我从未使用过子层,因此我将其与子视图结合使用,使代码更加容易:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.