Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 子视图如何检测主视图正在旋转?_Iphone_Uiviewcontroller_Orientation - Fatal编程技术网

Iphone 子视图如何检测主视图正在旋转?

Iphone 子视图如何检测主视图正在旋转?,iphone,uiviewcontroller,orientation,Iphone,Uiviewcontroller,Orientation,我有一个主视图。在此视图中,我添加了一个相同大小的视图。当主视图(背景)旋转时,它会被检测到,但子视图对旋转没有任何概念。它的函数甚至没有被调用。即使程序也启动了,如果我处于横向模式,也是一样的 如何使子视图知道设备正在旋转?也许您可以将事件从主视图拍摄到子视图,就像这样(在主视图中): 旋转主视图时,您可以触发NSNotification,子视图将注册以侦听该主视图。这里有NSNotification的快速概述 这种方法的一个优点是,除了UIView的子类之外的对象可以侦听此通知。我很快就对非

我有一个主视图。在此视图中,我添加了一个相同大小的视图。当主视图(背景)旋转时,它会被检测到,但子视图对旋转没有任何概念。它的函数甚至没有被调用。即使程序也启动了,如果我处于横向模式,也是一样的


如何使子视图知道设备正在旋转?

也许您可以将事件从主视图拍摄到子视图,就像这样(在主视图中):


旋转主视图时,您可以触发
NSNotification
,子视图将注册以侦听该主视图。这里有
NSNotification
的快速概述


这种方法的一个优点是,除了
UIView
的子类之外的对象可以侦听此通知。

我很快就对非主
UIViewController
实例缺少旋转通知支持感到沮丧

因此,我将自己烘焙成一个
UIViewController
扩展。请注意,这纯粹是为了在子视图中检测旋转,它不会旋转子视图-我现在正在处理该部分

下面是源代码的用法示例

// Released under license GPLv3.
// Copyright (c) 2012 N David Brown. All Rights Reserved.
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

// Note: 'shouldAutorotateToInterfaceOrientation:' is automatically called by
//       'willRotate..', 'didRotate..' method calling notification handler
//       blocks, so typically will not be desired for notification.
#define NOTIFY_SHOULD_AUTOROTATE 0
@interface UIViewController (NDBExtensions)
    // For dispatchers.
#if NOTIFY_SHOULD_AUTOROTATE
    -(void)notifyShouldAutorotate:(UIInterfaceOrientation)toInterfaceOrientation;
#endif
    -(void)notifyWillRotate:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
    -(void)notifyDidRotate:(UIInterfaceOrientation)fromInterfaceOrientation;
    // For listeners.
#if NOTIFY_SHOULD_AUTOROTATE
    -(void)listenForShouldAutorotate;
#endif
    -(void)listenForWillRotate;
    -(void)listenForDidRotate;
    -(void)listenForAnyRotate;
    -(void)stopListeningForAnyRotate;
    @end

@implementation UIViewController (NDBExtensions)

#if NOTIFY_SHOULD_AUTOROTATE
    -(void)notifyShouldAutorotate:(UIInterfaceOrientation)toInterfaceOrientation {
        NSString *name = @"shouldAutorotate";
        NSString *key = @"toInterfaceOrientation";
        NSNumber *val = [NSNumber numberWithInt:toInterfaceOrientation];
        NSDictionary *info = [NSDictionary dictionaryWithObject:val forKey:key];
        [[NSNotificationCenter defaultCenter] postNotificationName:name object:nil userInfo:info];
    }
#endif

-(void)notifyWillRotate:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    NSString *name = @"willRotate";
    NSString *key = @"toInterfaceOrientation";
    NSNumber *val = [NSNumber numberWithInt:toInterfaceOrientation];
    NSString *key2 = @"duration";
    NSNumber *val2 = [NSNumber numberWithDouble:duration];
    NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:val,key,val2,key2,nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:name object:nil userInfo:info];
}

-(void)notifyDidRotate:(UIInterfaceOrientation)fromInterfaceOrientation {
    NSString *name = @"didRotate";
    NSString *key = @"fromInterfaceOrientation";
    NSNumber *val = [NSNumber numberWithInt:fromInterfaceOrientation];
    NSDictionary *info = [NSDictionary dictionaryWithObject:val forKey:key];
    [[NSNotificationCenter defaultCenter] postNotificationName:name object:nil userInfo:info];
}

