Ios 动画期间未调用UIButton的选择器方法
我在动态视图上动态创建了按钮,之后我将该动态视图添加到主视图中。Ios 动画期间未调用UIButton的选择器方法,ios,swift,uibutton,uikit,Ios,Swift,Uibutton,Uikit,我在动态视图上动态创建了按钮,之后我将该动态视图添加到主视图中。 除了没有调用UIButton的选择器方法外,其他一切都正常工作 var baseView:UIView? override func viewDidLoad() { super.viewDidLoad() randomizeButtons()} func randomizeButtons(){ baseView = UIView(frame:CGRectMake(
除了没有调用UIButton的选择器方法外,其他一切都正常工作
var baseView:UIView?
override func viewDidLoad() {
super.viewDidLoad()
randomizeButtons()}
func randomizeButtons(){
baseView = UIView(frame:CGRectMake(view.bounds.origin.x + 10, view.bounds.origin.y + 50, view.bounds.size.width, view.bounds.size.height-20))
let viewWidth = baseView.bounds.size.width;
let viewHeight = baseView.bounds.size.height;
baseView.userInteractionEnabled = true
let i = GlobalArrayofUsers.count
var loopCount = 0
while loopCount < i{
let userBaseView = UIView(frame: CGRectMake(abs(CGFloat(arc4random()) % viewWidth - 90) , abs(CGFloat(arc4random()) % viewHeight - 120), 90, 90))
print(userBaseView.frame)
userBaseView.backgroundColor = UIColor.redColor()
userBaseView.userInteractionEnabled = true
userBaseView.layer.cornerRadius = 45
userBaseView.clipsToBounds = true
let button = UIButton(frame: userBaseView.bounds)
button.backgroundColor = UIColor.greenColor()
button.showsTouchWhenHighlighted = true
button.addTarget(self, action: #selector(ViewController.handleTapEventofBtn(_:)), forControlEvents: .TouchUpInside)
button.setTitle("hello", forState: .Normal)
button.userInteractionEnabled = true
userBaseView.addSubview(button)
baseView.addSubview(userBaseView)
print("button frame is\(button.frame)")
baseView.bringSubviewToFront(userBaseView)
loopCount += 1
}
}
baseView.subviews.forEach { (users) in
UIView.animateWithDuration(1, delay: 0, options: [.Autoreverse,.Repeat], animations: {
users.center.y += 10
}, completion: nil)
}
view.insertSubview(baseView, atIndex: view.subviews.count)
}
func handleTapEventofBtn(sender: UIButton!) {
// handling code
}
var baseView:UIView?
重写func viewDidLoad(){
super.viewDidLoad()
randomizeButtons()}
func randomizeButtons(){
baseView=UIView(框架:CGRectMake(view.bounds.origin.x+10,view.bounds.origin.y+50,view.bounds.size.width,view.bounds.size.height-20))
让viewWidth=baseView.bounds.size.width;
让viewHeight=baseView.bounds.size.height;
baseView.userInteractionEnabled=true
设i=GlobalArrayofUsers.count
var loopCount=0
而loopCount
知道为什么我的选择器方法没有被调用吗?这是由于动画。我怎么办
解决方案:在UIAnimationOptions中允许用户交互您是否尝试过以下方法:
for _ in 0...5{
// generate new view
let userBaseView = UIView(frame: CGRectMake(abs(CGFloat(arc4random()) % viewWidth - 90) , abs(CGFloat(arc4random()) % viewHeight - 120), 90, 90))
userBaseView.backgroundColor = UIColor.redColor()
// add button to view
let button = UIButton(frame: userBaseView.bounds)
button.backgroundColor = UIColor.yellowColor()
button.showsTouchWhenHighlighted = true
button.addTarget(self, action: #selector(self.handleTapEventofBtn(_:)), forControlEvents: .TouchUpInside)
button.setTitle("hello", forState: .Normal)
userBaseView.addSubview(button)
}
self.view.addSubview(userBaseView)
您只需在cycle之前创建userBaseView,然后在cycle中创建按钮并将其添加到userBaseView,然后将userBaseView添加到self.view或需要添加它的位置。在您的示例中,在循环的每个迭代中都创建userBaseView
我认为问题可能在这里:
userBaseView.addSubview(button)
// add new view containing button to self.view
self.view.addSubview(userBaseView)
}
userBaseView.addSubview(button)
baseView.addSubview(userBaseView)
首先,将userBaseView添加到循环中的self.view:
// add new view containing button to self.view
self.view.addSubview(userBaseView)
然后将其添加到baseView:
baseView.addSubview(userBaseView)
你能在IB中发布它的样子吗?//嗨。您的问题在选择器行中。试试这个:
for _ in 0...5 {
// generate new view
let userBaseView = UIView(frame: CGRectMake(abs(CGFloat(arc4random()) % self.view.frame.size.width - 90) , abs(CGFloat(arc4random()) % self.view.frame.size.height - 120), 90, 90))
userBaseView.backgroundColor = UIColor.redColor()
// add button to view
let button = UIButton(frame: userBaseView.bounds)
button.backgroundColor = UIColor.yellowColor()
button.showsTouchWhenHighlighted = true
button.addTarget(self, action: "buttonAction", forControlEvents: UIControlEvents.TouchUpInside)
button.setTitle("hello", forState: .Normal)
userBaseView.addSubview(button)
self.view.addSubview(userBaseView)
}
//使用xcode 7.2进行测试
func buttonAction() {
print("working")
}
问题是您试图单击正在设置动画的对象。从: 在动画制作过程中,用户交互将暂时禁用,以便 正在设置动画的视图。(在iOS 5之前,用户交互是 对整个应用程序禁用。) 但不用担心,解决方案非常简单,只需使用选项
.AllowUserInteraction
调用动画即可。例如(使用您的代码):
该操作有一个参数,因此选择器签名实际上是takeActionBtn:
(注意末尾的冒号)。
您需要更改该选项,否则按钮将尝试调用不带参数的函数版本,而该参数不存在
button.addTarget(self, action: "button:",forControlEvents: UIControlEvents.TouchUpInside)
func button(sender: UIButton!) {
// handling code
print("1")
}
但是我希望userBaseView也是动态的。我是通过编程来完成的。好的,你能在屏幕上显示结果吗?另外,请检查您的userBaseView&baseView是否为userInteractionEnable=True还有一件事,您在屏幕上有任何手势识别器吗?没有,没有任何手势。您是否尝试过在按钮目标中用您的控制器名称替换self?@Gottz是的,但同样的问题仍然存在。当我将代码放入viewController时,您的代码可以工作。你能更新你的问题以显示你的viewController的全部代码吗?你的按钮在按下时会高亮显示吗?@vacawama不,它们不会。没有选择器方法是可以的。我已经检查过了。当时正在调用该方法吗?不,无论如何都不会调用它。使用该方法也没有帮助吗button.addTarget(self,action:“buttonAction”,forControlEvents:UIControlEvents.TouchUpInside)请检查我的新代码。如果我使用动画,它不会work@NA000022您的代码将不再工作,因为
baseView
从未初始化过+用户未知但现在已初始化。问题是由于动画,因为我没有在UIViewAnimation中添加用户交互options@NA000022我可以复制它+解决的问题(参见我的更新)
button.addTarget(self, action: "button:",forControlEvents: UIControlEvents.TouchUpInside)
func button(sender: UIButton!) {
// handling code
print("1")
}