best simplest code to move a UIImage View from poi

2019-06-10 19:39发布

@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!

5条回答
萌系小妹纸
2楼-- · 2019-06-10 20:00

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

查看更多
该账号已被封号
3楼-- · 2019-06-10 20:07

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.

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-06-10 20:09
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!

查看更多
霸刀☆藐视天下
5楼-- · 2019-06-10 20:13

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 enter image description here 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)
    }
}
查看更多
太酷不给撩
6楼-- · 2019-06-10 20:15
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.

查看更多
登录 后发表回答