的UIButton
控制有一个属性“显示触摸就突出了”可以在故事板进行设置。选中此选项后,触摸UIButton
,UIButton
文字上将显示白光。
问:
我怎么能触摸时调整UIButton
光晕的半径大小?
的UIButton
控制有一个属性“显示触摸就突出了”可以在故事板进行设置。选中此选项后,触摸UIButton
,UIButton
文字上将显示白光。
问:
我怎么能触摸时调整UIButton
光晕的半径大小?
您的按钮连接到一个@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
,你可以改变的方式定制你喜欢。祝你好运!
从我以前的答案Need a Glowing Animation around a button我一直在努力使用代码,加入一些自定义,以解决您的答案,这里是结果,如果设置animateAllways = false
这个自定义按钮的行为,因为你需要
//
// 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")
}
}
希望这有助于你
@ user4806509我的答案是否解决了您的问题? –
谢谢,并为延迟感到抱歉,这已经向我展示了一些不同的技术,但它不会调整“Touch On Highlight”,而是添加一个单独的图层。我特别专注于调整“Touch On Highlight”的内置发光。 – user4806509
@ user4806509我不知道突出显示的内置辉光是否可以修改,无论如何,如果你解决这个让我知道学习它 –
谢谢,已经很接近了,但它并没有调整触摸就突出了,但除了Touc触摸时反而增加了,当没有碰过阴影和第二电热h突出显示本身。我特别专注于Touch On Highlight。 – user4806509
(注意,这个建议有助于学习不同的技术,所以非常感谢。) – user4806509
我很高兴它帮助我们学习新东西! –