我想为按钮标题添加渐变,并且按钮还应该具有闪烁/闪烁之类的动画。通过添加此项,点击手势不起作用
我创建了一个名为'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")
}
}
Apple在其Animation文档中描述:
“在iOS中,动画广泛用于重新定位视图,更改它们的大小,将它们从视图层次结构中删除,然后隐藏它们。“
View Programming Guide - Apple
本文继续着重强调使用核心动画而不是UIView动画来获得更详细的动画。
“在您想要执行更复杂的动画的地方,或者UIView类不支持的动画,可以使用Core动画和视图的基础层来创建动画。由于视图和图层对象错综复杂地链接在一起,对视图图层的更改会影响视图本身。“
基本上就是这样,您可以操纵视图的图层来更改视图。我认为在存在Core Animations为此目的时直接操作视图是不明智的。因此,您不能(也可能不应该)对具有UIAnimations的UIView进行操作,而对于具有Core Animations的图层可以进行操作。
您必须使用CoreAnimation
制作动画。因此,您的blinkAnimation()
方法将是,
func blinkAnimation(){
let fadein = CABasicAnimation(keyPath: "opacity")
fadein.fromValue = 1.0
fadein.toValue = 0.0
fadein.duration = 0.5
fadein.autoreverses = true
fadein.repeatCount = HUGE
maskingView.layer.add(fadein, forKey: "opacity")
}
这里是完整代码,
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 as! UIGestureRecognizerDelegate
tap.numberOfTapsRequired = 1
self.maskingView.isUserInteractionEnabled = true
self.maskingView.addGestureRecognizer(tap)
maskingView.layer.insertSublayer(gradient, at: 0)
点击手势选择器方法是,
@objc func goToOtherView(gesture: UITapGestureRecognizer) {
print("tap gesture selector called")
}
遮罩,动画和tapGesture / tapGesture /按钮单击(touchUpInside)不能同时工作
所以为了添加渐变使用
btnGradient.setTitleColor(UIColor(patternImage: UIImage(named: "gradientImage")!), for: .normal)
现在添加动画并在按钮上使用use touchUpInside事件