【问题标题】:Re-initialize UIView view and subview after rotation旋转后重新初始化 UIView 视图和子视图
【发布时间】:2013-07-06 18:49:07
【问题描述】:

旋转后我的视图大小重置有点问题,我无法修复它。

我在视图“bottomContainerView”上放置了一个“滑动”动作,大小因方向而异!

这是我的新代码:

-(void)swipeToggle:(UISwipeGestureRecognizer *)sender {
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeLeft || [[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) {
    if (sender.direction ==  UISwipeGestureRecognizerDirectionUp){

         NSLog(@"if gesture up - LS");

        [UIView animateWithDuration:0.5
                              delay:0.1
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             topContainerView.frame = CGRectMake(0.0, -160.0, 480.0, 244.0);
                             bottomContainerView.frame = CGRectMake(0.0, 0.0, 480.0, 300.0);}
                         completion:^(BOOL finished){
                           NSLog(@"%f,%f",bottomContainerView.frame.size.width,bottomContainerView.frame.size.height);                                   
                         }];

    } else if (sender.direction ==  UISwipeGestureRecognizerDirectionDown) {

        NSLog(@"else if gesture down - LS");

        [UIView animateWithDuration:0.5
                              delay:0.1
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             topContainerView.frame = CGRectMake(0.0, -160.0, 480.0, 244.0);
                             bottomContainerView.frame = CGRectMake(0.0, 84.0, 480.0, 216.0);}
                         completion:^(BOOL finished){
                            NSLog(@"%f,%f",bottomContainerView.frame.size.width,bottomContainerView.frame.size.height); 
                         }];
    }
}
else if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait) {
    if (sender.direction ==  UISwipeGestureRecognizerDirectionUp) {

        NSLog(@"if gesture down - PT");

        [UIView animateWithDuration:0.5
                          delay:0.1
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         topContainerView.frame = CGRectMake(0.0, 0.0, 320.0, 244.0);
                         bottomContainerView.frame = CGRectMake(0.0, 0.0, self.view.frame.size.width, 640.0);
                     }
                     completion:^(BOOL finished){
                         NSLog(@"%f,%f",bottomContainerView.frame.size.width,bottomContainerView.frame.size.height);
                     }];
     }
    else if (sender.direction ==  UISwipeGestureRecognizerDirectionDown) {

        NSLog(@"else if gesture up - PT");

        [UIView animateWithDuration:0.5
                              delay:0.1
                            options:UIViewAnimationOptionCurveEaseInOut
                         animations:^{
                             topContainerView.frame = CGRectMake(0.0, 0.0, 320.0, 244.0);
                             bottomContainerView.frame = CGRectMake(0.0, 244.0, self.view.frame.size.width, 216.0);
                         }
                         completion:^(BOOL finished){
                            NSLog(@"%f,%f",bottomContainerView.frame.size.width,bottomContainerView.frame.size.height); 
                         }];

    }
}        

}

发布更新 - 这是我的新代码与您教给我的内容正确,请解释一下 ;-)

【问题讨论】:

  • 你的动画代码是一团糟。为什么要在基于块的动画旁边使用 commitAnimations /beginAnimations?
  • 只是因为我是新手!但也许你可以向我解释为什么我做错了,以及如何做得更好而不是批评?
  • 尤其是因为你似乎是一个优秀的 iOS 开发者!

标签: ios animation uiview rotation


【解决方案1】:

查看此代码时,我的眼睛受到伤害。

在这里,您尝试结合两种声明动画的方式。 如果您使用beginAnimations:,则不必使用[UIView animateWithDuration:] 方法,但必须使用[UIView commitAnimations]; 完成动画代码

最清晰的方法是使用[UIView animateWithDuration:]并将动画代码放在animations:^{}块中。

P.S:我强烈建议不要在代码中使用幻数,而是使用常量。 SwitchToFullNormalScreen 和 SwitchToNormalScreen 动画只是复制粘贴。我的意思是来吧,那不好。

【讨论】:

  • 阅读您的评论后,我意识到自己的错误,因此我查看了 Apple 文档以了解更多信息!所以我尝试了,现在我理解得更好了!!非常感谢你,我学到了一些东西,我相信它会为我的未来服务!我唯一不明白的是最后的“BOOL完成”是什么,我觉得应该在这里放置代码来将我的视图重置为正确的大小,对吧?
  • 您应该学习如何使用 Xcode 内联文档。引用:“动画序列结束时要执行的块对象。该块没有返回值,并采用单个布尔参数指示动画是否在调用完成处理程序之前实际完成”
  • 我希望这篇文章被标记为已解决,但很抱歉,事实并非如此!即使您必须帮助我了解动画的工作原理,问题仍然存在。当我在 LS 模式下运行动画并返回纵向模式时,我的视图不会重新初始化,也不会恢复到正确的大小。感谢您的支持,但事情将被关闭,当它是真的!我整天努力解决这个问题,但我没有成功!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多