xcode SwiftUI:从结构体到类获取Int的值

5fjcxozz  于 2023-06-07  发布在  Swift
关注(0)|答案(1)|浏览(126)

我将selectedIndexInt的值从ContentView结构体获取到结构体中的LoopingPlayerUIView类时遇到问题,selectedIndex用于tabvview,我想在类中获取它的值以播放或暂停视频
下面是一个脚本的剪切:

struct ContentView: View {
//...
    @State public var selectedIndex: Int = 0

var body: some View {
    TabView(selection: $selectedIndex) {
        //script...
    }
//...
class LoopingPlayerUIView: UIView {
    let myView = ContentView()
    
    if myView.selectedIndex == 0{
        player.play()
    }
    else {
        player.pause()
        print("pause")
    }
}
//...

我尝试通过将myView定义为Contentview(),并通过myView.selectedIndex获取selectedIndex,因为我在其他论坛看到了,但没有成功。
谁能帮我在类中获取selectedIndex?
编辑:以下是完整的类LoopingPlayerUiView:

class LoopingPlayerUIView: UIView {
private let playerLayer = AVPlayerLayer()
private var playerLooper: AVPlayerLooper?

myView=ContentView()

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override init(frame: CGRect) {
    super.init(frame: frame)
    
    // Load the resource
    
    let storage = Storage.storage()
    var storageRef = storage.reference().child("User1/pexels-christopher-schultz-5147455-1080x1920-30fps (1).mp4")
    storageRef.downloadURL { [self] url, error in
        if let error = error {
            print(error)
            
            let fileurl = URL(string: "https://i.imgur.com/U52l05s.mp4")!
            let asset = AVAsset(url: fileurl)
            let item = CachingPlayerItem(asset: asset)
            
            // Setup the player
            let player = AVQueuePlayer()
            self.playerLayer.player = player
            self.playerLayer.videoGravity = .resizeAspectFill
            layer.addSublayer(self.playerLayer)
            
            // Create a new player looper with the queue player and template item
            playerLooper = AVPlayerLooper(player: player, templateItem: item)
            
            if myView.selectedIndex == 0 && myView.selecfixer == 0{
                // Start the movie
                player.play()
            }
            else {
                player.pause()
            }
        } else {
            print(url)
            
            let asset = AVAsset(url: url!)
            let item = AVPlayerItem(asset: asset)
            
            // Setup the player
            let player = AVQueuePlayer()
            playerLayer.player = player
            playerLayer.videoGravity = .resizeAspectFill
            layer.addSublayer(playerLayer)
            
            // Create a new player looper with the queue player and template item
            playerLooper = AVPlayerLooper(player: player, templateItem: item)
            
                        if selectedIndex == 0 {
                            player.play()
                        } else {
                            player.pause()
                            print("pause")
                        }
        }
    }
}

override func layoutSubviews() {
    super.layoutSubviews()
    playerLayer.frame = bounds
}

}

db2dz4w8

db2dz4w81#

SwiftUI是这样工作的:

  • 视图模型是一个类,一个ObservableObject
  • 视图是一个结构体,它创建视图模型的一个示例来读写它的变量

在阅读答案的其余部分之前,快速检查一下:LoopingPlayerUIView真的是UIKit视图吗?

  • 如果是的话,就忘了这个答案吧。
  • 如果它只是一个视图模型,请看下面这里。

以下是如何更好地构建代码的方法:

struct ContentView: View {
    // Create an instance of the view-model
    @StateObject private var viewModel = LoopingPlayer()

    // A @State variable should be private
    @State private var selectedIndex = 0

var body: some View {
    TabView(selection: $selectedIndex) {
        //script...
    }

    // Access the view-model method every time the local variable changes
    .onChange(of: selectedIndex) { value in
        viewModel.playOrPause(value)
    }
}

// The View-model is not a view - it usually is an ObservableObject,
// although here it doesn't look like you actually need it
class LoopingPlayer: ObservableObject {

    func playOrPause(_ index: Int) {
        if index == 0 {
            player.play()
        } else {
            player.pause()
            print("pause")
        }
    }
//...
}

相关问题