我有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
}
答案 0 :(得分: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
的{{1}}方法中,您可以添加观察者:
viewDidLoad
答案 1 :(得分: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];
禁用当前的自动播放功能。只需在收到通知时播放您的视频。仅在您选择细分时才发布通知。