简体   繁体   中英

Move a View into Another view on button click in Swift iOS

I am stumbled upon an issue in an application that i am making. I need to place one view into another view programmatically on button click.

初始状态

Now i need to move View 1 to the centre of View 2 on a button click with an animation. I tried to reposition the View1 to View 2 but i am not able to do it properly.

最后结果

This is the Final result that i am trying to achieve.

CODE FOR CREATING THE RED VIEW

    My.cellSnapshot  = snapshopOfCell(cell)
            var center = cell.center
            My.cellSnapshot!.center = center
            My.cellSnapshot!.alpha = 0.0
            ingredientsTableView.addSubview(My.cellSnapshot!)


func snapshopOfCell(inputView: UIView) -> UIView {
            UIGraphicsBeginImageContextWithOptions(inputView.bounds.size, false, 0.0)
            inputView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
            let image = UIGraphicsGetImageFromCurrentImageContext() as UIImage
            UIGraphicsEndImageContext()
            let cellSnapshot : UIView = UIImageView(image: image)
            cellSnapshot.layer.masksToBounds = false
            cellSnapshot.layer.cornerRadius = 0.0
            cellSnapshot.layer.shadowOffset = CGSizeMake(-5.0, 0.0)
            cellSnapshot.layer.shadowRadius = 5.0
            cellSnapshot.layer.shadowOpacity = 0.4
            return cellSnapshot
        }

Please help me in solving the problem.

Thanks in advance

i created a sample project and set up a target view as well as a button to start the animation in storyboard like this:

故事板设置

then in code i added the view to move and the button target code like this:

var sourceView: UIView!
@IBOutlet weak var destinationView: UIView!

var sourceViewPositioningConstraints = [NSLayoutConstraint]()

override func viewDidLoad() {
    super.viewDidLoad()

    sourceView = UIView()
    sourceView?.backgroundColor = UIColor.redColor()

    sourceView?.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(sourceView)

    // size constraints
    NSLayoutConstraint(item: sourceView, attribute: .Width, relatedBy: .Equal, toItem: view, attribute: .Width, multiplier: 0.25, constant: 0).active = true
    NSLayoutConstraint(item: sourceView, attribute: .Width, relatedBy: .Equal, toItem: sourceView, attribute: .Height, multiplier: 16/9, constant: 0).active = true

    // positioning constraints
    sourceViewPositioningConstraints += [NSLayoutConstraint(item: sourceView, attribute: .Top, relatedBy: .Equal, toItem: topLayoutGuide, attribute: .BottomMargin, multiplier: 1, constant: 0)]
    sourceViewPositioningConstraints += [NSLayoutConstraint(item: sourceView, attribute: .CenterX, relatedBy: .Equal, toItem: view, attribute: .CenterX, multiplier: 1, constant: 0)]
    NSLayoutConstraint.activateConstraints(sourceViewPositioningConstraints)
}

@IBAction func move(sender: UIButton) {
    // deactivate current positioning constraints
    NSLayoutConstraint.deactivateConstraints(sourceViewPositioningConstraints)
    sourceViewPositioningConstraints.removeAll()

    // add new positioning constraints
    sourceViewPositioningConstraints += [NSLayoutConstraint(item: sourceView, attribute: .CenterX, relatedBy: .Equal, toItem: destinationView, attribute: .CenterX, multiplier: 1, constant: 0)]
    sourceViewPositioningConstraints += [NSLayoutConstraint(item: sourceView, attribute: .CenterY, relatedBy: .Equal, toItem: destinationView, attribute: .CenterY, multiplier: 1, constant: 0)]
    NSLayoutConstraint.activateConstraints(sourceViewPositioningConstraints)

    // animate constraint changes
    UIView.animateWithDuration(1) {
        self.view.layoutIfNeeded()
    }
}

if you are not using autolayout for your movable view you can simply use something like this:

override func viewDidLoad() {
    super.viewDidLoad()

    sourceView = UIView()
    sourceView?.backgroundColor = UIColor.redColor()
    sourceView.frame = CGRect(x: 40, y: 40, width: 100, height: 100)
    view.addSubview(sourceView)

}

@IBAction func move(sender: UIButton) {
    // animate constraint changes
    UIView.animateWithDuration(1) {
        self.sourceView.center = self.destinationView.center
    }
}

You can move the view for 0.5 seconds.

UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: { 
redView.center = greenView.center 
}, completion: nil)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM