Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >圆动画滞后

圆动画滞后
EN

Stack Overflow用户
提问于 2018-06-24 15:44:59
回答 1查看 108关注 0票数 0

我正在用iOS制作一个圆动画,它看起来像这样:

Circle animation video link

在此动画中,当圆重复动画时,它会滞后于动画之间。我想让这个动画更流畅。

我使用清晰的视图,在其中我添加了一个圆形图层。下面是创建圆形图层的代码:

代码语言:javascript
运行
AI代码解释
复制
//Create circle shape layer
func setupCircle(frame: CGRect, strokeColor: CGColor)->CAShapeLayer {

    let circlePath = UIBezierPath(arcCenter: CGPoint(x: frame.size.height/2, y: frame.size.height/2), radius: CGFloat((frame.size.height/2)-25), startAngle: CGFloat(0), endAngle:CGFloat(Double.pi * 2), clockwise: true)

    let shapeLayer = CAShapeLayer()
    shapeLayer.path = circlePath.cgPath

    //change the fill color
    shapeLayer.fillColor = UIColor.clear.cgColor
    //you can change the stroke color
    shapeLayer.strokeColor = strokeColor
    //you can change the line width
    shapeLayer.lineWidth = 3.0

    return shapeLayer
}

下面是圆形图层动画的代码:

代码语言:javascript
运行
AI代码解释
复制
    //Add animation to circle layer
    func getAnimationForCircle(frame: CGRect) -> CAAnimation {

        let newPath = UIBezierPath(arcCenter: CGPoint(x: frame.size.height/2, y: frame.size.height/2), radius: CGFloat((frame.size.height/2)+25), startAngle: CGFloat(0), endAngle:CGFloat(Double.pi * 2), clockwise: true)

        let pathAnim = CABasicAnimation.init(keyPath: "path")
        pathAnim.toValue = newPath.cgPath

        let alphaAnimation = CABasicAnimation.init(keyPath: "opacity")
        alphaAnimation.fromValue = 0.75
        alphaAnimation.toValue = 0.0

        let combinedAnim = CAAnimationGroup.init()
        combinedAnim.animations = [pathAnim, alphaAnimation]
        combinedAnim.isRemovedOnCompletion = false
        combinedAnim.repeatCount = .infinity
//        combinedAnim.speed = 0.2
        combinedAnim.duration = 1.0
        combinedAnim.fillMode = kCAFillModeBoth

        return combinedAnim
    }

我怎样才能使这个动画流畅而没有任何延迟?

任何帮助都将不胜感激。谢谢!

EN

回答 1

Stack Overflow用户

发布于 2018-06-24 17:21:41

我唯一的建议是使单个动画与组动画的持续时间相同,以便它们在重复次数和持续时间上匹配。我修改了不透明度,使其具有关键帧。持续时间的不同可能导致了您的口吃或滞后。

代码语言:javascript
运行
AI代码解释
复制
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        let shapeLayer = self.setupCircle(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width/2, height: self.view.bounds.width/2), strokeColor: UIColor.blue.cgColor)

        let sub = UIView(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width/2, height: self.view.bounds.width/2))
        shapeLayer.frame = sub.bounds
        sub.layer.addSublayer(shapeLayer)
        sub.center = self.view.center
        self.view.addSubview(sub)


        let anim = self.getAnimationForCircle(frame: shapeLayer.frame)
        shapeLayer.add(anim, forKey: nil)
    }



    //Create circle shape layer
    func setupCircle(frame: CGRect, strokeColor: CGColor)->CAShapeLayer {

        let circlePath = UIBezierPath(arcCenter: CGPoint(x: frame.size.height/2, y: frame.size.height/2), radius: CGFloat((frame.size.height/2)-25), startAngle: CGFloat(0), endAngle:CGFloat(Double.pi * 2), clockwise: true)

        let shapeLayer = CAShapeLayer()
        shapeLayer.path = circlePath.cgPath

        //change the fill color
        shapeLayer.fillColor = UIColor.clear.cgColor
        //you can change the stroke color
        shapeLayer.strokeColor = strokeColor
        //you can change the line width
        shapeLayer.lineWidth = 3.0

        return shapeLayer
    }

    //Add animation to circle layer
    func getAnimationForCircle(frame: CGRect) -> CAAnimation {

        let newPath = UIBezierPath(arcCenter: CGPoint(x: frame.size.height/2, y: frame.size.height/2), radius: CGFloat((frame.size.height/2)+25), startAngle: CGFloat(0), endAngle:CGFloat(Double.pi * 2), clockwise: true)

        let pathAnim = CABasicAnimation.init(keyPath: "path")
        pathAnim.toValue = newPath.cgPath
        pathAnim.duration = 1.0

        let alphaAnimation = CAKeyframeAnimation.init(keyPath: "opacity")
        alphaAnimation.values = [0,0.75,0,0]
        alphaAnimation.keyTimes = [0,0.1,0.87,1]
        alphaAnimation.duration = 1.0

        let combinedAnim = CAAnimationGroup.init()
        combinedAnim.animations = [pathAnim, alphaAnimation]
        combinedAnim.isRemovedOnCompletion = false
        combinedAnim.repeatCount = .infinity

        combinedAnim.duration = 1.0
        combinedAnim.fillMode = kCAFillModeBoth

        return combinedAnim
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51011477

复制
相关文章
animate 动画滞后执行的解决方案
针对 jQuery 中 slideUp、slideDown、animate 等动画运用时出现的滞后反复执行等问题的解决方法有如下:
Krry
2018/09/10
1.2K0
[066]Camera360录像预览滞后
Camera360应用录像预览在我们的设备上存在滞后的问题。 具体现象在你快速摄像头角度的时候,预览画面不能及时更新到当前摄像头拍摄的角度的画面, 或者你拍你自己的手,快速握拳展开,预览画面需要延迟一些时间才能显示展开的手
王小二
2021/11/24
9540
[066]Camera360录像预览滞后
计算几何之圆与圆的交点
计算圆与圆的交点,需要用到余弦定理 步骤如下: 求出两个圆的圆心距d 求出向量c2.c-c1.c与c1.c到某交点的向量夹角a 求出向量c2.c-c1.c与x轴的夹角t 那么,两个交点就分别是以c1.c为起点,大小为c1.r,角度为t+a、t-a的两个向量 题目:CGL_7_E AC代码: #include <iostream> #include <cstdio> #include <math.h> using namespace std; #define COUNTER_CLOCKWISE -1 /
灯珑LoGin
2022/10/31
7100
计算几何之圆与圆的交点
一颗心到一个圆的演变,javafx框架tornadofx动画演示
采用runAsync和Thread.sleep(10)实现动画效果 import javafx.geometry.Pos import javafx.scene.canvas.GraphicsContext import javafx.scene.control.RadioButton import javafx.scene.paint.Color import javafx.scene.text.FontWeight import tornadofx.* import java.util.* import
用户6167008
2019/09/16
6670
一颗心到一个圆的演变,javafx框架tornadofx动画演示
[or8lllfpxg.gif] import javafx.animation.AnimationTimer import javafx.application.Application import javafx.application.Platform import javafx.geometry.Pos import javafx.scene.canvas.GraphicsContext import javafx.scene.control.RadioButton import javafx.sce
用户6167008
2019/09/04
7850
一颗心到一个圆的演变,javafx框架tornadofx动画演示
圆和线
如果有一个圆,在圆上有很多数学上的点,这些点足够多。那么将这些点拿出来,而不是很表示一段有趣的序列
林德熙
2019/06/15
6910
OpenCV 圆检测
OpenCV 的 HoughCircles() 函数可以用来在一张单通道图像里检测圆形物体。下面是各参数的介绍:
用户6021899
2019/09/08
2.3K0
canvas练习 - 圆
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>canvas画圆</title> <style> canvas{ border:
xing.org1^
2018/10/10
6120
canvas练习 - 圆
电流电压超前滞后的几个动图
网络声明:图片来源于网络,转载目的在于传递更多信息,版权归原作者所有,如涉及侵权,请后台联系硬件大熊进行处理。
硬件大熊
2022/12/06
5980
电流电压超前滞后的几个动图
# python # # 分形 # 圆
相关: # python # # 分形 # 唱片 # coding: utf-8 import turtle import random def draw_circles(pen, xy, radius, step, is_homocentric = True): (x, y) = xy pen.ht() pen.color("#FFFFFF") pen.goto(x, y) for r in range(radius, step, step): rgb =
滚神大人
2019/09/10
9130
自回归滞后模型进行多变量时间序列预测
显示了关于不同类型葡萄酒销量的月度多元时间序列。每种葡萄酒类型都是时间序列中的一个变量。
deephub
2023/01/18
1.2K0
圆排列问题-回溯法
问题描述:     给定n个大小不等的圆 c1 c2 c3 c4 要将n个圆排进一个矩形框中,且要求底边相切。找出有最小长度的圆排列。     例如:当n=3,且所给的3个圆半径分别为1,1,2时,这3个圆的最小长度的圆排列 最小长度为2+4根号2. 算法设计:     设开始的a =【r1,r2,r3,r4...rn】是所给的n歌圆半径。     CirclePerm(n,a)返回最小长度。     Center计算当前选择的圆中心的横坐标。     Compute计算当前圆排列的长度。     数组r当
用户1154259
2018/01/17
1.4K0
圆的反演变换
挺神奇的东西,网上没有多少资料,我也不是太懂,代码什么的都没写过,那就抄一下百度百科吧
attack
2018/08/01
1.1K0
晶圆键合技术
  晶圆键合技术是指通过化学和物理作用将两块已镜面抛光的同质或异质的晶片紧密地结合起来,晶片接合后,界面的原子受到外力的作用而产生反应形成共价键结合成一体,并使接合界面达到特定的键合强度。
用户2760455
2022/06/08
1.3K0
晶圆键合技术
canva之圆的绘制
                        // context.arc(x,y,r,sAngle,eAngle,counterclockwise) 创建弧/曲线
十月梦想
2018/08/29
7430
铣整圆编程说明
N05 G00 X10 Y25 Z1 S1250 M3;刀具快速移动(G00)到 P01 点。
lrglu
2022/03/30
5.6K0
铣整圆编程说明
opencv绘制矩形和圆
淼学派对
2023/10/14
3170
opencv绘制矩形和圆
HoughCircle找圆总结——opencv
Opencv内部提供了一个基于Hough变换理论的找圆算法,HoughCircle与一般的拟合圆算法比起来,各有优势:优势:HoughCircle对噪声点不怎么敏感,并且可以在同一个图中找出多个圆;反观拟合圆算法,单纯的拟合结果容易受噪声点的影响,且不支持一个输入中找多个圆 缺点:原始的Hough变换找圆,计算量很大,而且如果对查找圆的半径不加控制,不但运算量巨大,而且精度也不足,在输入噪声点不多的情况下,找圆效果远不如拟合找圆;为了提高找圆精度,相比拟合法,需要提供更多的参数加以控制,参数要求比较严格,且总体稳定性不佳 OpenCV内的HoughCircles对基础的Hough变换找圆做了一定的优化来提高速度,它不再是在参数空间画出一个完整的圆来进行投票,而只是计算轮廓点处的梯度向量,然后根据搜索的半径R在该梯度方向距离轮廓点距离R的两边各投一点,最后根据投票结果图确定圆心位置,其示意图如图1
vv彭
2020/11/24
1.4K0
圆填充( CIRCLE PACKING)算法圆堆图圆形空间填充算法可视化
首先,我们创建一组随机圆,位于边界正方形的中心部分,较小的圆比较大的圆更常见。我们将圆的大小表示为面积。
拓端
2021/12/13
4.1K0
圆填充( CIRCLE PACKING)算法圆堆图圆形空间填充算法可视化
动图详解电流电压的超前与滞后
由于Sin[ωt]在求导或积分后会出现Sin[ωt±90°],所以对于接上了正弦波的电感、电容,横坐标为ωt时可以观察到波形超前滞后的现象。
AI 电堂
2021/01/22
1.1K0

相似问题

Tkinter中的Entry Widget导致的空白

114

Tkinter文件路径中的文件读取

11

Tkinter类中entry.get()的问题

12

Python Tkinter中的Entry.get()错误

11

限制Tkinter Entry小部件中的值

82
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档