【问题标题】:change orientation dynamically动态改变方向
【发布时间】:2011-07-02 06:21:22
【问题描述】:

我想动态更改我的视图布局。我如何在我的代码中定义?在我的代码中,我定义为

const CGFloat kScrollObjHeight    = 460.0;
const CGFloat kScrollObjWidth    = 320.0;
const NSUInteger kNumImages        = 32;

- (void)layoutScrollImages
{
    UIImageView *view = nil;
    NSArray *subviews = [scrollView1 subviews];

    // reposition all image subviews in a horizontal serial fashion
    CGFloat curXLoc = 0;
    for (view in subviews)
    {
        if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
        {
            CGRect frame = view.frame;
            frame.origin = CGPointMake(curXLoc, 0);
            view.frame = frame;

            curXLoc += (kScrollObjWidth);
        }
    }

    // set the content size so it can be scrollable
    [scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}

- (void)viewDidLoad
{
    self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];

    // 1. setup the scrollview for multiple images and add it to the view controller
    //
    // note: the following can be done in Interface Builder, but we show this in code for clarity
    [scrollView1 setBackgroundColor:[UIColor blackColor]];
    [scrollView1 setCanCancelContentTouches:NO];
    scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    scrollView1.clipsToBounds = YES;        // default is NO, we want to restrict drawing within our scrollview
    scrollView1.scrollEnabled = YES;

    //imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image0.jpg"]];
    [scrollView1 addSubview:imageView];
    [scrollView1 setContentSize:CGSizeMake(imageView.frame.size.width, imageView.frame.size.height)];
    scrollView1.minimumZoomScale = 1;
    scrollView1.maximumZoomScale = 3;
    scrollView1.delegate = self;
    [scrollView1 setScrollEnabled:YES];

    // pagingEnabled property default is NO, if set the scroller will stop or snap at each photo
    // if you want free-flowing scroll, don't set this property.
    scrollView1.pagingEnabled = YES;

    // load all the images from our bundle and add them to the scroll view
    NSUInteger i;
    for (i = 1; i <= kNumImages; i++)
    {
        NSString *imageName = [NSString stringWithFormat:@"page-%d.jpg", i];
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView *ImageView = [[UIImageView alloc] initWithImage:image];

        // setup each frame to a default height and width, it will be properly placed when we call "updateScrollList" 
        CGRect rect = ImageView.frame;
        rect.size.height = kScrollObjHeight;
        rect.size.width = kScrollObjWidth;
        ImageView.frame = rect;
        ImageView.tag = i;    // tag our images for later use when we place them in serial fashion
        [scrollView1 addSubview:ImageView];
        [ImageView release];
    }

    [self layoutScrollImages];    // now place the photos in serial layout within the scrollview


}

-(UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return imageView;
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    return YES;
}


- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {

    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];


    switch(toInterfaceOrientation){
        case (UIInterfaceOrientationLandscapeLeft):
        {
            landscapeModeViewController=[[LandscapeModeViewController alloc] initWithNibName:@"LandscapeModeViewController" bundle:nil];
            [self.view addSubview:landscapeModeViewController.view];
            /*UIAlertView *info = [[UIAlertView alloc] initWithTitle:@"Info" message:@"change Mode" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [info show];
            [info release];*/
        }
            break;
        case(UIInterfaceOrientationLandscapeRight):
        {
            //landscapeModeViewController=[[LandscapeModeViewController alloc] initWithNibName:@"LandscapeModeViewController" bundle:nil];
            //[self.view addSubview:landscapeModeViewController.view];
            UIAlertView *info = [[UIAlertView alloc] initWithTitle:@"Info" message:@"change Mode" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [info show];
            [info release];
        }
            break;
        case(UIInterfaceOrientationPortrait):
        {
            //portraitMode=[[PortraitMode alloc] initWithNibName:@"PortraitMode" bundle:nil];
            //[self.view addSubview:portraitMode.view];
            UIAlertView *info = [[UIAlertView alloc] initWithTitle:@"Info" message:@"change Mode" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [info show];
            [info release];

        }
            break;

            /*if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
             UIAlertView *info = [[UIAlertView alloc] initWithTitle:@"Info" message:@"Select Landscape mode." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
             [info show];
             [info release];
             [scrollView1 release];
             landscapeModeViewController=[[LandscapeModeViewController alloc] initWithNibName:@"LandscapeModeViewController" bundle:nil];
             [self.view addSubview:landscapeModeViewController.view];


             }


             else{
             UIAlertView *info = [[UIAlertView alloc] initWithTitle:@"Info" message:@"Select Portrait Mode" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
             [info show];
             [info release];

             portraitMode=[[PortraitMode alloc] initWithNibName:@"PortraitMode" bundle:nil];
             [self.scrollView1 addSubview:portraitMode.view];
             }  */  


    }
}
- (void)dealloc
{    
    [scrollView1 release];
    [imageView release];

    [super dealloc];
}

- (void)didReceiveMemoryWarning
{

    [super didReceiveMemoryWarning];
}

@end

在上面的代码中,当旋转设备然后 uialerview 执行但以前的视图没有释放和他后面显示的 landsacpe 模式。那么我会为此做些什么呢?

【问题讨论】:

    标签: iphone objective-c cocoa-touch uiviewcontroller


    【解决方案1】:

    您可以按照您想要的任何方式为布局中的每个视图设置自动调整大小掩码。

    例如,如果您希望按钮始终显示在左上角,您可以将自动调整大小蒙版设置为灵活的顶部和左侧..

    如果你的布局在纵向和横向上完全不同,那么你需要在 willRotateToInterfaceOrientation 中设置框架。

    例如,如果您希望您的按钮在左上角纵向和横向居中,则需要在上述委托方法中设置框架。

    编辑答案:

     if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation)){
        if(!landscapeModeViewController){
             landscapeModeViewController=[[LandscapeModeViewController alloc] initWithNibName:@"LandscapeModeViewController" bundle:nil];
        }
        [self.view addSubview:landscapeModeViewController.view];
        [self.view bringSubviewToFront:landscapeModeViewController.view];
     }
     else if(UIInterfaceOrientationIsPortrait(toInterfaceOrientation)){
         if(!portraitMode){
             portraitMode=[[PortraitMode alloc] initWithNibName:@"PortraitMode" bundle:nil];
         }
         [self.view addSubview:portraitMode.view];
         [self.view bringSubviewToFront:portraitMode.view];
     }
    

    【讨论】:

    • 感谢您的回答,但我尝试不需要我。我已经编辑了我的问题,所以请检查给出另一个解决方案。
    • 在上面的代码中(有问题)你能告诉我如何在滚动视图中放大和缩小图像。
    • 我只修改了你的部分代码,放大缩小没有问题吧?您在从纵向视图返回的横向视图中遇到问题,所以我已经做到了,它总是会出现在前面....
    • 我想在滚动视图中插入事件放大缩小。在我的代码中,我有代码,但它不起作用。所以请给出我如何在两种模式下在我的代码中应用该事件的解决方案。
    • 你能否提出新问题并添加一些代码,因为这个问题不在当前问题的范围内????
    猜你喜欢
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    相关资源
    最近更新 更多