#if NOTIFY_SHOULD_AUTOROTATE
-(void)listenForShouldAutorotate {
    [[NSNotificationCenter defaultCenter]
        addObserverForName:@"shouldAutorotate"
        object:nil queue:nil
        usingBlock:^(NSNotification* notification){
            NSNumber *val = [[notification userInfo] objectForKey:@"toInterfaceOrientation"];
            UIInterfaceOrientation toInterfaceOrientation = (UIInterfaceOrientation)[val intValue];
            [self shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
        }];
}
#endif

-(void)listenForWillRotate {
    [[NSNotificationCenter defaultCenter]
        addObserverForName:@"willRotate"
        object:nil queue:nil
        usingBlock:^(NSNotification* notification){
            NSNumber *val = [[notification userInfo] objectForKey:@"toInterfaceOrientation"];
            UIInterfaceOrientation toInterfaceOrientation = (UIInterfaceOrientation)[val intValue];
            NSNumber *val2 = [[notification userInfo] objectForKey:@"duration"];
            NSTimeInterval duration = [val2 doubleValue];
            if ([self shouldAutorotateToInterfaceOrientation:toInterfaceOrientation]) {
                [self willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
            }
        }];
}

-(void)listenForDidRotate {
    [[NSNotificationCenter defaultCenter]
        addObserverForName:@"didRotate"
        object:nil queue:nil
        usingBlock:^(NSNotification* notification){
            NSNumber *val = [[notification userInfo] objectForKey:@"fromInterfaceOrientation"];
            UIInterfaceOrientation fromInterfaceOrientation
                = (UIInterfaceOrientation)[val intValue];
            UIInterfaceOrientation toInterfaceOrientation
                = (UIInterfaceOrientation)[[UIDevice currentDevice] orientation];
            if ([self shouldAutorotateToInterfaceOrientation:toInterfaceOrientation]) {
                [self didRotateFromInterfaceOrientation:fromInterfaceOrientation];
            }
        }];
}

-(void)listenForAnyRotate {
#if NOTIFY_SHOULD_AUTOROTATE
    [self listenForShouldAutorotate];
#endif
    [self listenForWillRotate];
    [self listenForDidRotate];
}

-(void)stopListeningForAnyRotate {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"shouldAutorotate" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"willRotate" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"didRotate" object:nil];
}
@end

我想到了这一点,但没有想到可能会有更好的解决方案。
// Released under license GPLv3.
// Copyright (c) 2012 N David Brown. All Rights Reserved.
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

// Note: 'shouldAutorotateToInterfaceOrientation:' is automatically called by
//       'willRotate..', 'didRotate..' method calling notification handler
//       blocks, so typically will not be desired for notification.
#define NOTIFY_SHOULD_AUTOROTATE 0
@interface UIViewController (NDBExtensions)
    // For dispatchers.
#if NOTIFY_SHOULD_AUTOROTATE
    -(void)notifyShouldAutorotate:(UIInterfaceOrientation)toInterfaceOrientation;
#endif
    -(void)notifyWillRotate:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
    -(void)notifyDidRotate:(UIInterfaceOrientation)fromInterfaceOrientation;
    // For listeners.
#if NOTIFY_SHOULD_AUTOROTATE
    -(void)listenForShouldAutorotate;
#endif
    -(void)listenForWillRotate;
    -(void)listenForDidRotate;
    -(void)listenForAnyRotate;
    -(void)stopListeningForAnyRotate;
    @end

@implementation UIViewController (NDBExtensions)

#if NOTIFY_SHOULD_AUTOROTATE
    -(void)notifyShouldAutorotate:(UIInterfaceOrientation)toInterfaceOrientation {
        NSString *name = @"shouldAutorotate";
        NSString *key = @"toInterfaceOrientation";
        NSNumber *val = [NSNumber numberWithInt:toInterfaceOrientation];
        NSDictionary *info = [NSDictionary dictionaryWithObject:val forKey:key];
        [[NSNotificationCenter defaultCenter] postNotificationName:name object:nil userInfo:info];
    }
#endif

-(void)notifyWillRotate:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    NSString *name = @"willRotate";
    NSString *key = @"toInterfaceOrientation";
    NSNumber *val = [NSNumber numberWithInt:toInterfaceOrientation];
    NSString *key2 = @"duration";
    NSNumber *val2 = [NSNumber numberWithDouble:duration];
    NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:val,key,val2,key2,nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:name object:nil userInfo:info];
}

