Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/97.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何从上到下设置UIView动画,反之亦然?_Ios_Swift_Animation_Uiimageview - Fatal编程技术网

Ios 如何从上到下设置UIView动画,反之亦然?

Ios 如何从上到下设置UIView动画,反之亦然?,ios,swift,animation,uiimageview,Ios,Swift,Animation,Uiimageview,我正在尝试从上到下设置UIImageView的动画,反之亦然。 我试过密码: 以前的值是(y==0): 并希望将其动画设置为500.0: laserImageView.frame = CGRect(x: 0, y: 0.0, width: 300.0, height: 300.0) UIView.animate(withDuration: 3.0, delay: 0.2, options: [.curveEaseInOut], animations: { laserImageView.

我正在尝试从上到下设置UIImageView的动画,反之亦然。 我试过密码:

以前的值是(
y==0
):

并希望将其动画设置为
500.0

laserImageView.frame = CGRect(x: 0, y: 0.0, width: 300.0, height: 300.0)
UIView.animate(withDuration: 3.0, delay: 0.2, options: [.curveEaseInOut], animations: {
    laserImageView.frame = CGRect(x: 0, y: 500.0, width: 300.0, height: 300.0)
}) { (finished) in
    if finished {
        // Repeat animation from bottom to top
    }
}
但是当我运行我的应用程序时,UIImageView在最后一点(
y==500
)出现,没有任何动画,就像初始位置是
500.0
,而不是
0.0


我不明白为什么动画不起作用。有什么想法吗?

一种方法是获取一个视图的顶部约束,您希望使用以下方法对其设置动画并更新约束值:-

- (IBAction)animateButton:(UIButton *)sender {
    [UIView transitionWithView:_topView duration:0.8 options:UIViewAnimationOptionRepeat animations:^{
        _headerViewTopConstraint.constant = self.view.frame.size.height - 20;
         [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
    }];
}

对于那些想使用代码(尽管是interface builder)进行此操作的人,可以使用以下答案:-

__weak FirstViewController *weakSelf = self;
[UIView transitionWithView:_topView duration:0.4 options:UIViewAnimationOptionCurveEaseIn animations:^{
    //
    _topView.frame = CGRectMake(0, 300, _topView.frame.size.width, _topView.frame.size.height);
    [weakSelf.view layoutIfNeeded];
} completion:^(BOOL finished) {
    //
}];
请尝试发布完整的问题,但不要对答案投反对票:)谢谢

尝试以下代码:

class ViewController: UIViewController {

let laserImageView = UIImageView(frame: CGRect(x: 0, y: 0.0, width: 300.0, height: 300.0))



override func viewDidLoad() {
    super.viewDidLoad()

    laserImageView.backgroundColor = UIColor.blue
    self.view.addSubview(laserImageView)
}


override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    bottom()


}

func bottom()  {

    UIView.animate(withDuration: 3.0, delay: 0.2, options: [.curveEaseInOut], animations: {
        self.laserImageView.frame = CGRect(x: 0, y: 500.0, width: 300.0, height: 300.0)
    }) { (finished) in
        if finished {
            // Repeat animation from bottom to top
            self.Up()
        }
    }
}

func Up()  {

    UIView.animate(withDuration: 3.0, delay: 0.2, options: [.curveEaseInOut], animations: {
        self.laserImageView.frame = CGRect(x: 0, y: self.laserImageView.bounds.origin.y, width: 300.0, height: 300.0)
    }) { (finished) in
        if finished {
            // Repeat animation to bottom to top
            self.bottom()
        }
    }

}

}

在此处添加了此动画代码。在viewDidLoad中?@AdityaSrivastava是,在
viewDidLoad
中。我总是在那里叫它=/使用自动布局?@KumarKL不,我在code中添加了所有东西,在那里你设置了这个
laserImageView.frame=CGRect(x:0,y:0.0,宽度:300.0,高度:300.0)
。在动画代码之前?提问者没有使用Interface Builder。所以你需要在提问时提及它。不需要否决投票,因为这肯定会解决你的目的。@moritz谢谢兄弟,但如果问题不完整,那么一个人怎么能给他一个准确的答案呢。除了否决投票,他还需要要求发布代码的答案。@Moritz okey bro但请想一想,如果问题不针对界面生成器或代码用户,请随意以任何方式发布答案,因此我选择了界面,回答有什么问题。是的,我在地点语言选择上错了,因为它是为目标c旁边的swift标签张贴的。问题已被swift标记,张贴目标c答案有什么意义?在这里张贴2个答案有什么意义。只需更新您的答案或合并两个答案。@AdityaSrivastava我已经发布了界面生成器的答案1和代码的答案1。@iAction func AnimateButtonAction(uSender:UIButton){UIView.transition(带:topView,持续时间:0.4,选项:UIViewAnimationOptions.curveEaseIn,动画:{self.topView.frame=CGRect(x:0,y:300,宽度:self.topView.frame.size.width,高度:self.topView.frame.size.height)self.view.layoutifneed();}){(isFinished)in}“@AnupamGupta就这样写吧——第一种方法写代码,然后第二种方法写界面生成器。尽量简单。不要为同一个问题给出两个答案。这里你必须非常简单。我找到了更优雅的解决方案;)