如何在tableviewcell按钮上停止动画?

时间:2015-06-17 07:31:29

标签: ios iphone swift animation audio

我有一个按钮,当我点击它时动画,在该按钮功能内,有通知生成是否播放该歌曲。我要做的是“当我点击它时开始动画,并在播放歌曲时停止动画”。我无法停止动画。

这是我的代码

func MPMoviePlayerPlaybackStateDidChange(notification: NSNotification){

    if (User.postPlayer.playbackState == MPMoviePlaybackState.Playing)
    { //playing

        println("playinggggg")
        buttonsender?.layer.removeAllAnimations()
    }
    else if (User.postPlayer.playbackState == MPMoviePlaybackState.Stopped)
    { //stopped
        println("stopped")
    }else if (User.postPlayer.playbackState == MPMoviePlaybackState.Paused)
    {
        println("paused")
    }else if (User.postPlayer.playbackState == MPMoviePlaybackState.Interrupted)
    {
        println("interupted")
    }else if (User.postPlayer.playbackState == MPMoviePlaybackState.SeekingForward)
    {
        println("skip forward")
    }else if (User.postPlayer.playbackState == MPMoviePlaybackState.SeekingBackward)
    {
        println("skip before")
    }

}


func playPost(sender: UIButton){

    NSNotificationCenter.defaultCenter().addObserver(self,

        selector: "MPMoviePlayerPlaybackStateDidChange:",

        name: MPMoviePlayerPlaybackStateDidChangeNotification,

        object: nil)

    animateButton(sender)

    User.postPlayer.stop()
    var songUrl = sender.layer.valueForKey("songUrl") as? String
    println(songUrl)

    clickedIndex = sender.layer.valueForKey("index") as? Int
    sender.setTitle("II", forState: UIControlState.Normal)
    User.postPlayer.contentURL = NSURL(string: songUrl!)
    User.postPlayer.play()

    buttonsender = sender

}

func animateButton(sender: UIButton) {
    let defaultDuration = 2.0
    let defaultDamping = 0.20
    let defaultVelocity = 6.0

    sender.transform = CGAffineTransformMakeScale(0.1, 0.1)

    UIView.animateWithDuration(defaultDuration,
        delay: 0,
        usingSpringWithDamping: CGFloat(defaultDamping),
        initialSpringVelocity: CGFloat(defaultVelocity),
        options: UIViewAnimationOptions.AllowUserInteraction,
        animations: {
            sender.transform = CGAffineTransformIdentity
        },
        completion: { finished in
            self.animateButton(sender)
        }
    )
}

buttonsender是一个全局变量。 如何在播放歌曲时停止播放动画? 或者,如果任何人对加载有更好的解决方案,我会试着看一下,只要给出任何建议。无论用目标c还是swift编写。

1 个答案:

答案 0 :(得分:0)

试试这个应该停止

[CATransaction begin];
[theView.layer removeAllAnimations];
[CATransaction commit];