Как может subView обнаружить, что mainView вращается? - PullRequest
1 голос
/ 25 апреля 2010

У меня есть mainView. К этому представлению я добавляю представление того же размера. Когда основной вид (фон) вращается, его обнаруживают, но подпредставление не имеет никакого представления о том, чтобы его повернуть. И его функции даже не называются. Даже когда программа запускается тоже, если я нахожусь в ландшафтном режиме, то же самое.

Как я могу заставить подпросмотр знать, что устройство вращается?

Ответы [ 3 ]

1 голос
/ 20 января 2012

Я быстро расстроился из-за отсутствия поддержки уведомлений о ротации для неосновных 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!");
}
1 голос
/ 25 апреля 2010

Возможно, вы можете снять событие из основного вида в вспомогательный вид, например, так (в основном виде):

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    [subView didRotateFromInterfaceOrientation:fromInterfaceOrientation];
}
0 голосов
/ 25 апреля 2010

Вы можете запустить NSNotification при вращении основного вида, который подпрограмма зарегистрирована для прослушивания. Здесь краткий обзор NSNotification здесь .

Одним из преимуществ этого подхода является то, что объекты, отличные от подклассов UIView, могут прослушивать это уведомление.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...