1

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

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

4

3 回答 3

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!");
}
于 2012-01-20T04:00:42.087 回答
1

也许您可以将事件从 mainView 拍摄到 subView,如下所示(在 mainView 中):

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    [subView didRotateFromInterfaceOrientation:fromInterfaceOrientation];
}
于 2010-04-25T08:49:55.897 回答
0

您可以在主视图旋转时触发NSNotification,子视图已注册为监听。NSNotification 这里有一个快速概述。

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

于 2010-04-25T08:54:15.323 回答