开发者

Unable to understand the orientation in iphone programming?

开发者 https://www.devze.com 2023-04-04 21:19 出处:网络
I am writing code in ViewDidLoad as following, [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

I am writing code in ViewDidLoad as following,

    [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:)                                               name:UIDeviceOrientationDidChangeNotification object:nil]; 
    //Whenever the device orientation changes, orientationChanged will be called

and the body of orientationChanged: Method is following

    - (void)orientationChanged:(NSNotification *)notification
    {
        UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientatio开发者_如何学编程n;

        NSLog(@"Method is Called"); 

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

        //Based upon which orientation the device is in, update your View rotations.
        //A simple view.transform = CGAffineTransformMakeRotation(whatever); will work well.
    }

I am unable to understand the behavior of this method, can any body tell me that what these two lines will do, the first one change frame of self.view.frame, and second one for superview which I did commented.

Thanks in Advance Saeen


You can manage your views according to orientation. Check my answer here.


this metod will call when ever you rotate the device you can set frame in this method

     - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
        {
        if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
        {
           self.view.frame = CGRectMake(0, 0, 480, 300);

        }
        else
        {
           self.view.frame = CGRectMake(0, 0, 320, 460);

        }
     }


use this insted of notification center it work fine

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

    - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
    {
        appDelegate.interface=interfaceOrientation;
        [popoverController dismissPopoverAnimated:YES];
        if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
        {  
        if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
        {
            txt_description.font = [UIFont systemFontOfSize:17];
            appDelegate.screenWidth = 1024;
            img_background.frame = CGRectMake(0, 0, 1024, 748);
            img_background.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"background_ipad_landscape" ofType:@"png"]];

            lbl_friendName.frame=CGRectMake(185, 58, 180, 30);
            btn_friendName.frame=CGRectMake(390, 60, 436, 27);
            [btn_friendName setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s14_ipad_landscape" ofType:@"png"]] forState:UIControlStateNormal];
            lbl_friendNameValue.frame=CGRectMake(399, 59, 300, 30);
            lbl_date.frame=CGRectMake(185, 110, 180, 30);
            btn_date.frame=CGRectMake(390, 110, 436, 27);
            [btn_date setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s14_ipad_landscape" ofType:@"png"]] forState:UIControlStateNormal];
            lbl_dateValue.frame=CGRectMake(399, 109, 300, 30);
            lbl_category.frame=CGRectMake(185, 160, 180, 30);
            btn_category.frame=CGRectMake(390, 160, 436, 27);
            [btn_category setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s14_ipad_landscape" ofType:@"png"]] forState:UIControlStateNormal];
            lbl_categoryValue.frame=CGRectMake(399, 159, 300, 30);
            //btn_radio2.frame=CGRectMake(199, 128, 21, 21);
            lbl_amount.frame=CGRectMake(185, 210, 180, 30);
            txt_amount.frame=CGRectMake(390, 210, 436, 31);
            lbl_currency.frame=CGRectMake(185, 260, 180, 30);
            lbl_currencyValue.frame=CGRectMake(399, 258, 330, 30);
            btn_currency.frame = CGRectMake(390, 260, 436, 27);
            [btn_currency setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s14_ipad_landscape" ofType:@"png"]] forState:UIControlStateNormal];
            lbl_typeofCost.frame=CGRectMake(185, 310, 180, 30);
            btn_radio1.frame = CGRectMake(390, 310, 21, 21);
            lbl_allocate.frame = CGRectMake(420, 308, 180, 27);
            btn_radio2.frame = CGRectMake(600, 310, 21, 21);
            lbl_share.frame = CGRectMake(630, 308, 180, 27);
            lbl_typeofCostValue.frame=CGRectMake(399, 348, 430, 30);
            btn_typeofCost.frame = CGRectMake(390, 350, 436, 27);
            [btn_typeofCost setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s14_ipad_landscape" ofType:@"png"]] forState:UIControlStateNormal];
            lbl_Description.frame=CGRectMake(185, 394, 180, 30);
            img_inputbox.frame=CGRectMake(390, 400, 436, 61);
            img_inputbox.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"input_box_s14_1_ipad_landscape" ofType:@"png"]];
            txt_description.frame=CGRectMake(395, 405, 436, 53);
            lbl_placeholder.frame=CGRectMake(402, 405, 350, 21);
            btn_save.frame=CGRectMake(430, 550, 128, 51);
            scrollView.contentSize=CGSizeMake(self.view.frame.size.width, 800);
            lbl_colon1.frame = CGRectMake(378, 58, 5, 30); 
            lbl_colon2.frame = CGRectMake(378, 108, 5, 30);
            lbl_colon3.frame = CGRectMake(378, 158, 5, 30);
            lbl_colon4.frame = CGRectMake(378, 208, 5, 30);
            lbl_colon5.frame = CGRectMake(378, 258, 5, 30);
            lbl_colon6.frame = CGRectMake(378, 304, 5, 30);
            lbl_colon7.frame = CGRectMake(378, 392, 5, 30);

        }
        else
        {
            appDelegate.screenWidth=480;
            [scrollView setContentSize:CGSizeMake(480, 440)];
            img_background.frame=CGRectMake(0, 0, 480,320);
            img_topbar.frame=CGRectMake(0, 0, 480, 44);
            img_background.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"background_landscape" ofType:@"png"]];
            //lbl_head.frame=CGRectMake(160, 9, 159, 33);

            lbl_friendName.frame=CGRectMake(13, 30, 110, 21);
            btn_friendName.frame=CGRectMake(130, 27, 286, 27);
            [btn_friendName setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"drop-down_s6_landscape" ofType:@"png"]] forState:0];


            lbl_date.frame=CGRectMake(13, 67, 95, 21);
            btn_date.frame=CGRectMake(130, 64, 286, 27);
            [btn_date setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"drop-down_s6_landscape" ofType:@"png"]] forState:0];

            lbl_category.frame=CGRectMake(13, 106, 95, 21);
            btn_category.frame=CGRectMake(130, 102, 286, 27);
            [btn_category setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"drop-down_s6_landscape" ofType:@"png"]] forState:0];
                        //btn_radio2.frame=CGRectMake(199, 128, 21, 21);
            lbl_amount.frame=CGRectMake(13, 143, 95, 21);
            txt_amount.frame=CGRectMake(130, 140, 286, 31);
            lbl_currency.frame=CGRectMake(13, 182, 95, 21);
            btn_currency.frame = CGRectMake(130, 179, 286, 27);
            [btn_currency setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"drop-down_s6_landscape" ofType:@"png"]] forState:0];

            lbl_typeofCost.frame=CGRectMake(13, 212, 95, 21);
            btn_radio1.frame=CGRectMake(130, 212, 21, 21);
            lbl_allocate.frame = CGRectMake(155, 212, 100, 21);
            btn_radio2.frame = CGRectMake(275, 212, 21, 21);
            lbl_share.frame = CGRectMake(310, 212, 80, 21);
            btn_typeofCost.frame = CGRectMake(130, 240, 286, 27);
            lbl_typeofCostValue.frame = CGRectMake(135, 242, 250, 21);
            [btn_typeofCost setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"drop-down_s6_landscape" ofType:@"png"]] forState:UIControlStateNormal];
            //[btn_provideDate setImage:[UIImage imageNamed:@"dropdown_s5.png"] forState:UIControlStateNormal];
            lbl_Description.frame=CGRectMake(13, 276, 150, 21);
            img_inputbox.frame=CGRectMake(130, 275, 286, 61);
            img_inputbox.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"input_box_s42_landscape" ofType:@"png"]];
            txt_description.frame=CGRectMake(135, 281, 276, 45);
            lbl_placeholder.frame=CGRectMake(142, 278, 200, 21);
            btn_save.frame=CGRectMake(190, 345, 108, 41);
            //btn_adv.frame=CGRectMake(0, 175, 480, 48);
            lbl_colon1.frame = CGRectMake(119, 27, 5, 27); 
            lbl_colon2.frame = CGRectMake(119, 63, 5, 27);
            lbl_colon3.frame = CGRectMake(119, 102, 5, 27);
            lbl_colon4.frame = CGRectMake(119, 140, 5, 27);
            lbl_colon5.frame = CGRectMake(119, 179, 5, 27);
            lbl_colon6.frame = CGRectMake(119, 208, 5, 27);
            lbl_colon7.frame = CGRectMake(119, 272, 5, 27);
            [self displayProperFontSize];
            }


        }
        if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown || interfaceOrientation == UIInterfaceOrientationPortrait)
        { 

            if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
            {
            txt_description.font = [UIFont systemFontOfSize:17];
            appDelegate.screenWidth=768;
            img_background.frame = CGRectMake(0, 0, 768, 1004);
            img_background.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"background_ipad" ofType:@"png"]];

            lbl_friendName.frame=CGRectMake(105, 65, 180, 30);
            btn_friendName.frame=CGRectMake(310, 67, 316, 27);
            [btn_friendName setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s6_ipad" ofType:@"png"]] forState:0];
            lbl_friendNameValue.frame=CGRectMake(318, 66, 280, 28);
            lbl_date.frame=CGRectMake(105, 116, 180, 30);
            btn_date.frame=CGRectMake(310, 116, 316, 27);
            [btn_date setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s6_ipad" ofType:@"png"]] forState:0];
            lbl_dateValue.frame=CGRectMake(315, 116, 278, 26);
            lbl_category.frame=CGRectMake(105, 164, 180, 30);
            btn_category.frame=CGRectMake(310, 166, 316, 27);
            [btn_category setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s6_ipad" ofType:@"png"]] forState:0];
            lbl_categoryValue.frame=CGRectMake(318, 167, 273, 26);
            //btn_radio2.frame=CGRectMake(199, 128, 21, 21);
            lbl_amount.frame=CGRectMake(105, 210, 180, 30);
            txt_amount.frame=CGRectMake(310, 210, 316, 31);
            lbl_currency.frame=CGRectMake(105, 260, 180, 30);
            btn_currency.frame = CGRectMake(310, 258,316, 33);
            lbl_currencyValue.frame=CGRectMake(316, 260, 310, 30);
            [btn_currency setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s6_ipad" ofType:@"png"]] forState:0];
            lbl_typeofCost.frame=CGRectMake(105, 310, 180, 30);
            btn_radio1.frame=CGRectMake(310, 318, 21, 21);
            lbl_allocate.frame = CGRectMake(340, 318, 100, 21);
            btn_radio2.frame = CGRectMake(445, 318, 21, 21);
            lbl_share.frame = CGRectMake(475, 318, 200, 21);
            btn_typeofCost.frame = CGRectMake(310, 357,316, 33);
            lbl_typeofCostValue.frame=CGRectMake(315, 359, 310, 30);
            [btn_typeofCost setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s6_ipad" ofType:@"png"]] forState:0];
            //[btn_provideDate setImage:[UIImage imageNamed:@"dropdown_s5.png"] forState:UIControlStateNormal];
            lbl_Description.frame=CGRectMake(105, 400, 180, 30);
            img_inputbox.frame=CGRectMake(310, 410, 316, 61);
            img_inputbox.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"input_box_s42_1_ipad" ofType:@"png"]];
            txt_description.frame=CGRectMake(316, 414, 310, 53);
            lbl_placeholder.frame = CGRectMake(322, 414, 290, 21);

            lbl_colon1.frame = CGRectMake(300, 65, 7, 30); 
            lbl_colon2.frame = CGRectMake(300, 114, 7, 30);
            lbl_colon3.frame = CGRectMake(300, 164, 7, 30);
            lbl_colon4.frame = CGRectMake(300, 209, 7, 30);
            lbl_colon5.frame = CGRectMake(300, 260, 7, 30);
            lbl_colon6.frame = CGRectMake(300, 313, 7, 30);
            lbl_colon7.frame = CGRectMake(300, 402, 7, 30);

            btn_save.frame=CGRectMake(300, 550, 128, 51);
            } 
            else
            {
               appDelegate.screenWidth=320;
               [scrollView setContentSize:CGSizeMake(self.view.frame.size.width, self.view.frame.size.height)];
                img_background.frame=CGRectMake(0, 0, 320,480);
                img_background.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"background" ofType:@"png"]];
                lbl_friendName.frame=CGRectMake(8, 16, 97, 34);
                btn_friendName.frame=CGRectMake(114, 20, 196, 27);
                [btn_friendName setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s6" ofType:@"png"]] forState:UIControlStateNormal];

            lbl_friendNameValue.frame=CGRectMake(121, 20, 170, 28);
            lbl_date.frame=CGRectMake(8, 59, 95, 21);
            btn_date.frame=CGRectMake(114, 56, 196, 27);
            [btn_date setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s6" ofType:@"png"]] forState:UIControlStateNormal];
            lbl_dateValue.frame=CGRectMake(121, 53, 170, 34);
            lbl_category.frame=CGRectMake(8, 96, 95, 21);
            btn_category.frame=CGRectMake(114, 94, 196, 27);
            [btn_category setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s6" ofType:@"png"]] forState:UIControlStateNormal];
            lbl_categoryValue.frame=CGRectMake(120, 90, 166, 34);
            //btn_radio2.frame=CGRectMake(199, 128, 21, 21);
            lbl_amount.frame=CGRectMake(8, 136, 95, 21);
            txt_amount.frame=CGRectMake(114, 131, 196, 31);
            lbl_currency.frame=CGRectMake(8, 170, 95, 21);

            btn_currency.frame =CGRectMake(114, 170, 196, 27);
            [btn_currency setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s6" ofType:@"png"]] forState:UIControlStateNormal];
            lbl_typeofCost.frame=CGRectMake(8, 205, 95, 21);
            btn_radio1.frame=CGRectMake(114, 205, 21, 21);
            lbl_allocate.frame = CGRectMake(140, 205, 80, 21);
            btn_radio2.frame = CGRectMake(220, 205, 21, 21);
            lbl_share.frame = CGRectMake(245, 199, 65, 34);
            btn_typeofCost.frame = CGRectMake(114, 233, 196, 27);
            lbl_typeofCostValue.frame = CGRectMake(119, 229, 170, 34);
            [btn_typeofCost setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dropdown_s6" ofType:@"png"]] forState:UIControlStateNormal];
            //[btn_provideDate setImage:[UIImage imageNamed:@"dropdown_s5.png"] forState:UIControlStateNormal];
            lbl_Description.frame=CGRectMake(8, 265, 95, 21);
            img_inputbox.frame=CGRectMake(114, 267, 196, 51);
            img_inputbox.image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"input_box_s16" ofType:@"png"]];
            txt_description.frame=CGRectMake(117, 274, 185, 37);
            lbl_placeholder.frame = CGRectMake(124, 270, 160, 21);
            btn_save.frame=CGRectMake(109, 326, 88, 41);

            lbl_colon1.frame = CGRectMake(105, 20, 5, 27); 
            lbl_colon2.frame = CGRectMake(105, 55, 5, 27);
            lbl_colon3.frame = CGRectMake(105, 94, 5, 27);
            lbl_colon4.frame = CGRectMake(105, 133, 5, 27);
            lbl_colon5.frame = CGRectMake(105, 168, 5, 27);
            lbl_colon6.frame = CGRectMake(105, 201, 5, 27);
            lbl_colon7.frame = CGRectMake(105, 262, 5, 27);
            [self displayProperFontSize];
            }   
        }
        [appDelegate adjustTab];


    }
0

精彩评论

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