Iphone 的定位?

发布于 2024-12-04 18:10:45 字数 1477 浏览 0 评论 0原文

我正在编写以下代码

- (void)viewDidLoad 
{ 
    [super viewDidLoad];
    [self checkOrientation];
}

-(void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
    [super viewWillDisappear:animated];
}

- (void)receivedRotate:(NSNotification *)notification
{    
    [self checkOrientation];
}  

-(void)checkOrientation
{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    if (orientation == UIDeviceOrientationLandscapeLeft||orientation==UIDeviceOrientationLandscapeRight)
    {
        NSLog(@"Right and Left");
        self.view.frame = CGRectMake(0, 0, 480, 320);
        // Set x coorinate of views you want to change
    }
    else
    {
        NSLog(@"Up or Down");
        // Set x coordinates of views to initial x xoordinates.
    }
}        

这条线

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

不起作用,如果我像

self.view.superview.frame = CGRectMake(-50, -70, 800, 900);

这样写它正在改变轴,但我不知道它的行为方式,

我想改变视图矩形,它的改变会改变方向,我该怎么做?

I am writing the following code

- (void)viewDidLoad 
{ 
    [super viewDidLoad];
    [self checkOrientation];
}

-(void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
    [super viewWillDisappear:animated];
}

- (void)receivedRotate:(NSNotification *)notification
{    
    [self checkOrientation];
}  

-(void)checkOrientation
{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    if (orientation == UIDeviceOrientationLandscapeLeft||orientation==UIDeviceOrientationLandscapeRight)
    {
        NSLog(@"Right and Left");
        self.view.frame = CGRectMake(0, 0, 480, 320);
        // Set x coorinate of views you want to change
    }
    else
    {
        NSLog(@"Up or Down");
        // Set x coordinates of views to initial x xoordinates.
    }
}        

This line

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

is not working, and If I will write like

self.view.superview.frame = CGRectMake(-50, -70, 800, 900);

then it is changing the axis, but I don't know how it is behaving,

I want to change the view rect change with it will change the orientations, how can I do that?

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

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

发布评论

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

评论(2

彩扇题诗 2024-12-11 18:10:45

为什么要使用通知进行轮换。您可以使用其中任何一个

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation; 

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

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;

- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;

- (void)didAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation; 

- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration; 

您可以在 UIVIewController上的参考文档

Why are you going for Notifications for rotations. You can make use of any of these

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation; 

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

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation;

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;

- (void)willAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;

- (void)didAnimateFirstHalfOfRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation; 

- (void)willAnimateSecondHalfOfRotationFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation duration:(NSTimeInterval)duration; 

You can find more details in the reference documents on UIVIewController

物价感观 2024-12-11 18:10:45

为什么你要自己做一个函数来检查方向。相反,请使用 UIViewController 本身的函数。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{    
    if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        yourLabel.frame = CGRECTMAKE(10,10,50,50); // for landscape
    }
    else 
    {
        yourLabel.frame = CGRECTMAKE(20,20,50,50); // for portrait
    }

    return YES;
}

Why are you checking the orientation by making a function by yourselves. Instead, use the function for the UIViewController itself.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{    
    if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
        yourLabel.frame = CGRECTMAKE(10,10,50,50); // for landscape
    }
    else 
    {
        yourLabel.frame = CGRECTMAKE(20,20,50,50); // for portrait
    }

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