我正在開發我的應用程序的新版本,並遇到了一個非常令人沮喪的問題。我有一些代碼,在旋轉後在屏幕上重新定位視圖(見下面的代碼)當我在模擬器中運行它時沒有問題,當我在物理設備上測試時,也沒有問題,直到我從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)];
}
}
}
該死的,這麼多的代碼? –
對於長碼感到遺憾,但我不想錯過任何東西。 – joffd
老實說,你在做什麼是完全錯誤的...你應該考慮使用自動佈局... –