9

私はUIViewプログラムで作成されているを持っています:

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.5];

        UIImageView* closeButton = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"modal_close.png"]];

        closeButton.frame = CGRectMake(self.frame.size.width*.89, self.frame.size.height*.09, closeButton.frame.size.width, closeButton.frame.size.height);

        UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(self.frame.size.width*.89, self.frame.size.height*.09,20, 20)];

        [button addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];

        UIView* mainView = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width*.1,self.frame.size.height*.1,self.frame.size.width*.8,self.frame.size.height*.8)];

        mainView.backgroundColor = [UIColor whiteColor];
        _displayMainView = mainView;        

        [self addSubview:_displayMainView];
        [self addSubview:closeButton];
        [self addSubview:button];

        mainView = nil;
        closeButton = nil;
    }
    return self;
}

回転を検出するにはどうすればよいですか? これは、別の既存のビューの上でモーダルとして機能することです。それが重要な場合、これは iPad のみのアプリです。

4

3 に答える 3

42

デバイスに回転通知の生成を開始させることができます:

  [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

デバイス ローテーション通知のオブザーバーとして独自のセレクターを追加します。

      [[NSNotificationCenter defaultCenter] addObserver: self selector:   @selector(deviceOrientationDidChange:) name: UIDeviceOrientationDidChangeNotification object: nil];

そして、通知ハンドラを書きます

- (void)deviceOrientationDidChange:(NSNotification *)notification {
     //Obtain current device orientation
     UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

      //Do my thing
 }

カスタム UIView の dealloc メソッドが呼び出されたとき (または完了したとき) にオブザーバーを削除し、デバイス変更通知の生成も停止することを忘れないでください。

-(void) dealloc{
[[NSNotificationCenter defaultCenter] removeObserver: self];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
}

これはあなたの質問にまったく役立ちますか?

于 2012-08-26T06:26:51.807 に答える
6

彼のアプローチは私が最終的に使用したものであるため、Swift 3に対する@ clearwater82の回答を書き直しています。

ビューのメソッドに次のコードを追加しましたinit(コントローラーの場合、そのviewDidLoadメソッドに含まれます)。

// Handle rotation
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
NotificationCenter.default.addObserver(
    self,
    selector: #selector(self.orientationChanged(notification:)),
    name: NSNotification.Name.UIDeviceOrientationDidChange,
    object: nil
)


orientationChanged次に、メソッドをビューに追加しました。このメソッドの名前は自由に変更できますが、上記のコード スニペットでも名前を変更することを忘れないでください。

// Called when device orientation changes
@objc func orientationChanged(notification: Notification) {
    // handle rotation here
}


最後にdeinit、ビューが削除されたときに不要な通知を削除するメソッドを追加しました。

deinit {
    NotificationCenter.default.removeObserver(self)
    UIDevice.current.endGeneratingDeviceOrientationNotifications()
}

この最後のスニペットが行うことは、通知オブザーバーからビュー自体を削除し、デバイスが他の回転通知を生成しないようにすることです。このアプローチは私が必要としていたものでしたが、ビューが削除されたときに通知が生成されるのを止めたくない場合があります。

これが役に立つことを願っています、乾杯

于 2017-03-15T08:59:01.420 に答える
4

次の 2 つのオプションがあると思います。

  1. ビューをカスタムに配置しUIViewController、メソッドをオーバーライドして、shouldAutorotateToInterfaceOrientation:サポートするすべての方向に対して YES を返します。ビュー コントローラーは、コンテンツ (ビュー) を自動的に回転およびサイズ変更します。正しい autoresizeMask (または autolayout を使用している場合は制約) があることを確認する必要があります。
  2. 加速度計を使用して、デバイスの向きの変化を直接観察します。その後、必要に応じてビューを調整できます。

可能であれば、これが実際に最初に選択すべきアプローチです。

于 2012-08-25T22:26:56.913 に答える