【问题标题】:Swift 3 - CGAffineTransform does not workSwift 3 - CGAffineTransform 不起作用
【发布时间】:2017-07-31 14:28:43
【问题描述】:

我正在以编程方式创建一个按钮,当添加 addTarget 时,CGAffineTransform 在我的项目中不起作用,为什么?

编辑:

func createButtonPuzzle(id: Int) {

     for i in 1...14 {

            let btnButton = UIButton(type: .roundedRect)
            btnButton.frame = CGRect(x: self.view.frame.size.width / 2, y: self.view.frame.size.height / 2, width: 100, height: 100)
            btnButton.backgroundColor = .blue
            btnButton.setTitle("iButton", for: .normal)
            btnButton.addTarget(self, action: #selector(moveButton(sender:)), for: .touchUpInside)

            view.addSubview(btnButton)   
     }
}


func moveButton(sender: UIButton) {

    if sender.tag == 1 {

        if sender.transform == CGAffineTransform.identity {

            UIView.animate(withDuration: 0.5, animations: 
            sender.transform = CGAffineTransform(translationX: 50, y: 100)

            })
        }                
    }
}

【问题讨论】:

  • 视图是使用自动布局还是自动调整大小定位的?这可能不适用于自动布局。
  • 不使用自动布局或自动调整大小,它已经在工作,但现在它不起作用! @Sulthan
  • 你设置断点了吗?你知道它是否真的达到了转换吗?为什么需要moveButton 的参数名称?
  • 我没有设置断点,我正在创建moveButton函数,因为btnButton是在另一个函数@dfd中创建的
  • 你创建的按钮都没有.tag = 1,所以你永远不会在if sender.tag == 1语句中执行代码

标签: swift uiview uibutton cgaffinetransform


【解决方案1】:

在函数中,没有使用sender.transform,而是使用btnButton.transform,这是因为你超出了作用域,sender可能被dismiss,导致动画被dismiss。

另一种选择是强力持有对象:

let tappedButton : UIButton = sender

然后使用 tappedButton

编辑

我的意思是这样的:

func moveButton(sender: UIButton) {

    if ((sender.tag == 1) && (sender == self.btnButton)) {

        if self.btnButton.transform == CGAffineTransform.identity {

            UIView.animate(withDuration: 0.5, animations: 
                self.btnButton.transform = CGAffineTransform(translationX: 50, y: 100)

            })
        }                
    }
}

编辑 - 2

根据你的代码有2个问题:

  1. 所有 14 个按钮都以 1 的形式堆叠在一起,这意味着按钮 14 将成为最顶部的按钮,而不是按钮 1。
  2. 您没有设置标签,然后它在您的 moveButton 签入中失败:

    func createButtonPuzzle(id: Int) {

     for i in 1...14 {
    
            let btnButton = UIButton(type: .roundedRect)
            btnButton.frame = CGRect(x: self.view.frame.size.width / 2, y: self.view.frame.size.height / 2, width: 100, height: 100)
            btnButton.backgroundColor = .blue
            btnButton.setTitle("iButton", for: .normal)
            btnButton.addTarget(self, action: #selector(moveButton(sender:)), for: .touchUpInside)
    
            **btnButton.tag = i // This is missing **
    
            view.addSubview(btnButton)   
     }
    

    }

然后这失败了: 如果 sender.tag == 1 {

因此,要对其进行测试并查看其是否正常工作,首先您需要从 14 变为 1,然后仅从 1..

然后将设置标签,当点击带有标签 1 的按钮时,它将起作用

【讨论】:

  • 我在另一个函数中创建 btnButton,我在 btnButton @unkgd 中添加这个函数
  • 你检查过你真的进入了动画吗?正如@dfd 建议的那样?如果您确实到达了那里,并且一切似乎都正常,那么将您的按钮作为数据成员添加到控制器中,并使用该引用,因为这意味着由于它的引用很弱,它被忽略了。
  • 是的,我检查了,我将数据添加到控制器,一切似乎都正常@unkgd
  • 我的意思是,创建一个按钮作为数据成员:fileprivate var btnButton : UIButton,并在方法本身而不是使用发件人,只需检查是否(se​​lf.btnButton == 发件人),并从中马上,使用 self.btnButton 而不是 sender - 用我的意思编辑我的答案
  • 在函数中创建的 btnButton 不在类中,只需等待编辑我的代码并添加所有代码 @unkgd
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-25
  • 2017-09-01
相关资源
最近更新 更多