Repository navigation
Expand file tree
/
Copy pathRiveMenuButtonDemoView.swift
More file actions
79 lines (71 loc) · 3.82 KB
/
Copy pathRiveMenuButtonDemoView.swift
File metadata and controls
79 lines (71 loc) · 3.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// RiveMenuButtonDemoView.swift
// RiveApp
//
// Copyright © 2026 https://github.com/ChenYilong . All rights reserved.
//
import UIKit
import CYLTabBarController
/*!
* [EN] A Rive animation in plain UIKit, outside the tab bar: `CYLTabBarItemAnimationFactory` makes the view from a
* `CYLTabBarAnimationURLWithOptions()` URL, and its player plays and stops it. The course's menu_button.riv has no view
* model, so the state machine input "isOpen" is used, held until it is stopped ("duration" 0). The Bell tab shows it
* above the demo's test list (`CYLSettingsDemoTableViewController.headerView`).
* [CN] 在 TabBar 之外、纯 UIKit 中使用 Rive 动画:`CYLTabBarItemAnimationFactory` 根据 `CYLTabBarAnimationURLWithOptions()` 生成的 URL 创建视图,
* 再通过其播放器播放与停止。课程的 menu_button.riv 没有 view model,因此使用状态机输入 "isOpen",保持到停止("duration" 为 0)。
* Bell Tab 把它显示在 Demo 测试列表上方(`CYLSettingsDemoTableViewController.headerView`)。
*/
final class RiveMenuButtonDemoView: UIView {
private static let animationSize = CGSize(width: 88, height: 88)
private var animationView: UIView?
private var isOn = false
override init(frame: CGRect) {
super.init(frame: frame)
// [EN] Starts at its final size: the animation view resizes with it (autoresizing), so a zero-sized container
// would make it grow by the container's size when laid out.
// [CN] 初始即为最终大小:动画视图随其缩放(autoresizing),容器若从零尺寸开始,布局后动画视图会额外增大容器的尺寸。
let container = UIView(frame: CGRect(origin: .zero, size: Self.animationSize))
container.translatesAutoresizingMaskIntoConstraints = false
let fileURL = Bundle.main.url(forResource: "menu_button", withExtension: "riv")
if let url = CYLTabBarAnimationURLWithOptions(fileURL, [
.riveStateMachine: "State Machine",
.riveInput: "isOpen",
.riveActiveDuration: "0",
] as [CYLTabBarAnimationOption: String]), let animationView = CYLTabBarItemAnimationFactory.animationView(url: url, size: Self.animationSize, contentMode: .scaleAspectFit) {
animationView.frame = container.bounds
animationView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
container.addSubview(animationView)
self.animationView = animationView
}
let button = UIButton(type: .system)
button.setTitle(cylDemoLocalizedString("播放 / 停止"), for: .normal)
button.titleLabel?.font = .preferredFont(forTextStyle: .headline)
button.addTarget(self, action: #selector(toggle), for: .touchUpInside)
let stack = UIStackView(arrangedSubviews: [container, button])
stack.translatesAutoresizingMaskIntoConstraints = false
stack.axis = .vertical
stack.alignment = .center
stack.spacing = 12
addSubview(stack)
NSLayoutConstraint.activate([
container.widthAnchor.constraint(equalToConstant: Self.animationSize.width),
container.heightAnchor.constraint(equalToConstant: Self.animationSize.height),
stack.centerXAnchor.constraint(equalTo: centerXAnchor),
stack.topAnchor.constraint(equalTo: topAnchor, constant: 16),
stack.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8),
])
}
@available(*, unavailable)
required init?(coder _: NSCoder) {
fatalError("init(coder:) is not supported")
}
@objc private func toggle() {
isOn.toggle()
let player = CYLTabBarItemAnimationFactory.player(for: animationView)
if isOn {
player?.play()
} else {
player?.stop()
}
}
}