gpt4 book ai didi

ios - 当按钮具有动画和渐变 mask 时,点击手势不起作用

转载 作者:行者123 更新时间:2023-11-29 05:09:31 29 4
gpt4 key购买 nike

我想为按钮标题添加渐变,并且按钮应该具有闪烁/闪烁等动画。添加此后,点击手势将不起作用

我使用 Storyboard创建了一个名为“maskingView”的 View ,并且按钮位于该名为“btnGradient”的 View 内

    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 那么系统会将其视为隐藏 View !一旦尝试设置类似 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 - 当按钮具有动画和渐变 mask 时,点击手势不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59817398/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com