繁体   English   中英

animateWithDuration 在 iOS 下不起作用

[英]animateWithDuration not working under iOS

我创建了一个视图 (viewToAnimate),我希望标题向上或向下设置动画。 当我按下“向下”动作时,它会向下很好地动画。 当我按下“向上”操作时,它什么也不做。这是为什么?为什么我不能让它离开屏幕动画?

Git 仓库: https : //github.com/joshoconnor89/animation

@IBAction func down(sender: AnyObject) {
    var searchHeaderFrame = self.viewToAnimate.frame
    searchHeaderFrame.origin.y = -self.viewToAnimate.frame.size.height
    self.viewToAnimate.frame = searchHeaderFrame
    searchHeaderFrame.origin.y = 0
    
    UIView.animateWithDuration(0.5, animations: {() -> Void in
        self.viewToAnimate.frame = searchHeaderFrame
        self.view!.layoutIfNeeded()
        
    })
}

@IBAction func up(sender: AnyObject) {
    
    var searchHeaderFrame = self.viewToAnimate.frame
    searchHeaderFrame.origin.y = 0
    self.viewToAnimate.frame = searchHeaderFrame
    searchHeaderFrame.origin.y = -self.viewToAnimate.frame.size.height
    
    UIView.animateWithDuration(0.5, animations: {() -> Void in
        self.viewToAnimate.frame = searchHeaderFrame
        self.view!.layoutIfNeeded()
        
    })
    
}

在故事板中查看:注意它从视图控制器上方开始:

在此处输入图片说明

在此处输入图片说明 错误是您将向上滑动按钮与这两种方法连接起来,尝试从中删除向上方法的连接并让向下连接。

问题是CGRect (searchHeaderFrame) 变量声明了每个动作(向下和向上动作),我已经运行了此代码并更新了您的代码,然后它对我有用,您更改以下代码,

 import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var viewToAnimate: UIView!
    var searchHeaderFrame : CGRect!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func SlideDownAction(sender: AnyObject) {

        searchHeaderFrame = self.viewToAnimate.frame
        searchHeaderFrame.origin.y = -self.viewToAnimate.frame.size.height
        self.viewToAnimate.frame = searchHeaderFrame
        searchHeaderFrame.origin.y = 0

        UIView.animateWithDuration(0.5, animations: {() -> Void in
            self.viewToAnimate.frame = self.searchHeaderFrame
            self.view!.layoutIfNeeded()

        })
    }

    @IBAction func SlideUpAction(sender: AnyObject) {

        searchHeaderFrame.origin.y = 0
        self.viewToAnimate.frame = searchHeaderFrame
        searchHeaderFrame.origin.y = -self.viewToAnimate.frame.size.height

        UIView.animateWithDuration(0.5, animations: {() -> Void in
            self.viewToAnimate.frame = self.searchHeaderFrame
            self.view!.layoutIfNeeded()

        })
    }

}

看到我的输出如下,

在此处输入图片说明

希望它有帮助

我强烈建议您在使用 AutoLayout 时为约束而不是视图框架设置动画。

这是完全按照您想要的方式工作的代码:

@implementation TestViewController

- (IBAction)slideUp:(id)sender
{
    _topSpaceConstraint.constant = -_viewToAnimate.frame.size.height;

    [UIView animateWithDuration:0.5f animations:^
    {
        [self.view layoutIfNeeded];
    }];
}

- (IBAction)slideDown:(id)sender
{
    _topSpaceConstraint.constant = 0;

    [UIView animateWithDuration:0.5f animations:^
     {
         [self.view layoutIfNeeded];
     }];
}

@end

你看,它比你在那里的代码简单得多。 您只需要使用超级视图更改 viewToAnimate 的垂直空间约束的常量值。 您可以将其设置为视图高度的负值,以便它完全离开屏幕,然后将其设置为零,然后它会粘在屏幕的最顶部。 就这么简单。

我建议您为约束而不是框架设置动画的原因是,在 AutoLayout 中,约束决定了视图的位置。 框架只是 AutoLayout 过程的结果。 虽然动画框架会起作用,但我相信当出现更复杂的动画时,您将来会遇到布局问题。

希望这会有所帮助。 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM