iphone/ipad orientation handling

后端 未结 6 992
盖世英雄少女心
盖世英雄少女心 2020-11-30 21:52

This is more of a general question for people to provide me guidance on, basically Im learning iPad/iPhone development and have finally come across the multi-orientation sup

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 22:11

    I can't vouch for this code, and in all honesty the above willRotateToInterfaceOrientation works great. Here's another take on it with FBDialog.m from Facebook for iphone / ipad. (albeit, I think this was for a webview)

    here's the gist

    [[NSNotificationCenter defaultCenter] addObserver:self
      selector:@selector(deviceOrientationDidChange:)
      name:@"UIDeviceOrientationDidChangeNotification" object:nil];
    
    
    - (void)deviceOrientationDidChange:(void*)object {
      UIDeviceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
      if ([self shouldRotateToOrientation:orientation]) {
    
    
        CGFloat duration = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:duration];
        [self sizeToFitOrientation:YES];
        [UIView commitAnimations];
      }
    }
    
    
    -(CGAffineTransform)transformForOrientation {
      UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
      if (orientation == UIInterfaceOrientationLandscapeLeft) {
        return CGAffineTransformMakeRotation(M_PI*1.5);
      } else if (orientation == UIInterfaceOrientationLandscapeRight) {
        return CGAffineTransformMakeRotation(M_PI/2);
      } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
        return CGAffineTransformMakeRotation(-M_PI);
      } else {
        return CGAffineTransformIdentity;
      }
    }
    
    - (void)sizeToFitOrientation:(BOOL)transform {
      if (transform) {
        self.transform = CGAffineTransformIdentity;
      }
    
      CGRect frame = [UIScreen mainScreen].applicationFrame;
      CGPoint center = CGPointMake(
        frame.origin.x + ceil(frame.size.width/2),
        frame.origin.y + ceil(frame.size.height/2));
    
      CGFloat scale_factor = 1.0f;
      if (FBIsDeviceIPad()) {
        // On the iPad the dialog's dimensions should only be 60% of the screen's
        scale_factor = 0.6f;
      }
    
      CGFloat width = floor(scale_factor * frame.size.width) - kPadding * 2;
      CGFloat height = floor(scale_factor * frame.size.height) - kPadding * 2;
    
      _orientation = [UIApplication sharedApplication].statusBarOrientation;
      if (UIInterfaceOrientationIsLandscape(_orientation)) {
        self.frame = CGRectMake(kPadding, kPadding, height, width);
      } else {
        self.frame = CGRectMake(kPadding, kPadding, width, height);
      }
      self.center = center;
    
      if (transform) {
        self.transform = [self transformForOrientation];
      }
    }
    

提交回复
热议问题