如何用导航控制器旋转视图?

时间:2011-07-09 09:46:30

标签: iphone uiview uinavigationcontroller uiscrollview uiimageview

@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

在上面的代码我有问题,当在模拟器上检查然后左右旋转设备。但观点不会改变,我想改变它。我怎么修改它?

1 个答案:

答案 0 :(得分:0)

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