简体   繁体   中英

How to rotate only one element in a UIView

I have a UIView which contains many items like UIScrollView.

How can I specify I want to rotate only one UIScrollView when I change the position of my device (or iPhone Simulator) ?

Thanks !

  • return NO from

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
  • handle the device rotation notification yourself:

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(didRotate:)
                                                 name:UIDeviceOrientationDidChangeNotification
                                               object:nil];
  • and rotate just the view you want:

self.theView.transform = CGAffineTransformIdentity;
self.theView.transform = CGAffineTransformMakeRotation(degreesToRadians(-90));
UIView *localView = [mainView viewWithTag:tagOfScrollViewToRotate];   
 UIInterfaceOrientation deviceOrientation = [UIApplicationsharedApplication].statusBarOrientation;

            float   angle;
            switch (deviceOrientation) {
                case UIInterfaceOrientationPortraitUpsideDown:
                {   
                    angle = M_PI;
                    CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
                    [localView setTransform:transform];

                    break;
                }
                case UIInterfaceOrientationLandscapeLeft:
                {
                    angle = -M_PI/2;
                    CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
                    [localView setTransform:transform];
                    localView.frame = CGRectMake(0, 0, 768, 1024);

                    break;
                }
                case UIInterfaceOrientationLandscapeRight:
                {
                    angle = M_PI/2;
                    CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
                    [localView setTransform:transform];
                    localView.frame = CGRectMake(0, 0, 768, 1024);
                    break;
                }

                default:
                {
                    angle = 0;
                    break;
                }
            }

Good luck, :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM