2017-04-04 13 views
0

버튼에 투명한 이미지 뒤에 투명 배경으로 버튼을 만들고을 만들려고합니다. myButton.imageView.image에는 이미지 세트 (myPNGWithTransparentBackground)가 있습니다.UIVisualEffectView를 UIButton의 투명한 imageView 이미지 뒤에 삽입

내가 꽤 간단 할 것이라고 생각하지만, 내가 상상보다 더 어려운 것으로 입증 ... 여기에 내가 무엇을 시도했다입니다 :

는 는
let frost = UIVisualEffectView(effect: UIBlurEffect(style: .light)) 
frost.frame = button.bounds 
frost.autoresizingMask = [.flexibleWidth, .flexibleHeight] 
// button.insertSubview(frost, at: 0) 
// button.insertSubview(frost, at: button.subviews.startIndex) 
// button.insertSubview(frost, at: button.subviews.endIndex) 
// button.imageView?.insertSubview(frost, at: (button.imageView?.subviews.endIndex)!) 
button.imageView?.insertSubview(frost, at: (button.imageView?.subviews.startIndex)!) 

는 상관없이 내가 insertSubview를 넣어 어디에, 항상 끝 UIButton의 imageView.image 앞에.

enter image description here

업데이트 내가 무엇을 시도했다의

Here's a Git repo. 하위 클래스는 "FrostyButton"이라고합니다.

+0

아마도 서브 클래 싱의 경로를 따라야합니다. 나는 * "내 욕망의 UIView 레이어 필요"* 프로토콜 및 확장 및 슈퍼 클래스를 사용하여 하위 클래스로뿐만 아니라 적합하지 않은 완벽한 예제라고 생각합니다. – dfd

+0

서브 클래 싱시와 동일한 동작입니다. 이것은 정말 간단해야 할 것 같습니다. 나는 정말로 바보 같은 일을하고 있다는 것을 확신합니다. – Adrian

+0

좋아요, 당신의 데모는 버튼 프레임에 의해 흐릿 해지는 파란색 배경을 가지고 있습니다. 그리고 블러 앞에서는 투명한 배경을 가진 이미지가 될 것입니다. 옳은? – dfd

답변

1

데모 프로젝트를 살펴본 후에는 UButton 이미지의 위에 imageView 을 추가해야합니다. 수정 된 코드는 다음과 같습니다. 필요에 따라 정리해주십시오! 있는 UIButton 이미지가 가능성이 깊이 "앞으로 가져 오기"로 내장되어 -

private var imageView2 = UIImageView() // declared globally 

private func makeItFrosty() { 
    let frost = UIVisualEffectView(effect: UIBlurEffect(style: .light)) 
    frost.frame = self.bounds 
    frost.autoresizingMask = [.flexibleWidth, .flexibleHeight] 
    if let imageView = imageView { 
     imageView.addSubview(frost) 
     // these three lines basically 'clone' the existing imageView and adds it on top of the 'frost' view 
     imageView2.image = imageView.image 
     imageView2.frame = imageView.frame 
     self.addSubview(imageView2) 
    } 
} 

난 당신이 뭔가를 할 필요가 너무 놀라지 아니에요.

+0

고마워요! 저는 버튼이 많지 않습니다. 실제 프로젝트에서 사용하는 pdf는 3 킬로바이트이므로 작동 할 것입니다. – Adrian

+0

오늘 아침 Github에서 프로젝트를 업데이트하여 FrostyButton 클래스가 완벽하게 작동합니다. 다시 감사합니다. – Adrian

0

문제가 무엇인지 모르겠지만 사용자 정의 버튼 클래스가 작동 중입니다 ... 버튼을 일부 바닐라 UIView와 일치시키고 UIView의 배경에서 알파를 자르려고 시도하지 않았습니다. 여기 제가 생각해 낸 수업이 있습니다.

UIVisualEffectViewUIView 안에 넣어 버렸습니다. 버튼 안에 붙어 있습니다. isEnabled은 표시 여부와 표시 여부를 전환합니다. 한 번에 하나씩 만 실행되는 여러 버튼이있는 클래스에서이 메서드를 사용합니다.

import UIKit 

@IBDesignable class FrostyButton: UIButton { 

    @IBInspectable override var isEnabled: Bool { 
     didSet { 
      if isEnabled { 
       self.visualEffectContainer.alpha = 1.0 
      } 
      else { 
       let animator = UIViewPropertyAnimator(duration: 0.5, curve: .linear, animations: { 
        self.visualEffectContainer.alpha = 0.0 
       }) 
       animator.startAnimation() 
      } 
     } 
    } 

    @IBInspectable var cornerRadius: CGFloat { 
     get { 
      return layer.cornerRadius 
     } 

     set { 
      layer.cornerRadius = newValue 
      layer.masksToBounds = newValue > 0 
     } 
    } 

    private let visualEffectContainer = UIView() 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     self.backgroundColor = .clear 
     makeItFrosty() 
    } 

    override init(frame: CGRect) { 
     super.init(frame: frame) 
    } 

    override func prepareForInterfaceBuilder() { 
     super.prepareForInterfaceBuilder() 
    } 

    private func makeItFrosty() { 

     let effectView = UIVisualEffectView(effect: UIBlurEffect(style: .light)) 
     effectView.frame = self.bounds 
     effectView.clipsToBounds = true 
     effectView.autoresizingMask = [.flexibleWidth, .flexibleHeight] 

     visualEffectContainer.frame = self.bounds 
     visualEffectContainer.clipsToBounds = true 
     visualEffectContainer.autoresizingMask = [.flexibleWidth, .flexibleHeight] 
     visualEffectContainer.addSubview(effectView) 
     visualEffectContainer.isUserInteractionEnabled = false 
     if imageView != nil { 
      self.insertSubview(visualEffectContainer, at: 0) 
     } 
    } 
}