0

我有一个HMSegmentedControl用于在段之间切换。我使用容器视图在这些片段之间切换,效果很好,但是我的一个标签有一个在viewDidAppear. 所以我的问题是,当容器视图加载之前的所有内容并根据 显示视图时isHidden = false,即使未选择该片段,我的视频也会开始播放。我该如何处理这种情况?

这是我的segmentedControlValueChanged事件代码

print("selected index \(segmentedControl.selectedSegmentIndex)")

    switch segmentedControl.selectedSegmentIndex {
    case 0:
        liveContainer.isHidden = true
    case 1:
        liveContainer.isHidden = true
    case 2:
        liveContainer.isHidden = false
    default:
        break
    }
4

2 回答 2

2

您可以NSNotificationCenter在显示/隐藏视频以播放/停止视频时向包含视频的视图控制器发送通知。这可以在容器视图段选择中完成。因此,您可以从中删除自动播放viewDidAppear并将其添加到发送通知时调用的方法中。

例如,在您的segmentedControlValueChanged活动中,您可以编写:

switch segmentedControl.selectedSegmentIndex {
case 0:
    liveContainer.isHidden = true
    NotificationCenter.default.post(name: Notification.Name("StopVideo"), object: nil)
case 1:
    liveContainer.isHidden = true
    NotificationCenter.default.post(name: Notification.Name("StopVideo"), object: nil)
case 2:
    liveContainer.isHidden = false
    NotificationCenter.default.post(name: Notification.Name("PlayVideo"), object: nil)
default:
    break
}

在您的视频ViewController中,您可以有两种方法:一种用于播放视频:

func playVideo() {
  //play video here
}

另一个用于阻止它:

func stopVideo() {
  //stop video here
}

ViewController在您的videoviewDidLoad方法中,您可以添加观察者:

NotificationCenter.default.addObserver(self, selector: #selector(playVideo), name: "PlayVideo", object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(stopVideo), name: "StopVideo", object: nil)
于 2017-01-04T15:42:21.823 回答
0

试试NSNotificationCenter

接收文件时:

-viewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNotification:) name:@"NOTIFICATIONID" object:nil];

并创建方法来接收通知。

-(void)receiveNotification:(NSNotification *) notification{
    if ([notification.name isEqualToString:@"NOTIFICATIONID"])
    {
        NSDictionary* userInfo = notification.userInfo;
        NSNumber* segmentID = (NSNumber*)userInfo[@"segmentID"];
        NSLog (@"Successfully received test notification! %i", segmentID.intValue);
    }
}

要发布通知:

NSDictionary* userInfo = @{@"segmentID": @(segmentID)}; //Used to pass Objects to Notification
[[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATIONID" object:self userInfo:userInfo];

禁用您当前的自动播放。当您收到通知时,只需播放您的视频。仅在您选择细分时发布通知。

于 2017-01-04T14:13:50.550 回答