ios – 在动画时如何使用UIButton

前端之家收集整理的这篇文章主要介绍了ios – 在动画时如何使用UIButton前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经发现了一个类似的问题,但没有回答我的问题.
我有一个UIButton,它是从屏幕底部到顶部的动画.我想在移动时能够使用按钮.现在,只有当动画完成并且按钮不再动画时,该按钮才能使用.另外,我听说可能需要使用NSTimer的东西?
class ViewController: UIViewController {

    @IBAction func button2(sender: UIButton) {
        button.hidden = false
        button.center = CGPointMake(126,380);        
        UIView.animateKeyframesWithDuration(3,delay: 0,options:   .AllowUserInteraction,animations: { () -> Void in
            self.button.center = CGPointMake(126,130 )
            }) { (_) -> Void in
        }   
    }   

    @IBOutlet var label: UILabel! 
    @IBOutlet var button: UIButton!

    @IBAction func button1(sender: UIButton) {
        button.hidden = true
        label.hidden = false
    }   

    override func viewDidLoad() {
        super.viewDidLoad() 
        button.hidden = true
        label.hidden = true
    } 
}

解决方法

您必须使用CADisplayLink.例如:
@IBOutlet var button2: UIButton!

@IBAction func button3(sender: UIButton)
{
    label.hidden = false
    button2.hidden = true
}

@IBAction func button1(sender: UIButton)
{
    button2.frame = CGRectMake(120,400,100,100)
    let displayLink = CADisplayLink(target: self,selector: "handleDisplayLink:")
    displayLink.addToRunLoop(NSRunLoop.currentRunLoop(),forMode:   NSDefaultRunLoopMode)
}

func handleDisplayLink(displayLink: CADisplayLink)
{
    var buttonFrame = button2.frame
    buttonFrame.origin.y += -2
    button2.frame = buttonFrame

    if button2.frame.origin.y <= 50 
    {
        displayLink.invalidate()
    }
}

你也可以检查这个问题:Moving a button in swift using animate with duration with constraints and detecting a touch during it

猜你在找的iOS相关文章