subView如何检测到mainView正在旋转?

时间:2010-04-25 08:39:47

标签: iphone uiviewcontroller orientation

我有一个mainView。对于这个视图,我添加了相同大小的视图。当mainView(背景)旋转时,它被检测到,但是子视图对旋转没有任何想法。它的功能甚至没有被调用。即使程序也启动,如果我处于横向模式,它的方式也一样。

如何让subView知道设备正在旋转?

3 个答案:

答案 0 :(得分:1)

也许你可以从mainView到subView拍摄一个事件,就像这样(在mainView中):

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    [subView didRotateFromInterfaceOrientation:fromInterfaceOrientation];
}

答案 1 :(得分:1)

由于缺少非主要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

使用示例:

// 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!");
}

答案 2 :(得分:0)

当主视图旋转时,您可以触发NSNotification,子视图将被注册为侦听。有NSNotification over here的快速概述。

此方法的一个优点是UIView的子类以外的对象可以侦听此通知。