스위프트의 블록(애니메이션 위드 듀레이션:애니메이션:완료:)
스위프트에서 블록을 작동시키는데 어려움을 겪고 있습니다.완료 블록 없이 작동한 예는 다음과 같습니다.
UIView.animateWithDuration(0.07) {
self.someButton.alpha = 1
}
또는 트레일링 클로저가 없는 경우:
UIView.animateWithDuration(0.2, animations: {
self.someButton.alpha = 1
})
완료 블록을 추가하려고 하면 작동하지 않습니다.
UIView.animateWithDuration(0.2, animations: {
self.blurBg.alpha = 1
}, completion: {
self.blurBg.hidden = true
})
자동 완성은 나에게 줍니다.completion: ((Bool) -> Void)?어떻게 해야 할지는 잘 모르겠어요후행 폐쇄도 시도했지만 동일한 오류가 발생했습니다.
! Could not find an overload for 'animateWithDuration that accepts the supplied arguments
Swift 3/4 업데이트:
// This is how I do regular animation blocks
UIView.animate(withDuration: 0.2) {
<#code#>
}
// Or with a completion block
UIView.animate(withDuration: 0.2, animations: {
<#code#>
}, completion: { _ in
<#code#>
})
완료 블록에 후행 마감을 사용하지 않는 이유는 명확성이 부족하다고 생각하기 때문입니다. 하지만 마음에 드신다면 아래에서 트레버의 답변을 보실 수 있을 것입니다.
의 완료 매개변수animateWithDuration는 하나의 부울 파라미터를 사용하는 블록을 사용합니다.Swift에서는 Obj-C 블록에서와 마찬가지로 클로저가 취할 파라미터를 지정해야 합니다.
UIView.animateWithDuration(0.2, animations: {
self.blurBg.alpha = 1
}, completion: {
(value: Bool) in
self.blurBg.hidden = true
})
여기서 중요한 부분은.(value: Bool) in. 이것은 컴파일러에게 이 클로저가 'value'라는 Boole을 가져가고 Void를 반환한다는 것을 알려줍니다.
참고로, 만약 당신이 불을 반환하는 종결문을 쓰고 싶다면, 구문은
{(value: Bool) -> bool in
//your stuff
}
완료가 정확합니다. 클로저가 다음을 수락해야 합니다.Bool매개 변수:(Bool) -> ().해라
UIView.animate(withDuration: 0.2, animations: {
self.blurBg.alpha = 1
}, completion: { finished in
self.blurBg.hidden = true
})
다음 항목과 함께 자체적으로 밑줄을 그립니다.in키워드가 입력을 무시합니다.
스위프트 2
UIView.animateWithDuration(0.2, animations: {
self.blurBg.alpha = 1
}, completion: { _ in
self.blurBg.hidden = true
})
스위프트 3,4,5
UIView.animate(withDuration: 0.2, animations: {
self.blurBg.alpha = 1
}, completion: { _ in
self.blurBg.isHidden = true
})
위의 수락된 답변을 바탕으로 위의 나의 해결책이 있습니다.그것은 시야를 어둡게 하고 거의 보이지 않는 곳에 숨깁니다.
스위프트 2
func animateOut(view:UIView) {
UIView.animateWithDuration (0.25, delay: 0.0, options: UIViewAnimationOptions.CurveLinear ,animations: {
view.layer.opacity = 0.1
}, completion: { _ in
view.hidden = true
})
}
스위프트 3,4,5
func animateOut(view: UIView) {
UIView.animate(withDuration: 0.25, delay: 0.0, options: UIView.AnimationOptions.curveLinear ,animations: {
view.layer.opacity = 0.1
}, completion: { _ in
view.isHidden = true
})
}
여기 있습니다, 이것은 편집할 것입니다.
스위프트 2
UIView.animateWithDuration(0.3, animations: {
self.blurBg.alpha = 1
}, completion: {(_) -> Void in
self.blurBg.hidden = true
})
스위프트 3,4,5
UIView.animate(withDuration: 0.3, animations: {
self.blurBg.alpha = 1
}, completion: {(_) -> Void in
self.blurBg.isHidden = true
})
Bool 영역을 밑줄로 만든 이유는 해당 값을 사용하지 않기 때문입니다. 필요한 경우 (_)을 (값 : Bool)로 대체할 수 있습니다.
때로는 상황에 따라 다른 방식으로 애니메이션을 생성하기 위해 변수를 던집니다.당신에게 필요한 것은
let completionBlock : (Bool) -> () = { _ in
}
또는 동일하게 장황하게 사용할 수도 있습니다.
let completionBlock = { (_:Bool) in
}
하지만 어떤 경우에도, 당신은 당신이 그를Bool어딘가에.
SWIFT 3.x + 4.x
업데이트하고 간소화하고 싶습니다.
아래의 예는 어떠한 경우에도 구현됩니다.view그것은 천천히 그리고 그것이 완전히 투명할 때, 그것은 부모로부터 스스로를 제거합니다.view
ok변수는 항상 반환됩니다.true애니메이션 종료와 함께.
alpha = 1
UIView.animate(withDuration: 0.5, animations: {
self.alpha = 0
}) { (ok) in
print("Ended \(ok)")
self.removeFromSuperview()
}
언급URL : https://stackoverflow.com/questions/24071334/blocks-on-swift-animatewithdurationanimationscompletion
'programing' 카테고리의 다른 글
| 다음 형제/X를 선택하는 방법XPath를 사용한 ML 태그 (0) | 2023.10.12 |
|---|---|
| Amazon RDS MySQL 인스턴스의 성능이 매우 느림 (0) | 2023.10.12 |
| MySQL - 텍스트 대 CHAR 및 VARCHAR (0) | 2023.10.12 |
| 스프링 부트 내장 Tomcat 로그 (0) | 2023.10.12 |
| c / c++의 메모리에 있는 주소를 통해 함수를 호출하는 중 (0) | 2023.10.12 |