Skip to content

Commit c9cf2f4

Browse files
committed
feat(ios): add rive view
1 parent 112a5e6 commit c9cf2f4

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

Rive.podspec

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@ Pod::Spec.new do |s|
1818
load 'nitrogen/generated/ios/Rive+autolinking.rb'
1919
add_nitrogen_files(s)
2020

21+
s.dependency "RiveRuntime", "6.8.1"
22+
2123
install_modules_dependencies(s)
2224
end

ios/HybridRiveView.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import Foundation
2+
import UIKit
3+
4+
class HybridRiveView : HybridRiveViewSpec {
5+
// MARK: View Props
6+
var autoBind: Bool = false {
7+
didSet {
8+
print("did set auto bind: \(autoBind)")
9+
}
10+
}
11+
12+
var autoPlay: Bool = false {
13+
didSet {
14+
print("did set auto play: \(autoPlay)")
15+
}
16+
}
17+
18+
// MARK: View Methods
19+
func play() {
20+
riveView?.play()
21+
}
22+
23+
func pause() {
24+
riveView?.pause()
25+
}
26+
27+
// MARK: View Initializers
28+
var riveView: RiveReactNativeView? {
29+
return view as? RiveReactNativeView
30+
}
31+
32+
var view: UIView = {
33+
let view = RiveReactNativeView()
34+
view.demoSetupRiveView()
35+
return view
36+
}()
37+
38+
// MARK: Internal
39+
func beforeUpdate() {
40+
print("before update")
41+
}
42+
43+
func afterUpdate() {
44+
print("after update")
45+
}
46+
}

ios/RiveReactNativeView.swift

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import UIKit
2+
import RiveRuntime
3+
4+
class RiveReactNativeView: UIView {
5+
private var riveView: RiveView?
6+
private var baseViewModel: RiveViewModel?
7+
8+
private let riveUrl = "https://cdn.rive.app/animations/vehicles.riv"
9+
10+
// MARK: - Public Methods
11+
func play() {
12+
baseViewModel?.play()
13+
}
14+
15+
func pause() {
16+
baseViewModel?.pause()
17+
}
18+
19+
public func demoSetupRiveView() {
20+
downloadRiveFile { [weak self] riveFile in
21+
guard let riveFile = riveFile else { return }
22+
let model = RiveModel(riveFile: riveFile)
23+
self?.baseViewModel = RiveViewModel(model)
24+
self?.riveView = self?.baseViewModel?.createRiveView()
25+
26+
if let riveView = self?.riveView {
27+
riveView.translatesAutoresizingMaskIntoConstraints = false
28+
self?.addSubview(riveView)
29+
NSLayoutConstraint.activate([
30+
riveView.leadingAnchor.constraint(equalTo: self!.leadingAnchor),
31+
riveView.trailingAnchor.constraint(equalTo: self!.trailingAnchor),
32+
riveView.topAnchor.constraint(equalTo: self!.topAnchor),
33+
riveView.bottomAnchor.constraint(equalTo: self!.bottomAnchor)
34+
])
35+
}
36+
}
37+
}
38+
39+
// MARK: - Internal
40+
private func downloadRiveFile(completion: @escaping (RiveFile?) -> Void) {
41+
guard let url = URL(string: riveUrl) else {
42+
completion(nil)
43+
return
44+
}
45+
DispatchQueue.global(qos: .background).async {
46+
do {
47+
let riveData = try Data(contentsOf: url)
48+
let riveFile = try RiveFile(data: riveData, loadCdn: true)
49+
DispatchQueue.main.async {
50+
completion(riveFile)
51+
}
52+
} catch {
53+
DispatchQueue.main.async {
54+
completion(nil)
55+
}
56+
}
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)