簡體   English   中英

在Swift iOS中單擊按鈕即可將視圖移至另一個視圖

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

我偶然發現了我正在制作的應用程序中的一個問題。 我需要在單擊按鈕時以編程方式將一個視圖放入另一個視圖。

初始狀態

現在,我需要在單擊帶有動畫的按鈕時將“視圖1”移動到“視圖2”的中心。 我試圖將View1重新定位為View 2,但無法正確執行。

最后結果

這是我試圖達到的最終結果。

創建紅色視圖的代碼

    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
        }

請幫助我解決問題。

提前致謝

我創建了一個示例項目,並設置了一個目標視圖以及一個按鈕來啟動故事板中的動畫,如下所示:

故事板設置

然后在代碼中我添加了要移動的視圖和按鈕目標代碼,如下所示:

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

如果您沒有在可移動視圖中使用自動版式,則可以使用以下方法:

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

您可以將視圖移動0.5秒。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM