简体   繁体   English

在设备iOS旋转时发送动作

[英]Send Action on rotation of device iOS

here is what I'm trying to achieve, it would be to send an action on rotation of the device... 这是我要实现的目标,它是发送有关设备旋转的操作...

the point is i'm having a 360 player, I would like the video to start only once the device is on landscape. 关键是我有一个360播放器,我希望仅在设备处于横向状态时才能开始播放视频。 If it's in portrait i'd like to display a message a saying "rotate your device in landscape to start", and when the user rotate the device, the video play. 如果是纵向模式,我想显示一条消息,说“横向旋转设备以开始播放”,当用户旋转设备时,将播放视频。

below is my code so far: 下面是我到目前为止的代码:

- (IBAction)test:(id)sender forEvent:(UIEvent *)event {

if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation ]== UIDeviceOrientationLandscapeRight)
{
        NSString *path = [[NSBundle mainBundle] pathForResource:@"demo1" ofType:@"mp4"];
        NSURL *url = [NSURL fileURLWithPath:path];

        Video360ViewController *videoController = [[Video360ViewController alloc] initWithNibName:@"HTY360PlayerVC" bundle:nil url:url];

        [videoController VRMode:true];

        if (![[self presentedViewController] isBeingDismissed]) {
            [self presentViewController:videoController animated:YES completion:nil];
        }
}
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown )
{
    NSLog(@"UIDeviceOrientationPortrait");
}

}

The problem of this code is than it trigger only on TouchUp Inside in the Sent Event . 此代码的问题是,它仅在Sent Event中的TouchUp Inside上触发。 . . anyway to have the action starting on rotation ? 无论如何要开始旋转动作?

Thanks for all your help ! 感谢你的帮助 !

EDIT--- 编辑 - -

TRYIED THE FOLLOWING ASWELL: 还尝试了以下内容:

- (IBAction)test:(id)sender forEvent:(UIEvent *)event {
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

if ([[UIDevice currentDevice] orientation] == UIDeviceOrientationLandscapeLeft || [[UIDevice currentDevice] orientation ]== UIDeviceOrientationLandscapeRight)
{
        NSString *path = [[NSBundle mainBundle] pathForResource:@"demo1" ofType:@"mp4"];
        NSURL *url = [NSURL fileURLWithPath:path];

        Video360ViewController *videoController = [[Video360ViewController alloc] initWithNibName:@"HTY360PlayerVC" bundle:nil url:url];

        [videoController VRMode:true];

        if (![[self presentedViewController] isBeingDismissed]) {
            [self presentViewController:videoController animated:YES completion:nil];
        }
}
if([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown )
{
    NSLog(@"UIDeviceOrientationPortrait");
}

}

你有没有尝试过

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation

Try put your code inside: 尝试将您的代码放入其中:

- (void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{

}

and write this methods in your UIViewController . 并在UIViewController编写此方法。 This method is called when the device (interface really) is rotated; 旋转设备(实际上是接口)时将调用此方法。 if you want do something before the rotation of interface, use willRotateFromInterfaceOrientation 如果要在旋转接口之前执行某些操作,请使用willRotateFromInterfaceOrientation

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM