我正在开发我的应用程序的新版本,并遇到了一个非常令人沮丧的问题。我有一些代码在旋转后重新定位屏幕上的视图(见下面的代码)当我在模拟器中运行它没有问题,当我在物理设备上测试时也没有问题,直到我从xCode断开连接。断开与Xcode的连接后,视图不再正确定位。如果我重新连接到Xcode并再次运行它仍然不会像以前那样工作/预期。我添加了您在代码中看到的日志记录,现在知道在第一次执行时高度和正确记录但在断开连接后它们被反转。我还安装了一个adhoc发行版到两个不同的设备,并在第一次执行时一切正常(这次没有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)];
}
}
}
答案 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方法中执行任何操作,因为所有内容都是使用自动布局处理的。我必须说我到目前为止完全避免了自动布局,并且真的希望我早点花时间做研究。