如何用导航控件旋转视图


@implementation MainViewController
@synthesize scrollView,imageView;
    - (id) init { 

    if (self != nil) {
        self.title = @"Evolution";
    }
    return self;}
    - (void)viewDidLoad {
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDetected:)];
    tapGesture.numberOfTapsRequired = 1;
    tapGesture.numberOfTouchesRequired = 1;

    scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    int numberOfImages = 32;
    CGFloat currentX = 0.0f;
    for (int i=1; i <= numberOfImages; i++) {
        // create image
        NSString *imageName = [NSString stringWithFormat:@"page-%d.jpg", i];
        UIImage *image = [UIImage imageNamed:imageName];
        imageView = [[UIImageView alloc] initWithImage:image];
        // put image on correct position
        CGRect rect = imageView.frame;
        rect.origin.x = currentX;
        imageView.frame = rect;
        // update currentX
        currentX +=454; //mageView.frame.size.width;
        [scrollView addSubview:imageView];
        [imageView release];
    }
    [scrollView addGestureRecognizer:tapGesture];
    scrollView.contentSize = CGSizeMake(currentX, 800);
    scrollView.pagingEnabled=YES;
    scrollView.userInteractionEnabled = YES;
    scrollView.maximumZoomScale = 15;
    scrollView.minimumZoomScale = 0.5;
    scrollView.bounces = NO;
    scrollView.bouncesZoom = NO;
    scrollView.delegate = self;
    scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    [self.view addSubview:scrollView];
    [scrollView release];
    [super viewDidLoad];}
     - (BOOL)shouldAutorotateToInterfaceOrientation(UIInterfaceOrientation)interfaceOrientation{
    return YES;}
    -(void)tapDetected:(UIGestureRecognizer*)recognizer{
        ImageViewController *settings = [[ImageViewController alloc] init];
    [self.navigationController pushViewController:settings animated:YES];
    [settings release];}
@end
@implementation ImgScrollViewAppDelegate
    @synthesize window;@synthesize viewController;
    - (void)applicationDidFinishLaunching:(UIApplication *)application{
    LogMethod();
    // If you want the status bar to be hidden at launch use this:
    // application.statusBarHidden = YES;
    //
    // To set the status bar as black, use the following:
    // application.statusBarStyle = UIStatusBarStyleBlackOpaque;

    // Create window
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // this helps in debugging, so that you know "exactly" where your views are placed;
    // if you see "red", you are looking at the bare window, otherwise use black
    // window.backgroundColor = [UIColor redColor];
    viewController = [ [ MainViewController alloc ] init ];
    navigationController = [ [ UINavigationController alloc ] initWithRootViewController: viewController ];
    /* Anchor the view to the window */
    [window addSubview:[navigationController view]];
    /* Make the window key and visible */
    [window makeKeyAndVisible];
} 
@end
在上面的代码中,我有一个问题,那就是当在模拟器上检查它时,然后向左和向右旋转设备。但是视图没有改变,我想改变它。怎么修改呢?

在你的视图控制器实现中有一个方法- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation。如果你想让它旋转到任何方向,让它返回YES。如果您只想让它自动旋转到某些方向,请添加If语句来检查并返回yes或no。

最新更新