堆栈视图错误还是我错过了什么?

Stack view bug or did I miss something?

在这里写了一个示例应用程序来说明一些看起来不太正确的动画行为?我在屏幕的下半部分创建了一个堆栈视图,向它添加了一个按钮,将它向上移动到屏幕上{它移动}包括按钮,但按钮上的动作仍然在下半部分?

确实,如果我查看 Xcode 上的调试器,它根本没有移动。

我在这里错过了什么动作?是的?

import UIKit

class ViewController: UIViewController {

var selectSV: UIStackView!
var goNorth:CABasicAnimation!

override func viewDidLoad() {
    super.viewDidLoad()
    selectSV = UIStackView(frame: CGRect(x: 256, y: 696, width: 256, height: 196))
    selectSV!.axis = .Vertical
    selectSV!.spacing = 4.0
    selectSV!.alignment = .Center
    selectSV!.distribution = .FillEqually

    let zeroRect = CGRect(x: 0,y: 0,width: 0,height: 0)
    let newB = UIButton(frame: zeroRect)
    newB.setTitle("Blah", forState: UIControlState.Normal)
    newB.titleLabel!.font = UIFont(name: "Palatino", size: 24)
    newB.frame = CGRect(x: 0, y: 0, width: 128, height: 32)
    newB.addTarget(self, action: #selector(ViewController.blahblah(_:)), forControlEvents: .TouchUpInside)
    newB.backgroundColor = UIColor.blueColor()

    self.selectSV.addArrangedSubview(newB)
    self.view.addSubview(selectSV)
    NSTimer.scheduledTimerWithTimeInterval(4.0, target: self, selector: #selector(ViewController.moveSV), userInfo: nil, repeats: false)
    // Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func blahblah(sender: AnyObject) {
    print("You hit me!")
}

func moveSV() {
    self.animateNorth()
    self.selectSV.layer.addAnimation(self.goNorth, forKey: nil)
}

func animateNorth() {
    goNorth = CABasicAnimation(keyPath: "position.y")
    goNorth.fromValue = 792
    goNorth.toValue = 288
    goNorth.duration = 4.0
    goNorth.delegate = self
    goNorth.setValue("window", forKey:"goNorth")
    goNorth.removedOnCompletion = false
    goNorth.fillMode = kCAFillModeForwards
}
}

您正在为 stackview 的支持层而不是 stackview 本身的框架设置动画。要为堆栈视图设置动画,您可以使用 UIView 方法 animateWithDuration:animations: 并为堆栈视图的 frame 属性 设置动画。要更好地查看现在发生的情况,您可以为堆栈视图启用 selectSV.clipsToBounds = true。这样,stackview 的 layer 在离开框架时会被剪裁。

谢谢果冻!

注释掉这个...

 self.animateNorth()
 self.selectSV.layer.addAnimation(self.goNorth, forKey: nil)

并用这个完美替换它!!

UIView.animateWithDuration(0.5, animations: {
                            self.selectSV.center.y = 288.0
                        })

当您知道在这个领域要学多少……就很容易了……