简体   繁体   中英

How do I find correct position of UIButton on swift?

Hi, I try to apply a pulse effection to button in swift.

But everytime I apply it, a pulse effection is always above the button.

@IBAction func btnAction(_ sender: UIButton) {
    var position = sender.center

    let pulse = PulseAnimation(numberOfPulses: Float.infinity, radius: 100, position: position) }

How can I find Real CGPoint of UIButton? Thanks.

PulseAnimation.swift

class PulseAnimation: CALayer {

var animationGroup = CAAnimationGroup()
var animationDuration: TimeInterval = 1.5
var radius: CGFloat = 200
var numberOfPulses: Float = 10

override init(layer: Any) {
    super.init(layer: layer)
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

init(numberOfPulses: Float = 10, radius: CGFloat, position: CGPoint) {
    super.init()
    self.backgroundColor = UIColor.black.cgColor
    self.contentsScale = UIScreen.main.scale
    self.opacity = 0
    self.radius = radius
    self.numberOfPulses = numberOfPulses
    self.position = position

    self.bounds = CGRect(x: 0, y: 0, width: radius*2, height: radius*2)
    self.cornerRadius = radius

    DispatchQueue.global(qos: DispatchQoS.QoSClass.default).async {
        self.setupAnimationGroup()
        DispatchQueue.main.async {
            self.add(self.animationGroup, forKey: "pulse")
        }
    }
}

func scaleAnimation() -> CABasicAnimation {
    let scaleAnimation = CABasicAnimation(keyPath: "transform.scale.xy")
    scaleAnimation.fromValue = NSNumber(value: 0)
    scaleAnimation.toValue = NSNumber(value: 1)
    scaleAnimation.duration = animationDuration
    return scaleAnimation
}

func createOpacityAnimation() -> CAKeyframeAnimation {
    let opacityAnimation = CAKeyframeAnimation(keyPath: "opacity")
    opacityAnimation.duration = animationDuration
    opacityAnimation.keyTimes = [0, 0.3, 1]
    opacityAnimation.values = [0.4, 0.8, 0]
    return opacityAnimation
}
func setupAnimationGroup() {
    self.animationGroup.duration = animationDuration
    self.animationGroup.repeatCount = numberOfPulses
    let defaultCurve = CAMediaTimingFunction(name: .default)
    self.animationGroup.timingFunction = defaultCurve
    self.animationGroup.animations = [scaleAnimation(), createOpacityAnimation()]
}
}

It is a class for applying pulse effection. It uses CGPoint for animating it and just use position to self.position. I dunno why the position is incorrect. Thanks to answer.

Real CGPoint what you want to know is a CGPoint that has x range 0~sender.frame.width and y range 0~sender.height Try this. :)

    @IBAction func buttonAction(_ sender: UIButton) {
        let position = CGPoint(x: sender.frame.width * 0.5, y: sender.frame.height * 0.5)

        let pulse = PulseAnimation(numberOfPulses: Float.infinity, radius: 100, position: position)
        sender.layer.addSublayer(pulse)

    }

在此处输入图片说明 在此处输入图片说明

I don't know what the class PulseAnimation does but with a search I could find a topic about it and applied to my code for testing the expected effect.

  @IBAction func numberHit(_ sender: UIButton) {
        let pulseAnimation = CABasicAnimation(keyPath: "opacity")
        pulseAnimation.duration = 30
        pulseAnimation.fromValue = 0
        pulseAnimation.toValue = 1
        pulseAnimation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
        pulseAnimation.autoreverses = true
        pulseAnimation.repeatCount = .greatestFiniteMagnitude
        sender.layer.add(pulseAnimation, forKey: nil)
}

Please checkout this: Swift Pulse Animation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM