2015-04-19 25 views
0

我正在開發我的應用程序的新版本,並遇到了一個非常令人沮喪的問題。我有一些代碼,在旋轉後在屏幕上重新定位視圖(見下面的代碼)當我在模擬器中運行它時沒有問題,當我在物理設備上測試時,也沒有問題,直到我從xCode斷開連接。從Xcode斷開連接後,視圖不再正確定位。如果我重新連接到Xcode並再次運行它仍然不會像以前/預期的那樣工作。我添加了代碼中看到的日誌記錄,現在知道在第一次執行時高度和日誌都正確記錄,但是在斷開連接後它們被反轉。我還爲兩個不同的設備安裝了adhoc發行版,並在第一次執行時再次正常工作(這次沒有Xcode連接)。但是如果我在下一次執行中完全關閉應用程序(雙擊home並擦除),我們將獲得與之前完全相同的結果,並重新定位屏幕(假定NSlog值將再次顛倒)。我搜索了高和低,根本找不到任何解決方案。從xcode斷開連接後,應用程序行爲發生變化

- (void)deviceOrientationDidChangeNotification:(NSNotification*)note 
{ 
NSLog(@"rotate"); 
int height = [UIScreen mainScreen].bounds.size.height; 
int width = [UIScreen mainScreen].bounds.size.width; 
NSLog(@"%@,%d",@"height: ",height); 
NSLog(@"%@,%d",@"width: ",width); 

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 

if (UIDeviceOrientationIsPortrait(orientation)==YES){ 

    if ([self.vcID.text isEqualToString:@"i4"]) { 
     self.addheightCont.constant = 50; 

    }else{ 
     self.addheightCont.constant = 100.0; 

    } 

}else if (UIDeviceOrientationIsLandscape(orientation)==YES){ 

    if ([self.vcID.text isEqualToString:@"i5"]) { 
     self.addheightCont.constant = 32.0; 

    }else if ([self.vcID.text isEqualToString:@"i4"]) { 
     self.addheightCont.constant = 32.0; 

    }else{ 
     self.addheightCont.constant = 50.0; 
    } 

} 


if (height==568) { 

    self.imageScroller.delegate = self; 

    self.imageScroller.zoomScale = 0.0; 
    [self.imageScroller setContentOffset:CGPointMake(0, 0) animated:YES]; 

    self.LargeImage.frame = CGRectMake(0, 0, 320, 568); 
    self.imageScroller.frame = CGRectMake(0, 0, 320, 568); 
    self.imageScroller.minimumZoomScale = 1.0; 
    self.imageScroller.maximumZoomScale = 100.0; 

    if (self.CommentsView.alpha == 1.0) { 
     [self.CommentsView setCenter:CGPointMake(160, 284)]; 
     [self.ShowCommentsButton setCenter:CGPointMake(24, 445)]; 
     [self.LikeButtonbutton setCenter:CGPointMake(70.5, 445)]; 
    } 

    if (self.editCommentView.alpha == 1.0) { 
     [self.editCommentView setCenter:CGPointMake(160, 284)]; 
    } 
}else if (height==480){ 

     self.imageScroller.delegate = self; 

     self.imageScroller.zoomScale = 0.0; 
     [self.imageScroller setContentOffset:CGPointMake(0, 0) animated:YES]; 

     self.LargeImage.frame = CGRectMake(0, 0, 320, 480); 
     self.imageScroller.frame = CGRectMake(0, 0, 320, 480); 
     self.imageScroller.minimumZoomScale = 1.0; 
     self.imageScroller.maximumZoomScale = 100.0; 

     if (self.CommentsView.alpha == 1.0) { 
      [self.CommentsView setCenter:CGPointMake(160, 240)]; 
      [self.ShowCommentsButton setCenter:CGPointMake(24, 445)]; 
      [self.LikeButtonbutton setCenter:CGPointMake(70.5, 445)]; 
     } 

     if (self.editCommentView.alpha == 1.0) { 
      [self.editCommentView setCenter:CGPointMake(160, 284)]; 
     } 



}else if ((height==320) && (width==568)){ 

    self.imageScroller.delegate = self; 

    self.imageScroller.zoomScale = 0.0; 
    [self.imageScroller setContentOffset:CGPointMake(0, 0) animated:YES]; 

    self.LargeImage.frame = CGRectMake(0, 0, 568, 320); 
    self.imageScroller.frame = CGRectMake(0, 0, 568, 320); 
    self.imageScroller.minimumZoomScale = 1.0; 
    self.imageScroller.maximumZoomScale = 100.0; 

    if (self.CommentsView.alpha == 1.0) { 
     [self.CommentsView setCenter:CGPointMake(277, 160)]; 
     [self.ShowCommentsButton setCenter:CGPointMake(24, 304)]; 
     [self.LikeButtonbutton setCenter:CGPointMake(70.5, 304)]; 

    } 
    if (self.editCommentView.alpha == 1.0) { 
     [self.editCommentView setCenter:CGPointMake(284, 160)]; 
    } 
}else if ((height==320) && (width==480)){ 
    self.imageScroller.delegate = self; 

    self.imageScroller.zoomScale = 0.0; 
    [self.imageScroller setContentOffset:CGPointMake(0, 0) animated:YES]; 

    self.LargeImage.frame = CGRectMake(0, 0, 480, 320); 
    self.imageScroller.frame = CGRectMake(0, 0, 480, 320); 
    self.imageScroller.minimumZoomScale = 1.0; 
    self.imageScroller.maximumZoomScale = 100.0; 

    if (self.CommentsView.alpha == 1.0) { 
     [self.CommentsView setCenter:CGPointMake(240, 160)]; 
     [self.ShowCommentsButton setCenter:CGPointMake(24, 304)]; 
     [self.LikeButtonbutton setCenter:CGPointMake(70.5, 304)]; 

    } 
    if (self.editCommentView.alpha == 1.0) { 
     [self.editCommentView setCenter:CGPointMake(284, 160)]; 
    } 
}else if ((height==375) && (width==667)){ 
    self.imageScroller.delegate = self; 

    self.imageScroller.zoomScale = 0.0; 
    [self.imageScroller setContentOffset:CGPointMake(0, 0) animated:YES]; 

    self.LargeImage.frame = CGRectMake(0, 0, 667, 375); 
    self.imageScroller.frame = CGRectMake(0, 0, 667, 375); 
    self.imageScroller.minimumZoomScale = 1.0; 
    self.imageScroller.maximumZoomScale = 100.0; 

    if (self.CommentsView.alpha == 1.0) { 
     [self.CommentsView setCenter:CGPointMake(333.5, 187.5)]; 
     [self.ShowCommentsButton setCenter:CGPointMake(24, 304)]; 
     [self.LikeButtonbutton setCenter:CGPointMake(70.5, 304)]; 

    } 
    if (self.editCommentView.alpha == 1.0) { 
     [self.editCommentView setCenter:CGPointMake(333.5, 115)]; 
    } 
}else if (height==667) { 

    self.imageScroller.delegate = self; 

    self.imageScroller.zoomScale = 0.0; 
    [self.imageScroller setContentOffset:CGPointMake(0, 0) animated:YES]; 

    self.LargeImage.frame = CGRectMake(0, 0, 375, 667); 
    self.imageScroller.frame = CGRectMake(0, 0, 375, 667); 
    self.imageScroller.minimumZoomScale = 1.0; 
    self.imageScroller.maximumZoomScale = 100.0; 

    if (self.CommentsView.alpha == 1.0) { 
     [self.CommentsView setCenter:CGPointMake(187.5, 333.5)]; 
     [self.ShowCommentsButton setCenter:CGPointMake(24, 445)]; 
     [self.LikeButtonbutton setCenter:CGPointMake(70.5, 445)]; 
    } 

    if (self.editCommentView.alpha == 1.0) { 
     [self.editCommentView setCenter:CGPointMake(187.5,115)]; 
    } 
}else if (height==736) { 

    self.imageScroller.delegate = self; 

    self.imageScroller.zoomScale = 0.0; 
    [self.imageScroller setContentOffset:CGPointMake(0, 0) animated:YES]; 

    self.LargeImage.frame = CGRectMake(0, 0, 414, 736); 
    self.imageScroller.frame = CGRectMake(0, 0, 414, 736); 
    self.imageScroller.minimumZoomScale = 1.0; 
    self.imageScroller.maximumZoomScale = 100.0; 

    if (self.CommentsView.alpha == 1.0) { 
     [self.CommentsView setCenter:CGPointMake(207, 333.5)]; 
     [self.ShowCommentsButton setCenter:CGPointMake(24, 514)]; 
     [self.LikeButtonbutton setCenter:CGPointMake(70.5, 514)]; 
    } 

    if (self.editCommentView.alpha == 1.0) { 
     [self.editCommentView setCenter:CGPointMake(207,115)]; 
    } 
}else if (height==414){ 
    self.imageScroller.delegate = self; 

    self.imageScroller.zoomScale = 0.0; 
    [self.imageScroller setContentOffset:CGPointMake(0, 0) animated:YES]; 

    self.LargeImage.frame = CGRectMake(0, 0, 736, 414); 
    self.imageScroller.frame = CGRectMake(0, 0, 736, 414); 
    self.imageScroller.minimumZoomScale = 1.0; 
    self.imageScroller.maximumZoomScale = 100.0; 

    if (self.CommentsView.alpha == 1.0) { 
     [self.CommentsView setCenter:CGPointMake(368, 187.5)]; 
     [self.ShowCommentsButton setCenter:CGPointMake(24, 380)]; 
     [self.LikeButtonbutton setCenter:CGPointMake(70.5, 380)]; 

    } 
    if (self.editCommentView.alpha == 1.0) { 
     [self.editCommentView setCenter:CGPointMake(368, 115)]; 
    } 
} 
} 
+0

