Home How to change UIButton without reset position?
Reply: 0

How to change UIButton without reset position?

user897 Published in July 23, 2018, 1:46 pm

I have made a draggable button

let buttonGesture = UIPanGestureRecognizer(target: self, action: #selector(draggedButton(_:)))
menuButton.isUserInteractionEnabled = true

When the button drag to certain position, image will be changed.

@objc func draggedButton(_ sender:UIPanGestureRecognizer){
    self.view.bringSubview(toFront: menuButton)
    let translation = sender.translation(in: self.view)

    menuButton.center = CGPoint(x: menuButton.center.x + translation.x, y: menuButton.center.y + translation.y)
    sender.setTranslation(CGPoint.zero, in: self.view)

    if(menuButton.center.x < 100){
        menuButton.setImage(UIImage(named: "newimage"), for: UIControlState.normal)

But after setImage, button will reset position, how to prevent this?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.250399 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO