在Swift iOS中单击按钮时将视图移动到另一个视图

时间:2016-07-03 09:23:38

标签: ios swift uiview uiviewanimation

我在我正在制作的应用程序中偶然发现了一个问题。我需要在按钮单击时以编程方式将一个视图放入另一个视图。

Initial State

现在我需要在带动画的按钮点击上将View 1移动到View 2的中心。我试图将View1重新定位到View 2,但我无法正确完成。

Final Result

这是我想要达到的最终结果。

创建红色视图的代码

    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
        }

请帮我解决问题。

提前致谢

2 个答案:

答案 0 :(得分:2)

我创建了一个示例项目并设置了一个目标视图以及一个按钮来在故事板中启动动画,如下所示:

storyboard setup

然后在代码中我添加了移动视图和按钮目标代码,如下所示:

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

如果您使用autolayout作为可移动视图,您可以使用以下内容:

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

答案 1 :(得分:1)

您可以将视图移动0.5秒。

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