如何在 iPhone 的“方向”中设置不同的图像视图框架大小?

发布于 2024-10-21 15:52:20 字数 2718 浏览 5 评论 0原文

我已经创建了图像视图并在该视图中设置图像,并将图像视图添加为滚动视图的子视图(如幻灯片放映图像)。所以我想动态设置图像视图框架大小和滚动视图框架大小。我想在纵向和横向模式下设置不同的图像视图框架大小和滚动视图大小。那么如何在横向和纵向模式下设置不同的帧大小。

这是我的示例代码,

- (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); // 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

}

请帮助我。

谢谢。

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); // 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深已缘浅 2024-10-28 15:52:20

嗨,

您可以注册通知中心以更改屏幕方向。

注册通知中心的代码。

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

实现 ScreenRotate 方法。

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

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];    

    if( orientation == UIDeviceOrientationLandscapeLeft)
    {

    }
    else if ( orientation == UIDeviceOrientationLandscapeRight )
    {

    }
    else if ( orientation == UIDeviceOrientationPortrait )
    {

    }    
 }

向通知中心注销的代码。

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

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



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

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;
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文