我有一个在视图上执行动画的功能。我想为这个函数实现一个完成处理程序,它将在动画完成后调用。
在ViewController中......
hudView.hide(animated: true, myCompletionHandler: {
// Animation is complete
})
在HudView类中......
func hide(animated: Bool, myCompletionHandler: () -> Void) {
if animated {
transform = CGAffineTransform(scaleX: 0.7, y: 0.7)
UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: [], animations: {
self.alpha = 0
self.transform = CGAffineTransform.identity
}, completion: nil) // I want to run 'myCompletionHandler' in this completion handler
}
}
我尝试了很多东西,却找不到正确的语法:
}, completion: myCompletionHandler)
将非转义参数'myCompletionHandler'传递给期望的函数 @escaping关闭
}, completion: myCompletionHandler())
无法将'Void'类型的值转换为预期的参数类型'((Bool) - >空隙)?“
}, completion: { myCompletionHandler() })
关闭使用非转义参数'myCompletionHandler'可能允许它 逸出
作为一个快速的新手,这些错误消息对我来说并不是很重要,我似乎无法找到正确方法的任何示例。
将myCompletionHandler
传递给.animate
完成处理程序的正确方法是什么?
答案 0 :(得分:4)
如果要将自己的闭包作为输入参数传递给 <kendo-grid
[kendoGridGroupBinding]="data"
[groupable]="true"
[sortable]="true"
[pageSize]="80"
[rowHeight]="35"
[height]="460"
scrollable="virtual"
(groupChange)="groupChange($event)"
>
<kendo-grid-column field="id" [width]="80" title="ID"></kendo-grid-column>
<kendo-grid-column field="firstName" title="First Name" [width]="130"></kendo-grid-column>
<kendo-grid-column field="lastName" title="Last Name" [width]="130"></kendo-grid-column>
<kendo-grid-column field="city" title="City" [width]="130"></kendo-grid-column>
<kendo-grid-column field="title" title="Title" [width]="180"></kendo-grid-column>
</kendo-grid>
public groupChange(groups: GroupDescriptor[]): void {
console.log('groupChange', groups);
this.collapseAllGroups(groups);
}
public collapseAllGroups(groups): void {
if (this.grid && groups.length > 0) {
for (let i = 0; i < this.data.length; i++) {
this.grid.collapseGroup(i.toString());
}
this.collapsed = true;
}
}
,则需要匹配闭包的类型,因此UIView.animate
的类型必须为myCompletionHandler
,就像((Bool) -> ())?
。
completion
您可以这样称呼它:
func hide(animated: Bool, myCompletionHandler: ((Bool) -> ())?) {
if animated {
transform = CGAffineTransform(scaleX: 0.7, y: 0.7)
UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: [], animations: {
self.alpha = 0
self.transform = CGAffineTransform.identity
}, completion: myCompletionHandler) // I want to run 'myCompletionHandler' in this completion handler
}
}
答案 1 :(得分:0)
这是在UIView.animate中使用完成的方法:
func hide(animated: Bool, myCompletionHandler: () -> Void) {
if animated {
transform = CGAffineTransform(scaleX: 0.7, y: 0.7)
UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: [], animations: {
self.alpha = 0
self.transform = CGAffineTransform.identity
}, completion: { (success) in
myCompletionHandler()
})
}
}
答案 2 :(得分:0)
You can create your function as,
func hide(_ animated:Bool, completionBlock:((Bool) -> Void)?){
}
And you can call it as,
self.hide(true) { (success) in
// callback here
}