【发布时间】: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