【问题标题】:App behaviour changes after disconnected from xcode与 xcode 断开连接后应用行为发生变化
【发布时间】:2015-04-19 11:49:42
【问题描述】:

我正在开发我的应用程序的新版本,但遇到了一个非常令人沮丧的问题。我有一些代码可以在旋转后重新定位屏幕上的视图(见下面的代码)当我在模拟器中运行它时没有问题,当我在物理设备上测试时也没有问题,直到我从 xCode 断开连接。从 Xcode 断开后,视图不再正确定位。如果我重新连接到 Xcode 并再次运行,它仍然无法像以前/预期的那样工作。我添加了您在代码中看到的日志记录,现在知道在第一次执行时,heigh 和 with 记录正确,但在断开连接后它们被反转。我还为两个不同的设备安装了一个临时发行版,并且在第一次执行时一切正常(这次没有 Xcode 连接)。但是,如果我在下一次执行时完全关闭应用程序(双击主页并擦除),我们会得到与以前完全相同的结果,视图重新定位到屏幕外(大概 NSlog 值将再次反转)。我搜索了高低,根本找不到任何解决方案。

- (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)];
    }
}
}

【问题讨论】:

  • 妈的,这么多代码?
  • 抱歉代码太长,但我不想错过任何内容。
  • 老实说,你所做的是完全错误的......你应该考虑使用自动布局......
  • 我确实尝试了自动布局,但我通过更改位置将视图从下方动画到屏幕上。我尝试布局更改动画块中的约束,但尽管它正确放置了视图,但它没有在屏幕上设置动画,它只是出现在最终位置。然后,我尝试在动画完成后更改约束,但这导致视图大小发生意外更改。这就是为什么我去手动定位它。如果我知道如何将视图从下方动画到屏幕上,我会因为这样会清除所有用于旋转的代码。

标签: ios objective-c iphone xcode


【解决方案1】:

根据 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 方法中做任何事情,因为一切都是通过自动布局处理的。我必须说直到现在我都完全避免了自动布局,真希望我早点花时间做研究。

【讨论】:

    猜你喜欢
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    • 2017-05-28
    相关资源
    最近更新 更多