2017-07-08 83 views

回答

0

您的按鈕連接到一個@IBOutlet並調用它button,那麼你就可以做到這一點的把這個代碼裏面viewDidLoad方法:

button.layer.shadowColor = UIColor.red.cgColor 
button.layer.shadowRadius = 30.0 
button.layer.shadowOpacity = 0.9 
button.layer.shadowOffset = CGSize.zero 
button.layer.masksToBounds = false 

,你可以改變的方式定製你喜歡。祝你好運!

+0

謝謝,已經很接近了,但它並沒有調整觸摸就突出了,但除了Touc觸摸時反而增加了,當沒有碰過陰影和第二電熱h突出顯示本身。我特別專注於Touch On Highlight。 – user4806509

+0

(注意,這個建議有助於學習不同的技術,所以非常感謝。) – user4806509

+1

我很高興它幫助我們學習新東西! –

0

從我以前的答案Need a Glowing Animation around a button我一直在努力使用代碼,加入一些自定義,以解決您的答案,這裏是結果,如果設置animateAllways = false這個自定義按鈕的行爲,因爲你需要

enter image description here

// 
// GlowingButton.swift 
// NavigationButtonRotateQuestion 
// 
// Created by Reinier Melian on 01/07/2017. 
// Copyright © 2017 Pruebas. All rights reserved. 
// 

import UIKit 

@IBDesignable 
class GlowingButton: UIButton { 

    @IBInspectable var animDuration : CGFloat = 3 
    @IBInspectable var cornerRadius : CGFloat = 5 
    @IBInspectable var maxGlowSize : CGFloat = 10 
    @IBInspectable var minGlowSize : CGFloat = 0 
    @IBInspectable var glowColor : UIColor = nil ?? UIColor.red 
    @IBInspectable var animateAllways : Bool = false 
    fileprivate var animating : Bool = false 

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

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 

    override func awakeFromNib() { 
     super.awakeFromNib() 
     self.contentScaleFactor = UIScreen.main.scale 
     self.layer.masksToBounds = false 

     if(self.animateAllways){ 
      self.setupButtonForContinueAnimation() 
      self.startAnimation() 
     }else{ 
      self.setupButton() 
     } 
    } 

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 

     if(!self.animateAllways){ 
     let layerAnimation = CABasicAnimation(keyPath: "shadowRadius") 
     layerAnimation.fromValue = minGlowSize 
     layerAnimation.toValue = maxGlowSize 
     layerAnimation.isAdditive = false 
     layerAnimation.duration = CFTimeInterval(animDuration/2) 
     layerAnimation.fillMode = kCAFillModeForwards 
     layerAnimation.isRemovedOnCompletion = false 
     self.layer.add(layerAnimation, forKey: "addGlowing") 
     } 
    } 

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 

    } 

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 

     if(!self.animateAllways){ 
     let layerAnimation = CABasicAnimation(keyPath: "shadowRadius") 
     layerAnimation.fromValue = maxGlowSize 
     layerAnimation.toValue = minGlowSize 
     layerAnimation.isAdditive = false 
     layerAnimation.duration = CFTimeInterval(animDuration/2) 
     layerAnimation.fillMode = kCAFillModeForwards 
     layerAnimation.isRemovedOnCompletion = false 
     self.layer.add(layerAnimation, forKey: "removeGlowing") 
     } 
    } 

    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) { 

     if(!self.animateAllways){ 
     let layerAnimation = CABasicAnimation(keyPath: "shadowRadius") 
     layerAnimation.fromValue = maxGlowSize 
     layerAnimation.toValue = minGlowSize 
     layerAnimation.isAdditive = false 
     layerAnimation.duration = CFTimeInterval(animDuration/2) 
     layerAnimation.fillMode = kCAFillModeForwards 
     layerAnimation.isRemovedOnCompletion = false 
     self.layer.add(layerAnimation, forKey: "removeGlowing") 
     } 
    } 

    func setupButton() 
    { 
     self.layer.cornerRadius = cornerRadius 
     self.layer.shadowPath = CGPath(roundedRect: self.bounds, cornerWidth: cornerRadius, cornerHeight: cornerRadius, transform: nil) 
     self.layer.shadowRadius = 0 
     self.layer.shadowColor = self.glowColor.cgColor 
     self.layer.shadowOffset = CGSize.zero 
     self.layer.shadowOpacity = 1 
    } 

    func setupButtonForContinueAnimation() 
    { 
     self.setupButton() 
     self.layer.shadowRadius = maxGlowSize 
    } 

    func startAnimation() 
    { 
     let layerAnimation = CABasicAnimation(keyPath: "shadowRadius") 
     layerAnimation.fromValue = maxGlowSize 
     layerAnimation.toValue = minGlowSize 
     layerAnimation.autoreverses = true 
     layerAnimation.isAdditive = false 
     layerAnimation.duration = CFTimeInterval(animDuration/2) 
     layerAnimation.fillMode = kCAFillModeForwards 
     layerAnimation.isRemovedOnCompletion = false 
     layerAnimation.repeatCount = .infinity 
     self.layer.add(layerAnimation, forKey: "glowingAnimation") 

    } 

} 

希望這有助於你

+0

@ user4806509我的答案是否解決了您的問題? –

+0

謝謝,併爲延遲感到抱歉,這已經向我展示了一些不同的技術,但它不會調整「Touch On Highlight」,而是添加一個單獨的圖層。我特別專注於調整「Touch On Highlight」的內置發光。 – user4806509

+1

@ user4806509我不知道突出顯示的內置輝光是否可以修改,無論如何,如果你解決這個讓我知道學習它 –