best simplest code to move a UIImage View from point A to point B

你。 提交于 2020-01-14 05:03:26

问题


@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)
}

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!


回答1:


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
    }

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)
    }
}



回答2:


override func viewDidAppear(animated: Bool) {
    var destinationY:CGFloat = self.view.bounds.height / 2 // Your destination Y
    UIView.animateWithDuration(0.5, delay: 0.3, options: nil, animations: {
        self.imageView.center.y = destinationY
    }, completion: nil)
}

Just moving it too far, that'll put the center in the middle of the screen.




回答3:


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

Save original point and go!




回答4:


For this line: self.imageView.center.y += self.view.bounds.height

Maybe make it move less? self.imageView.center.y += self.view.bounds.height - 200




回答5:


Your implementation is fine, you just have to understand the amount you are changing y by. self.view.bounds.height refers to the height of the view controllers view. That's probably pretty big, in fact, likely the entire height of the screen you're looking at. So you need to decide what point you want to move it to and do some math. Say you want to stop it when it hits the top edge of the screen. You might do something very simple like:

self.imageView.center.y += (self.view.frame.origin.y - self.imageView.frame.origin.y)

I did not test this, please implement in the way that works best for your situation.



来源:https://stackoverflow.com/questions/30066602/best-simplest-code-to-move-a-uiimage-view-from-point-a-to-point-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!