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

How to change UIButton without reset position?

CL So
1#
CL So Published in 2018-01-13 06:55:17Z

I have made a draggable button

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

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?

Bharath
2#
Bharath Reply to 2018-01-13 07:03:14Z

Try setting button background image instead of button image. Since setting image will rescale its position.

You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO