【问题标题】:Center of button in scrollview滚动视图中的按钮中心
【发布时间】:2018-12-17 16:40:01
【问题描述】:

我想制作一个动画,当你点击一个按钮时会产生一个脉冲。但这实际上不适用于滚动视图,因为它是“屏幕”上的特定点,而不是滚动视图。 当您向下滚动时,脉冲的原点保持不变。

@objc func addPulse() {
    let pulse = Pulsing(numberOfPulses: 1, radius: 120, position: playButton.center)
    pulse.animationDuration = 0.8
    pulse.backgroundColor = UIColor.red.cgColor

    self.view.layer.insertSublayer(pulse, below: playButton.layer)

位置必须来自 CGPoint 类型。

【问题讨论】:

  • 什么是self.view?是滚动视图还是其他视图? playButton 是在 self.view 还是其他视图中?

标签: ios swift uiscrollview uibutton


【解决方案1】:

根据您提供的少量上下文,我将继续假设您在 UIScrollView 中有一个按钮,您需要在按钮后面添加一个脉冲动画,但问题是随着视图滚动和按钮更改它的位置动画一直出现在相同的位置。另外,我想你在 UIViewController 类中。

如果我的猜测完全正确,这就是我认为正在发生的事情:

  • playButton 的位置将始终相同,因为它是它相对于其父级(在本例中为 UIScrollView)的位置。绝对位置将是项目在屏幕中的位置。
  • 您要在 UIView(UIViewController 的)内部添加动画,而不是在 UIScrollView 本身。这将是滚动时出现的问题。

我的建议是你试试:

@objc func addPulse() {
     let pulse = Pulsing(numberOfPulses: 1, radius: 120, position: playButton.center)
     pulse.animationDuration = 0.8
     pulse.backgroundColor = UIColor.red.cgColor

     scrollView.layer.insertSublayer(pulse, below: playButton.layer)
}

【讨论】:

    【解决方案2】:

    如果您使用的是 ScrollView,最好使用滚动视图的中心而不是按钮的中心,因为滚动时按钮会消失。因此,当您创建 Pulsing 对象时,请使用 self.view.center 作为位置。

    @objc func addPulse() {
        let pulse = Pulsing(numberOfPulses: 1, radius: 120, position: self.view.center)
        pulse.animationDuration = 0.8
        pulse.backgroundColor = UIColor.red.cgColor
    
        self.view.layer.insertSublayer(pulse, below: playButton.layer)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多