【问题标题】:Stack view bug or did I miss something?堆栈视图错误还是我错过了什么?
【发布时间】:2016-04-06 07:19:10
【问题描述】:

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

确实,如果我查看 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
}
}

【问题讨论】:

    标签: ios swift animation layer


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      谢谢果冻!

      注释掉这个...

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

      并用这个完美地替换它!

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

      当您知道在这个领域要学习多少...时很容易...

      【讨论】:

        猜你喜欢
        • 2021-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-14
        相关资源
        最近更新 更多