該死的,這麼多的代碼? –

+0

對於長碼感到遺憾,但我不想錯過任何東西。 – joffd

+0

老實說,你在做什麼是完全錯誤的...你應該考慮使用自動佈局... –

回答

0

Fahim的建議後,我做了一些關於自動佈局的研究,並因此設法使用自動佈局來實現修復。我已經爲父視圖頂部和頂部的註釋視圖的距離設置了一個約束,並使用下面的代碼簡單地改變常量值,以便在屏幕上和離開屏幕的位置爲視圖設置動畫效果。因此我不再需要視圖旋轉中的問題中的任何代碼。雖然我沒有找到答案,爲什麼當手機與Xcode斷開連接時行爲會發生變化,不再是自動佈局執行所有工作的問題。

要動畫在屏幕上查看我使用:

[UIView animateWithDuration:0.5 animations:^{ 

       self.CommentViewOffset.constant = 40; 
       [self.view layoutIfNeeded]; 
     }]; 

和關閉屏幕,我使用:

UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; 
[UIView animateWithDuration:0.5 animations:^{ 

    if (UIDeviceOrientationIsPortrait(orientation)==YES){ 
    self.CommentViewOffset.constant = 750.0; 
    self.editComentViewOffset.constant = 750; 
    [self.view layoutIfNeeded]; 

    }else if (UIDeviceOrientationIsLandscape(orientation)==YES){ 
    self.CommentViewOffset.constant = 440; 
    self.editComentViewOffset.constant = 440; 
       [self.view layoutIfNeeded]; 
    } 

現在我有沒有必要在deviceOrientationDidChangeNotification方法做任何事情的一切都被處理與自動佈局。我必須說我到現在爲止完全避免了自動佈局,並且真希望我已經花時間做了更早的研究。

相關問題