gpt4 book ai didi

swift - 圆形比例动画效果有点奇怪

转载 作者:可可西里 更新时间:2023-11-01 00:35:40 25 4
gpt4 key购买 nike

我正在为我的 GMSMarker 制作动画,以便它在几秒钟内脉冲一次。

func addWave()
{
// circleView.layer.cornerRadius = size / 2

//Scale
let scaleAnimation = CABasicAnimation(keyPath: "transform.scale")
scaleAnimation.fromValue = 1
scaleAnimation.toValue = zoom

//Opacity
let alphaAnimation = CABasicAnimation(keyPath: "opacity")
alphaAnimation.toValue = 0.0

//Corner radius
// let cornerRadiusAnimation = CABasicAnimation(keyPath: "cornerRadius")
// cornerRadiusAnimation.fromValue = size / 2
// cornerRadiusAnimation.toValue = (size * zoom)/2

//Animation Group
let animations: [CAAnimation] = [scaleAnimation, alphaAnimation]

let animationGroup = CAAnimationGroup()
animationGroup.duration = duration
animationGroup.animations = animations
animationGroup.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
animationGroup.repeatCount = 1
animationGroup.fillMode = kCAFillModeForwards
animationGroup.isRemovedOnCompletion = false
circleView.layer.add(animationGroup, forKey: "group")
}

结果是这样的:

enter image description here enter image description here

如果我取消注释 Corner radius 部分,它看起来像这样:

enter image description here

所以我需要一个建议。

最佳答案

根据我的观察,我认为这一定是您的 CAShapeLayer 路径错误的问题,因此圆圈被遮蔽了。

刚刚写了一个雷达动画,希望对你有帮助。

    let scaleAnimation = CABasicAnimation(keyPath: "transform.scale")
scaleAnimation.fromValue = 0
scaleAnimation.toValue = 1

let alphaAnimation = CABasicAnimation(keyPath: "opacity")
alphaAnimation.fromValue = 1
alphaAnimation.toValue = 0

let animations = CAAnimationGroup()
animations.duration = 0.8
animations.repeatCount = Float.infinity
animations.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseOut)
animations.animations = [scaleAnimation, alphaAnimation]

circleView.layer.add(animations, forKey: "animations")

我所做的是,我使用了两个 CAShapeLayer(一个是橙色标记,另一个是标记背面的雷达层)。动画应用于雷达层。

    radarLayer = CAShapeLayer()
radarLayer.frame = CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height);
radarLayer.path = UIBezierPath(rect: radarLayer.frame).cgPath
radarLayer.fillColor = UIColor.orange.cgColor
radarLayer.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2)
radarLayer.cornerRadius = radarLayer.frame.size.width/2
radarLayer.masksToBounds = true
radarLayer.opacity = 0
self.layer.addSublayer(radarLayer)

circleLayer = CAShapeLayer()
circleLayer.frame = CGRect(x: 0, y: 0, width: 16, height: 16);
circleLayer.path = UIBezierPath(rect: circleLayer.frame).cgPath
circleLayer.fillColor = UIColor.orange.cgColor
circleLayer.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2)
circleLayer.cornerRadius = circleLayer.frame.size.width/2
circleLayer.masksToBounds = true
self.layer.addSublayer(circleLayer)

附言。我是 Swift 的新手,如果我哪里错了,请启发我 :)

示例:http://i.imgur.com/v2jFWgw.jpg

关于swift - 圆形比例动画效果有点奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44136720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com