Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何为动画添加完成?_Ios_Swift_Animation - Fatal编程技术网

Ios 如何为动画添加完成?

Ios 如何为动画添加完成?,ios,swift,animation,Ios,Swift,Animation,嘿,我想创建一个设置游戏应用程序,现在我想我的卡(UIView)移动到一个新位置。动画结束后,我希望此视图从超级视图中删除 func btnUp(card: CardSubview, frame: CGRect) { let newPosition = CGRect(x: (self.superview?.frame.minX)!, y: (self.superview?.frame.maxY)!, width: card.bounds.width, height: ca

嘿,我想创建一个设置游戏应用程序,现在我想我的卡(UIView)移动到一个新位置。动画结束后,我希望此视图从超级视图中删除

   func btnUp(card: CardSubview, frame: CGRect) {

        let newPosition = CGRect(x: (self.superview?.frame.minX)!, y: (self.superview?.frame.maxY)!, width: card.bounds.width, height: card.bounds.height)
        //UIView.animate(withDuration: 3.0, animations: {card.frame = newPosition})
        UIView.animate(withDuration: 3.0, animations: {card.frame = newPosition}, completion: {if card.frame == newPosition {card.removeFromSuperview()}})

    }
这是可行的,但如果我想添加一个完成,我会得到以下错误:

无法将类型为“()->()”的值转换为所需的参数类型 “((Bool)->Void”?”**

那么我做错了什么呢?

试试这个:

您需要设置
完成块
变量

func btnUp(card: CardSubview, frame: CGRect) {

    let newPosition = CGRect(x: (self.superview?.frame.minX)!, y: (self.superview?.frame.maxY)!, width: card.bounds.width, height: card.bounds.height)
    //UIView.animate(withDuration: 3.0, animations: {card.frame = newPosition})
    UIView.animate(withDuration: 3.0, animations: {
        card.frame = newPosition
        }, completion: { finish in
             if card.frame == newPosition {card.removeFromSuperview()

        }})
}
说:

完成

动画序列结束时要执行的块对象。这 块没有返回值,只接受一个 指示动画是否在开始之前实际完成 已调用完成处理程序

因此,您需要将该布尔参数指定给完成处理程序:

UIView.animate(withDuration: 3.0, animations: {card.frame = newPosition}, completion: { finish in
    if card.frame == newPosition {
         card.removeFromSuperview()
    }
})
可能重复的
UIView.animate(withDuration: 3.0, animations: {card.frame = newPosition}, completion: { finish in
    if card.frame == newPosition {
         card.removeFromSuperview()
    }
})