Ios 如何使按钮在动画中被点击时消失
我创建了一个按钮,并设置了一些动画,但现在我不能点击它或互动,而它是在运动,如果它让我点击它,我会得到一个错误。 我的目标是当你点击按钮时,让它在移动时消失。请帮忙!!提前谢谢Ios 如何使按钮在动画中被点击时消失,ios,iphone,swift,uibutton,uiviewanimation,Ios,Iphone,Swift,Uibutton,Uiviewanimation,我创建了一个按钮,并设置了一些动画,但现在我不能点击它或互动,而它是在运动,如果它让我点击它,我会得到一个错误。 我的目标是当你点击按钮时,让它在移动时消失。请帮忙!!提前谢谢 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. // Create button let
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Create button
let button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 100)
button.setTitle("Test Button", forState: UIControlState.Normal)
button.addTarget(self, action: "buttonAction", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
UIView.animateWithDuration(4, delay: 3, options: UIViewAnimationOptions.AllowUserInteraction | UIViewAnimationOptions.LayoutSubviews | UIViewAnimationOptions.Autoreverse, animations: {
button.frame = CGRectMake(100, 300, 100, 100)
}, completion: nil)
// Create a function that makes the button dissapear when it is tapped
func buttonAction() {
button.alpha = 0
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}您必须通过将按钮从IB“ctrl+拖动”到ViewController来创建插座。接下来,您可以创建一个IBaction()函数
@IBAction func likedThis(sender: UIButton) {
//your code
}
当您按下按钮时,您放入此函数中的代码将自动触发。为了修复崩溃,您需要将按钮操作函数移出查看加载范围,并按如下方式修改它:
func buttonAction(button: UIButton) {
button.alpha = 0
}
您还需要将按钮。addTarget(…)参数操作从此“按钮操作”更改为此“按钮操作:”。请参阅下面的代码
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
如果要在按钮移动时与其交互,则不能使用animateWithDuration来设置按钮位置的动画。按钮的触摸点实际上会立即移动到最终位置(当按钮移动时,您可以在那里触摸,您将看到它调用其动作方法)。您需要使用计时器(或CADisplayLink)在每次通话中递增地移动按钮 另外,正如其他人所指出的,您需要将按钮的操作更改为“buttonAction:”以便按钮将自身作为参数发送。您可以像这样使用CADisplayLink
import UIKit
class ViewController: UIViewController {
var button: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
button = UIButton.buttonWithType(UIButtonType.System) as UIButton
button.frame = CGRectMake(100, 100, 100, 100)
button.setTitle("Test Button", forState: UIControlState.Normal)
button.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
self.view.addSubview(button)
var displayLink = CADisplayLink(target: self, selector: "handleDisplayLink:")
displayLink.addToRunLoop(NSRunLoop.currentRunLoop(), forMode: NSDefaultRunLoopMode)
}
func handleDisplayLink(displayLink: CADisplayLink) {
var buttonFrame = button.frame
buttonFrame.origin.y += 1
button.frame = buttonFrame
if button.frame.origin.y >= 300 {
displayLink.invalidate()
}
}
func buttonAction(sender: UIButton) {
sender.alpha = 0
}
}
也许您可以使用布尔变量跟踪对象是否正在移动 然后,尝试使用按钮的“隐藏”属性:
var moving: Bool!
if moving == true {
button.hidden = true
} else {
button.hidden = false
}
希望你能找到答案。你想让它在点击后移动时淡出吗?如果它对你有用,如果你接受我的回答,我将不胜感激。@GarretKaye,你找到答案了吗,还是你仍然需要帮助?是的,我需要帮助!我想弄清楚如何实现一个CAdisplayLink,这样我就可以在点设置动画时点击它,使其消失。请帮帮我!或者点击按钮我的意思是不需要
if
语句,只需制作button.hidden=moving