简体   繁体   English

animateWithDuration 在 iOS 下不起作用

[英]animateWithDuration not working under iOS

I've created a view (viewToAnimate) which I want the header to animate up or down.我创建了一个视图 (viewToAnimate),我希望标题向上或向下设置动画。 When I press the "down" action, it animates fine downward.当我按下“向下”动作时,它会向下很好地动画。 When I press the "up action, it does nothing. Why is this? Why can't I get it to animate off the screen?当我按下“向上”操作时,它什么也不做。这是为什么?为什么我不能让它离开屏幕动画?

Git Repo: https://github.com/joshoconnor89/animation 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()
        
    })
    
}

View in storyboard: notice that it starts above the viewcontroller:在故事板中查看:注意它从视图控制器上方开始:

在此处输入图片说明

在此处输入图片说明 The error is you connect slide up button with the two methods try to delete the connection of up method from it and let connection of down.错误是您将向上滑动按钮与这两种方法连接起来,尝试从中删除向上方法的连接并让向下连接。

The problem is CGRect (searchHeaderFrame) variable declare each Action (down and up action), i have run this code and update your code, then its working for me, you change the below code,问题是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()

        })
    }

}

see my output is below,看到我的输出如下,

在此处输入图片说明

hope its helpful希望它有帮助

I strongly suggest that you animate the constraints instead of the frame of the view when you are using AutoLayout.我强烈建议您在使用 AutoLayout 时为约束而不是视图框架设置动画。

Here's the code that works exactly the way you want:这是完全按照您想要的方式工作的代码:

@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

You see, it's much more simpler than the code you have there.你看,它比你在那里的代码简单得多。 You just have to change the constant value of the vertical space constraint of your viewToAnimate with superview.您只需要使用超级视图更改 viewToAnimate 的垂直空间约束的常量值。 You can set it as the negative value of the height of your view so that it will go exactly off the screen, and then set it back to zero then it will stick to the topmost part of the screen.您可以将其设置为视图高度的负值,以便它完全离开屏幕,然后将其设置为零,然后它会粘在屏幕的最顶部。 Simple as that.就这么简单。

The reason why I suggest you to animate the constraints rather than the frame is that in AutoLayout it is the constraints that dictates the position of the views.我建议您为约束而不是框架设置动画的原因是,在 AutoLayout 中,约束决定了视图的位置。 The frame is just the result of the AutoLayout process.框架只是 AutoLayout 过程的结果。 Although animating the frame would work, but I am sure you'll encounter layout issues in the future when there comes much more complicated animations.虽然动画框架会起作用,但我相信当出现更复杂的动画时,您将来会遇到布局问题。

Hope this helps.希望这会有所帮助。 :) :)

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

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