ios - 将背景图像保持在动画的中心

标签 ios swift uiviewanimation

我有动画设置,可将图像大小调整为原始大小的 1.3 倍左右。动画和一切都工作正常,但图像正在向左上方移动。这意味着图像的位置在调整大小时不居中。我该如何解决这个问题

这些是我设置的动画

var borderWidth:CABasicAnimation = CABasicAnimation(keyPath: "borderWidth")
        borderWidth.fromValue = 0
        borderWidth.toValue = 5
        borderWidth.repeatCount = Float.infinity

        sender.layer.borderWidth = 0

        var increaseButtonHeight:CABasicAnimation = CABasicAnimation(keyPath: "bounds.size.height")
        increaseButtonHeight.fromValue = sender.frame.size.height
        increaseButtonHeight.toValue = sender.frame.size.height * 1.3

        var increaseButtonWidth: CABasicAnimation = CABasicAnimation(keyPath: "bounds.size.width")
        increaseButtonWidth.fromValue = sender.frame.size.width
        increaseButtonWidth.toValue = sender.frame.size.width * 1.3

        var boom:CAAnimationGroup = CAAnimationGroup()
        boom.animations = [borderWidth,increaseButtonWidth, increaseButtonHeight]
        boom.repeatCount = Float.infinity
        boom.duration = 0.5
        boom.autoreverses = true
        sender.layer.addAnimation(boom, forKey: "boom")

我是否需要设置一个新的动画,以便在动画发生时使按钮连续居中?

请帮忙

尼基尔

最佳答案

将图层的属性contentsGravity设置为kCAGravityCenter

关于ios - 将背景图像保持在动画的中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31061469/

相关文章:

用于从桌面访问 iOS 应用程序的文档目录的 Windows API?

ios - 测试时如何配置In-Memory数据库?

ios - 在 Swift 中使用 CLLocationManager 获取当前位置

ios - 在 UIViewController 转换中将 CAAnimation 与 View Animation 混合问题

objective-c - 在 iOS 的 Objective-C 中创建带有完成 block 的可中断动画?

ios - 带有自动反转的 UIView 动画

ios - 如何处理有很多返回值的 UIApplication handleOpenURL?

android - 基于现有的 Django 网站/应用程序开发移动应用程序

ios - Swift - 最高效的代码重用渐变方式?

swift - 继承 SLKTextViewController