-(void)notifyDidRotate:(UIInterfaceOrientation)fromInterfaceOrientation {
    NSString *name = @"didRotate";
    NSString *key = @"fromInterfaceOrientation";
    NSNumber *val = [NSNumber numberWithInt:fromInterfaceOrientation];
    NSDictionary *info = [NSDictionary dictionaryWithObject:val forKey:key];
    [[NSNotificationCenter defaultCenter] postNotificationName:name object:nil userInfo:info];
}

#if NOTIFY_SHOULD_AUTOROTATE
-(void)listenForShouldAutorotate {
    [[NSNotificationCenter defaultCenter]
        addObserverForName:@"shouldAutorotate"
        object:nil queue:nil
        usingBlock:^(NSNotification* notification){
            NSNumber *val = [[notification userInfo] objectForKey:@"toInterfaceOrientation"];
            UIInterfaceOrientation toInterfaceOrientation = (UIInterfaceOrientation)[val intValue];
            [self shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
        }];
}
#endif

-(void)listenForWillRotate {
    [[NSNotificationCenter defaultCenter]
        addObserverForName:@"willRotate"
        object:nil queue:nil
        usingBlock:^(NSNotification* notification){
            NSNumber *val = [[notification userInfo] objectForKey:@"toInterfaceOrientation"];
            UIInterfaceOrientation toInterfaceOrientation = (UIInterfaceOrientation)[val intValue];
            NSNumber *val2 = [[notification userInfo] objectForKey:@"duration"];
            NSTimeInterval duration = [val2 doubleValue];
            if ([self shouldAutorotateToInterfaceOrientation:toInterfaceOrientation]) {
                [self willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
            }
        }];
}

-(void)listenForDidRotate {
    [[NSNotificationCenter defaultCenter]
        addObserverForName:@"didRotate"
        object:nil queue:nil
        usingBlock:^(NSNotification* notification){
            NSNumber *val = [[notification userInfo] objectForKey:@"fromInterfaceOrientation"];
            UIInterfaceOrientation fromInterfaceOrientation
                = (UIInterfaceOrientation)[val intValue];
            UIInterfaceOrientation toInterfaceOrientation
                = (UIInterfaceOrientation)[[UIDevice currentDevice] orientation];
            if ([self shouldAutorotateToInterfaceOrientation:toInterfaceOrientation]) {
                [self didRotateFromInterfaceOrientation:fromInterfaceOrientation];
            }
        }];
}

-(void)listenForAnyRotate {
#if NOTIFY_SHOULD_AUTOROTATE
    [self listenForShouldAutorotate];
#endif
    [self listenForWillRotate];
    [self listenForDidRotate];
}

-(void)stopListeningForAnyRotate {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"shouldAutorotate" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"willRotate" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"didRotate" object:nil];
}
@end
// In PrimaryViewController.h (instance of this contains 'view'
// which is first subview in window).

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    // Normal rules go here.
    return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
}

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration {
    // Normal rules go here.
    // ..and notification dispatch:
    [self notifyWillRotate:toInterfaceOrientation duration:duration];
}

-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    // Normal rules go here.
    // ..and notification dispatch:
    [self notifyDidRotate:fromInterfaceOrientation];
}


// In OtherViewController.h (this could be any non-primary view controller).

-(void)viewDidLoad {
    [self listenForAnyRotate];
}

-(void)viewDidUnload {
    [self stopListeningForAnyRotate];
}

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    // Normal rules go here.
    return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);
}

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration {
    // Normal rules go here.
    NSLog(@"#willRotate received!");
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    // Normal rules go here.
    NSLog(@"#didRotate received!");
}