将UIImage视图从A点移动到B点的最佳简单代码 [英] best simplest code to move a UIImage View from point A to point B

查看:164
本文介绍了将UIImage视图从A点移动到B点的最佳简单代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@IBOutlet weak var imageView: UIImageView!

override func viewWillAppear(animated: Bool) {
    self.imageView.center.y -= view.bounds.height
}
override func viewDidAppear(animated: Bool) {
    UIView.animateWithDuration(0.5, delay: 0.3, options: nil, animations: {
        self.imageView.center.y += self.view.bounds.height
    }, completion: nil)
}

此代码确实可以在屏幕上成功移动图像视图,但我不确定如何使它停在我想要的地方。它只是滑出屏幕。目的是将图像从A点移动到B点。如果有另一种更简单的方法,我很高兴知道。谢谢!

This code does successfully move the image view across the screen, but I'm not sure how to make it stop where I want it to. It just slides off the screen. The goal is to move the image from point A to point B. If there is another easier way to do this I would be happy to know. Thanks!

推荐答案

在viewWillAppear中添加它,您将获得动画,问题是autoLayout

Add this in viewWillAppear,you will get your animation,you problem is autoLayout

override func viewWillAppear(animated: Bool) {
        self.imageview.setTranslatesAutoresizingMaskIntoConstraints(true)
        self.imageview.center.y -= self.view.bounds.height
    }

移动视框时,最好使用动画antoLayout约束

When you move a view frame,it is better use animate antoLayout Constraint

将红色约束拖到出口中,然后将
拖到出口
示例代码

Drag the red Constraint into outlet then Example Code

class ViewController: UIViewController{
    @IBOutlet weak var imageview: UIImageView!
    @IBOutlet weak var yConstraint: NSLayoutConstraint!

    override func viewWillAppear(animated: Bool) {
        yConstraint.constant -= self.view.bounds.height;
        self.view.updateConstraintsIfNeeded()
        self.view.layoutIfNeeded()
    }
    override func viewDidAppear(animated: Bool) {
        yConstraint.constant += self.view.bounds.height;

        UIView.animateWithDuration(0.5, delay: 0.3, options: nil, animations: {
            self.view.layoutIfNeeded()
            }, completion: nil)
    }
}

这篇关于将UIImage视图从A点移动到B点的最佳简单代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