【问题标题】:Move a View into Another view on button click in Swift iOS在 Swift iOS 中单击按钮时将视图移动到另一个视图
【发布时间】:2016-07-03 09:23:38
【问题描述】:

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

现在我需要通过单击动画将视图 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
        }

请帮我解决问题。

提前致谢

【问题讨论】:

  • 你在使用自动布局吗?
  • @AndréSlotta 是的,我正在使用自动布局。视图 1 是一个以编程方式创建的视图。
  • 试试view1.center = view2.center
  • 您只是想让视图重叠吗?还是之后一个视图必须是另一个视图的子视图?能否请您展示如何在代码中创建红色视图?
  • 这应该将视图移动 0.5 秒。 UIView.animateWithDuration(0.5,延迟:0.0,选项:UIViewAnimationOptions.CurveEaseOut,动画:{ redView.center = greenView.center },完成:无)

标签: ios swift uiview uiviewanimation


【解决方案1】:

您可以将视图移动 0.5 秒。

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

【讨论】:

    【解决方案2】:

    我创建了一个示例项目并设置了一个目标视图以及一个按钮以在情节提要中启动动画,如下所示:

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2016-04-10
    • 1970-01-01
    相关资源
    最近更新 更多