UIVisualEffectView 및 / 또는 UIBlurEffect를 페이드 인 및 아웃하는 방법은 무엇입니까?
UIBlurEffect를 사용하여 UIVisualEffectsView를 페이드 인 및 아웃하고 싶습니다.
var blurEffectView = UIVisualEffectView()
blurEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .dark))
나는 UIButton
그것을 페이드 인하 기 위해 호출하는 함수 내에서 일반 애니메이션을 사용하고 페이드 아웃과 동일하지만 .alpha = 0
& hidden = true
:
blurEffectView.hidden = false
UIView.animate(withDuration: 1, delay: 0, options: .curveEaseOut) {
self.blurEffectView.alpha = 1
}
이제 양방향으로 페이딩이 작동하지만 페이드 아웃 할 때 오류가 발생 합니다 .
<UIVisualEffectView 0x7fdf5bcb6e80>
불투명도에 애니메이션을 적용하라는 요청을 받고 있습니다. 이렇게하면 불투명도가 1이 될 때까지 효과가 깨져 보입니다.
질문
어떻게 성공적으로 페이드 할 수 UIVisualEffectView
없이 들락날락 위반 을하고 페이딩 전환을 가지고?
노트
- 나는 그것을
UIVisualEffectView
안으로 넣고 그것을UIView
페이드 하려고 노력했지만 , 성공하지 못했다.
나는 이것이 iOS9에서 새로운 것이라고 생각하지만 이제 UIVisualEffectView
애니메이션 블록 내부 의 효과를 설정할 수 있습니다 .
let overlay = UIVisualEffectView()
// Put it somewhere, give it a frame...
UIView.animate(withDuration: 0.5) {
overlay.effect = UIBlurEffect(style: .light)
}
nil
제거 하려면 로 설정 하십시오.
매우 중요-시뮬레이터에서 이것을 테스트 할 때 이것이 작동하도록 시뮬레이터의 그래픽 품질 재정의를 고품질로 설정해야합니다.
애플 문서는 (현재) 상태 ...
UIVisualEffectView 클래스를 사용하는 경우 1보다 작은 알파 값을 사용하지 마십시오.
과
시각적 효과보기 또는 수퍼 뷰에서 알파를 1 미만으로 설정하면 많은 효과가 잘못 보이거나 전혀 표시되지 않습니다.
여기서 중요한 맥락이 누락되었다고 생각합니다 ...
영구 뷰를 위해 1보다 작은 알파 값을 피하는 것이 목적이라고 제안합니다. 내 겸손한 의견으로는 이것은 뷰의 애니메이션에는 적용되지 않습니다.
내 요점은 1보다 작은 알파 값이 애니메이션에 허용된다는 점입니다.
터미널 메시지는 다음을 나타냅니다.
UIVisualEffectView는 불투명도를 애니메이션하도록 요청됩니다. 이렇게하면 불투명도가 1이 될 때까지 효과가 깨져 보입니다.
이것을주의 깊게 읽으면 효과가 깨지는 것처럼 보일 것 입니다. 이것에 대한 나의 요점 :
- 명백한 단절은 변하지 않는 지속적인 뷰에만 정말로 중요합니다.
UIVisualEffect
알파 값이 1 미만인 지속적 / 변하지 않는 보기는 Apple이 의도 한 / 설계 한대로 표시되지 않습니다. 과- 터미널의 메시지는 오류가 아니라 경고 일뿐입니다.
내 문제를 해결하는 데 도움이 된 @jrturton의 답변을 확장하려면 다음을 추가합니다.
페이드 아웃하려면 UIVisualEffect
다음 (Objective-C) 코드를 사용하십시오.
UIView.animateWithDuration(1.0, animations: {
// EITHER...
self.blurEffectView.effect = UIBlurEffect(nil)
// OR...
self.blurEffectView.alpha = 0
}, completion: { (finished: Bool) -> Void in
self.blurEffectView.removeFromSuperview()
} )
두 가지 방법을 모두 성공적으로 사용합니다 : effect
속성을 nil
로 설정하고 alpha
속성을로 설정합니다 0
.
, 설정하는 것을주의 effect
하기 nil
, 설정하면서, 애니메이션의 끝에서 (더 나은 설명의 결핍)는 "좋은 플래시"를 생성 alpha
하는 0
원활한 전환을 만듭니다.
(구문 오류가 있으면 알려주세요 ... obj-c로 작성합니다.)
다음은 Swift 3을 사용하여 iOS10 및 이전 버전 모두에서 작동하는 솔루션입니다.
extension UIVisualEffectView {
func fadeInEffect(_ style:UIBlurEffectStyle = .light, withDuration duration: TimeInterval = 1.0) {
if #available(iOS 10.0, *) {
let animator = UIViewPropertyAnimator(duration: duration, curve: .easeIn) {
self.effect = UIBlurEffect(style: style)
}
animator.startAnimation()
}else {
// Fallback on earlier versions
UIView.animate(withDuration: duration) {
self.effect = UIBlurEffect(style: style)
}
}
}
func fadeOutEffect(withDuration duration: TimeInterval = 1.0) {
if #available(iOS 10.0, *) {
let animator = UIViewPropertyAnimator(duration: duration, curve: .linear) {
self.effect = nil
}
animator.startAnimation()
animator.fractionComplete = 1
}else {
// Fallback on earlier versions
UIView.animate(withDuration: duration) {
self.effect = nil
}
}
}
}
이 요점 을 확인 하여 예제 사용법을 찾을 수도 있습니다.
해결 방법- UIVisualEffectView
컨테이너보기에 넣고 alpha
해당 컨테이너의 속성을 변경 합니다. 이 접근 방식은 iOS 9에서 완벽하게 작동합니다. iOS 10에서는 더 이상 작동하지 않는 것 같습니다.
콘솔의 경고 외에 문제없이 시각 효과보기의 알파를 변경할 수 있습니다. 보기가 흐릿하지 않고 단순히 부분적으로 투명하게 나타날 수 있습니다. 그러나 애니메이션 중에 알파 만 변경하는 경우 일반적으로 문제가되지 않습니다.
이로 인해 앱이 충돌하거나 거부되지 않습니다. 실제 장치 (또는 8 개)에서 테스트하십시오. 외관과 성능에 만족한다면 괜찮습니다. Apple은 알파 값이 1 인 시각 효과보기만큼 보이거나 작동하지 않을 수 있다고 경고했습니다.
정적 기본 뷰의 스냅 샷을 찍고 블러 뷰의 불투명도를 건드리지 않고 페이드 인 및 페이드 아웃 할 수 있습니다. blurView의 ivar 가정 :
func addBlur() {
guard let blurEffectView = blurEffectView else { return }
//snapShot = UIScreen.mainScreen().snapshotViewAfterScreenUpdates(false)
let snapShot = self.view.snapshotViewAfterScreenUpdates(false)
view.addSubview(blurEffectView)
view.addSubview(snapShot)
UIView.animateWithDuration(0.25, animations: {
snapShot.alpha = 0.0
}, completion: { (finished: Bool) -> Void in
snapShot.removeFromSuperview()
} )
}
func removeBlur() {
guard let blurEffectView = blurEffectView else { return }
let snapShot = self.view.snapshotViewAfterScreenUpdates(false)
snapShot.alpha = 0.0
view.addSubview(snapShot)
UIView.animateWithDuration(0.25, animations: {
snapShot.alpha = 1.0
}, completion: { (finished: Bool) -> Void in
blurEffectView.removeFromSuperview()
snapShot.removeFromSuperview()
} )
}
UIVisualEffectView를 페이드 인하려면-ios10의 경우 UIViewPropertyAnimator를 사용하십시오.
UIVisualEffectView *blurEffectView = [[UIVisualEffectView alloc] initWithEffect:nil];
blurEffectView.frame = self.view.frame;
blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
UIView *blackUIView = [[UIView alloc]initWithFrame:self.view.frame];
[bacgroundImageView addSubview:blackUIView];
[blackUIView addSubview:blurEffectView];
UIViewPropertyAnimator *animator = [[UIViewPropertyAnimator alloc] initWithDuration:4.f curve:UIViewAnimationCurveLinear animations:^{
[blurEffectView setEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleDark]];
}];
그런 다음 백분율을 설정할 수 있습니다.
[animator setFractionComplete:percent];
ios9의 경우 알파 구성 요소를 사용할 수 있습니다.
The alpha of the UIVisualEffectView always has to be 1. I think you can achieve the effect by setting the alpha of the background color.
Source : https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIVisualEffectView/index.html
I make an uiview which alpha is 0 and add blurview as subview of that. So i can hide/show or rounding corners it with animation.
I ended up with the following solution, using separate animations for the UIVisualEffectView
and the contents. I used the viewWithTag()
method to get a reference to the UIView
inside the UIVisualEffectView
.
let blurEffectView = UIVisualEffectView()
// Fade in
UIView.animateWithDuration(1) { self.blurEffectView.effect = UIBlurEffect(style: .Light) }
UIView.animateWithDuration(1) { self.blurEffectView.viewWithTag(1)?.alpha = 1 }
// Fade out
UIView.animateWithDuration(1) { self.blurEffectView.effect = nil }
UIView.animateWithDuration(1) { self.blurEffectView.viewWithTag(1)?.alpha = 0 }
I would prefer the single animation changing the alpha, but this avoids the error and seems to work just as well.
I just had this problem and the way I got around it was to house the UIVisualEffectsView in a UIView, and animate that UIView's alpha.
This worked well, except that it as soon as the alpha changed below 1.0 it turned to a solid white and looked very jarring. In order to get around this, you must set the UIView's layer property containerView.layer.allowsGroupOpacity = false
and this will prevent it from flashing white.
Now you can animate in/fade out the UIView containing the visual effects view and any other subviews using it's alpha property and not have to worry about any graphical glitches or it logging a warning message.
_visualEffectView.contentView.alpha = 0;
To change the alpha of UIVisualEffectView, you should change the contentView of _visualEffectView.If you change alpha of _visualEffectView, you will get this
<UIVisualEffectView 0x7ff7bb54b490> is being asked to animate its opacity. This will cause the effect to appear broken until opacity returns to 1.
Usually, I only want to animate a blur when I'm presenting a view controller over the screen and want to blur the presenting view controller. Here's an extension that adds blur() and unblur() to a view controller in order to facilitate that:
extension UIViewController {
func blur() {
// Blur out the current view
let blurView = UIVisualEffectView(frame: self.view.frame)
self.view.addSubview(blurView)
UIView.animate(withDuration:0.25) {
blurView.effect = UIBlurEffect(style: .light)
}
}
func unblur() {
for childView in view.subviews {
guard let effectView = childView as? UIVisualEffectView else { continue }
UIView.animate(withDuration: 0.25, animations: {
effectView.effect = nil
}) {
didFinish in
effectView.removeFromSuperview()
}
}
}
}
You can of course make this more robust by letting the user choose the effect style, modify the duration, call something when the animation is completed, tag the added visual effect view in blur() to ensure it's the only one removed when you unblur(), etc., but I haven't found the need to do these things so far, since this tends to be a "fire and forget" type of operation.
based on @cc's answer i modified his extension to blur a view
extension UIView { func blur() { // Blur out the current view let blurView = UIVisualEffectView(frame: self.bounds) self.addSubview(blurView) UIView.animate(withDuration:0.25) { blurView.effect = UIBlurEffect(style: .dark) } } func unblur() { for childView in subviews { guard let effectView = childView as? UIVisualEffectView else { continue } UIView.animate(withDuration: 2.5, animations: { effectView.effect = nil }) { didFinish in effectView.removeFromSuperview() } } } }
Improving @Tel4tel and @cc response, here is an extension with parameters and a brief explanation.
extension UIView {
// Perform a blur animation in the whole view
// Effect tone can be .light, .dark, .regular...
func blur(duration inSeconds: Double, effect tone: UIBlurEffectStyle) {
let blurView = UIVisualEffectView(frame: self.bounds)
self.addSubview(blurView)
UIView.animate(withDuration: inSeconds) {
blurView.effect = UIBlurEffect(style: tone)
}
}
// Perform an unblur animation in the whole view
func unblur(duration inSeconds: Double) {
for childView in subviews {
guard let effectView = childView as? UIVisualEffectView else { continue
}
UIView.animate(withDuration: inSeconds, animations: {
effectView.effect = nil
}){
didFinish in effectView.removeFromSuperview()
}
}
}
}
Then you can use it like: view.blur(duration: 5.0, effect: .light)
or view.unblur(duration: 3.0)
Remember to NOT use it in viewDidLoad()
as it will override the animation. Also, when running on a Simulator, turn the graphics to the Higher level to be able to see the animation (Debug > Graphics Quality Override > High Quality).
'program story' 카테고리의 다른 글
전단지-기존 마커를 찾고 마커를 삭제하는 방법은 무엇입니까? (0) | 2020.09.13 |
---|---|
Eclipse가 시작되지 않음-Java 가상 머신을 찾을 수 없음 (0) | 2020.09.13 |
List (of T)와 Collection (of T)의 차이점은 무엇입니까? (0) | 2020.09.13 |
Qt 이벤트 및 신호 / 슬롯 (0) | 2020.09.13 |
node-jwt-simple이있는 passport-local (0) | 2020.09.12 |