我想为按钮标题添加渐变,并且按钮也应该具有闪烁/闪烁之类的动画。通过添加此项,点击手势不起作用
我创建了一个名为“ maskingView”的视图,并使用情节提要在该视图内名为“ btnGradient”的按钮
let gradient = CAGradientLayer()
gradient.colors = [UIColor.red.cgColor, UIColor.blue.cgColor]
gradient.startPoint = CGPoint(x: 0.0, y: 0.5)
gradient.endPoint = CGPoint(x: 1.0, y: 0.5)
gradient.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 30)
maskingView = btnGradient
// function for animation
blinkAnimation()
let tap = UITapGestureRecognizer(target: self, action: #selector(self.goToOtherView(gesture:)))
tap.delegate = self
tap.numberOfTapsRequired = 1
self. maskingView.isUserInteractionEnabled = true
self. maskingView.addGestureRecognizer(tap)
maskingView.layer.insertSublayer(gradient, at: 0)
func blinkAnimation(){
maskingView.alpha = 1.0
UIView.animate(withDuration: 0.5, delay: 0.0, options: [.repeat, .autoreverse, .allowUserInteraction], animations: {
self.maskingView.alpha = 0.0
}, completion: nil)
}
最佳答案
由于您正在设置self.maskingView.alpha = 0.0
,因此无法正常工作。如果将alpha设置为0
,则系统会将其视为隐藏视图!尝试通过设置类似Alpha的方式
self.maskingView.alpha = 0.1
样例代码:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var maskingView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let gradient = CAGradientLayer()
gradient.colors = [UIColor.red.cgColor, UIColor.blue.cgColor]
gradient.startPoint = CGPoint(x: 0.0, y: 0.5)
gradient.endPoint = CGPoint(x: 1.0, y: 0.5)
gradient.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 30)
// function for animation
blinkAnimation()
let tap = UITapGestureRecognizer(target: self, action: #selector(self.goToOtherView(gesture:)))
tap.numberOfTapsRequired = 1
self.maskingView.isUserInteractionEnabled = true
self.maskingView.addGestureRecognizer(tap)
maskingView.layer.insertSublayer(gradient, at: 0)
// Do any additional setup after loading the view.
}
func blinkAnimation(){
maskingView.alpha = 1.0
UIView.animate(withDuration: 0.5, delay: 0.0, options: [.repeat, .autoreverse, .allowUserInteraction], animations: {
self.maskingView.alpha = 0.1
}, completion: nil)
}
@objc func goToOtherView(gesture:Any) {
print("tap")
}
}
关于ios - 当按钮具有动画和渐变蒙版时,点击手势不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59817398/