LottieAnimationView的大小无法改变/太小(iOS/Swift)

7
无论我创建的视图是LOTAnimatedSwitch还是View,动画的图像总是非常小。 Lottie动画不占用我创建的视图的大小。这是否是从LottieFiles下载动画的问题?文件的尺寸为600x600像素。我正在使用Lottie版本2.5.0和Swift 4。例如:

enter image description here

    let animatedSwitch = LOTAnimatedSwitch(named: "toggle_switch")
    animatedSwitch.frame.origin = CGPoint(x: 8, y: separatorLineView.frame.height + separatorLineView.frame.origin.y + 8)
    animatedSwitch.frame.size = CGSize(width: dialogViewWidth - 16, height: 40)
    animatedSwitch.setProgressRangeForOnState(fromProgress: 0.5, toProgress: 1)
    animatedSwitch.setProgressRangeForOffState(fromProgress: 0, toProgress: 0.5)
    animatedSwitch.contentMode = .scaleAspectFill
    animatedSwitch.clipsToBounds = true
    animatedSwitch.backgroundColor = .purple

可能是您提供的框架太小了,所以出现了这个问题。 - Hardik Thakkar
4个回答

4
问题出在我从LottieFiles下载的文件上。为了解决动画/图标变小的问题,我在Adobe After Effects中缩放了组合大小以适应预览框架。我使用bodymovin插件将.aeb文件导出为.json文件。
Hardik的答案也很有帮助。问题在于我下载的文件周围有很多空白区域,直到我将图片放大为止。

4
尝试使用这段代码,我不确定它是否能解决你的问题。
let animatedSwitch = LOTAnimatedSwitch(named: "toggle_switch")
animatedSwitch.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
animatedSwitch.center = self.view.center
animatedSwitch.setProgressRangeForOnState(fromProgress: 0.5, toProgress: 1)
animatedSwitch.setProgressRangeForOffState(fromProgress: 0, toProgress: 0.5)
animatedSwitch.contentMode = .scaleAspectFit
self.view.addSubview(animatedSwitch)
self.view.backgroundColor = UIColor.lightGray

这完全没有帮助。 - Aakash Dave
这仅适用于toggle_switch类型的LOTAnimatation。上述代码在设备上测试后粘贴。 - Hardik Thakkar
@HardikThakkar 嘿,感谢您的迅速回复。我尝试在Swift 4.2中使用完全相同的代码,但我的动画没有改变。它仍然在侧面有很多填充。您有其他可供选择的方案吗? - Aakash Dave
侧边的填充取决于您使用的动画文件,您需要相应地进行更改。 - Hardik Thakkar
添加以下代码行。"animatedSwitch.contentMode = .scaleAspectFit" - Mwangi Gituathi
显示剩余3条评论

2
    animationView = .init(name: "lf30_editor_fip4qqkq")
    animationView!.frame = CGRect(x: 0, y: 0, width: 150, height: 150)
    animationView!.center = self.view.center
    animationView!.contentMode = .scaleAspectFit
    animationView!.loopMode = .loop
    animationView!.animationSpeed = 1.0
    view.addSubview(animationView!)
    animationView!.play()

现在只需调整宽度和高度设置即可... - Mwangi Gituathi

0
我也遇到了这个问题。我将动画视图的宽度和高度修改为所需大小,并将内容模式更改为缩放纵横比填充。如果您想要使动画更大,只需更新宽度和高度。以下是示例代码。
animationView.animation = Animation.named("loading")
animationView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
animationView.contentMode = .scaleAspectFill
animationView.loopMode = .loop
animationView.play()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接