gpt4 book ai didi

ios - 在另一个动画之前在 alpha 上平滑地暂停重复的 UIView block 动画

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

我阅读了许多 Stack Overflow 问题来寻找解决方案,但我找不到以适用于 iOS 13 的方式解决此特定问题的问题。

我有一个标签,一旦我的 View Controller 加载,我想重复脉冲(如缓慢、淡出的闪烁)。

当需要从 View 过渡时,按下按钮,我想停止脉冲标签当前具有的任何 alpha 值,然后将其与其他 UI 元素一起从那里淡出。

过去,我会使用 UIViewPropertyAnimator 来实现此重复动画,然后在点击按钮后暂停动画,但从 iOS 13 UIView.setAnimationRepeatCount(.greatestFiniteMagnitude 开始) )UIView.setAnimationRepeatAutoreverses(true) 已弃用,这(我认为)使得无法使用属性动画器构建我的脉冲动画。

这是我的示例代码:

import UIKit

class ViewController: UIViewController {

private lazy var pulsingLabel: UILabel = {
let label = UILabel()
label.text = "I am pulsing"
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()

private lazy var stopButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Stop Pulsing",
for: .normal)
button.addTarget(self,
action: #selector(fadeEverythingOut),
for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()

override func viewDidLoad() {
super.viewDidLoad()

view.addSubview(pulsingLabel)
view.addSubview(stopButton)

pulsingLabel.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
pulsingLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

stopButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
stopButton.firstBaselineAnchor.constraint(equalToSystemSpacingBelow: pulsingLabel.lastBaselineAnchor,
multiplier: 4).isActive = true

startRepeatingAnimation()
}

private func startRepeatingAnimation() {
UIView.animate(withDuration: 1,
delay: 0,
options: [.repeat, .autoreverse, .curveEaseInOut],
animations: {
self.pulsingLabel.alpha = 0
})
}

@objc
private func fadeEverythingOut() {

pulsingLabel.layer.removeAllAnimations()

UIView.animate(withDuration: 2.0,
animations: {
self.pulsingLabel.alpha = 0
})
}
}

在此示例中,我调用 pulsingLabel.layer.removeAllAnimations() 来停止动画,但这不是可接受的解决方案,因为标签在调用时立即消失。

我还尝试了一些奇怪的东西,例如将该行夹在 UIView.setAnimationsEnabled(false)UIView.setAnimationsEnabled(true) 之间,但这不起作用.

我还尝试在调用pulsingLabel.layer.removeAllAnimations()之前保存pulsingLabel的alpha,然后立即再次设置它,但我总是得到一个值0 代表 pulsingLabel.alpha

我是否因为放弃UIViewPropertyAnimator而错过了一些东西,即使在UIView上使用了已弃用的功能,我仍然能够重复它?或者也许我需要在不同的图层上或以不同的方式调用 .layer.removeAllAnimations()

任何想法将不胜感激,因为我非常关心在我的应用程序中获得这些细节!

最佳答案

我没有看到你在寻找快速答案,但我现在只知道 ObjC。所以这里是计时器的解决方案(虽然在 ObjC 中很抱歉):

在 View 中加载:

    self.angle = 0;
self.myTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(update) userInfo:nil repeats:YES];

angle是CGFloat,用于存储当前值,以便在更新方法中通过余弦函数计算alpha:

-(void)update {
self.angle += M_PI/36.0;
if (self.angle > M_PI) self.angle = 0.0;
CGFloat val = (1 + cos(self.angle)) / 2.0;
dispatch_async(dispatch_get_main_queue(), ^{
self.myLabel.alpha = val;
});
}

您可以调整值 36.0 以使淡入/淡出更慢或更快。随着 angle 的增加,val 将给出一个在 0 和 1 之间来回摆动的值。然后使用 GCD 我们根据需要实时设置标签 alpha。当按下按钮移动到另一个 View Controller 时(根据您的要求),您只需通过使其无效来停止计时器,并且标签将保留在设置的任何 alpha 值上。

[self.myTimer invalidate];
self.myTimer = nil;
// CGFloat currentAlpha = self.myLabel.alpha;

从这里,您可以使用标签 alpha 执行其他操作。

关于ios - 在另一个动画之前在 alpha 上平滑地暂停重复的 UIView block 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57780370/

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