开发者

How to set the different image view frame size in Orientations in iPhone?

开发者 https://www.devze.com 2023-02-16 19:37 出处:网络
I have created the the image view and set the images in that view and added the image view as sub view of the scroll view(like slide show images). SO i want to set the image view frame size and scroll

I have created the the image view and set the images in that view and added the image view as sub view of the scroll view(like slide show images). SO i want to set the image view frame size and scroll view frame size as dynamically. I want to set the different image view frame size and the scroll view size in the portrait and landscape mode. So how can i set the different frame size in the landscape and portrait mode.

Here my sample code,

- (void)viewDidLoad {

    [super viewDidLoad];

        self.scroll = [[UIScrollView alloc] init];

        self.scroll.delegate = self;

        self.scroll.pagingEnabled = YES;

        self.scroll.contentMode = UIViewContentModeScaleAspectFit;

        self.scroll.autoresizingMask = ( UIViewAutoresizingFlexibleHeight |
                                                                        UIViewAutoresizingFlexibleWidth );

        [self.view addSubview:self.scroll];

         totalArray =   [[NSMutableArray alloc] initWithObjects:[UIImage imageNamed:@"dashboard_demo_2.png"],
                                   [UIImage imageNamed:@"dashboard_demo_1.png"],
                                   [UIImage imageNamed:@"dashboard_demo_2.png"],
                                   [UIImage imageNamed:@"3.jpg"],
                                   [UIImage imageNamed:@"4.jpg"],
                                   [UIImage imageNamed:@"12.jpg"],
                                   [UIImage imageNamed:@"5.jpg"],nil];
        x = 0;

        for(int i = 0; i< [totalArray count]; i++)
        {
                img = [[UIImageView alloc] initWithFrame:CGRectMake(x, 0, 320, 200)]; 

                img.image =[totalArray objectAtIndex:i];

                x = x+320; // in portait mode if the image sets correctly, but landscape mode it will x = x + 480;

                [self.scroll addSubview:img];

        }      
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

        UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

        if (orientation == UIDeviceOrientationPortrait ||
                orientation == UIDeviceOrientationPortraitUpsideDown )
        {
                [self portraitMode];
        }
        else if (orientation == UIDeviceOrientationLandscapeLeft ||
                         orientation == UIDeviceOrientationLandscapeRight )
        {
                [self LandscapeMode];

        }              

        return YES;
}

-(void) portraitMode
{

       //How to set the frame sizes.

        self.scroll.frame = CGRectMake(0, 0, 320, 200);

        img.frame = CGRectMake(x, 0, 320, 200);

        scroll.contentSize = CGSizeMake(x, 200开发者_StackOverflow中文版); // it doesn't set

}

-(void) LandscapeMode
{

        //How to set the frame sizes

         self.scroll.frame = CGRectMake(0, 0, 480, 320);

        img.frame = CGRectMake(x, 0, 480, 200);

        scroll.contentSize = CGSizeMake(x, 320); // it doesn't set

}

PLease help me out.

Thanks.


Hi ,

you can register to notification center for change in screen orientation.

Code to enroll with notification center.

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                  selector:@selector(ScreenRotate:) 
                                  name:UIDeviceOrientationDidChangeNotification
                                  object:nil];

Implement the ScreenRotate method.

-(void) screenRotate:(NSNotificationCenter*) notification
 {

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];    

    if( orientation == UIDeviceOrientationLandscapeLeft)
    {

    }
    else if ( orientation == UIDeviceOrientationLandscapeRight )
    {

    }
    else if ( orientation == UIDeviceOrientationPortrait )
    {

    }    
 }

Code to unregister with notification center.

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

    [[NSNotificationCenter defaultCenter] removeObserver:self 
                                                    name:UIDeviceOrientationDidChangeNotification
                             object:nil];



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return YES;
}

0

精彩评论

暂无评论...
验证码 换一张
取 消