gpt4 book ai didi

ios - 从 Swift 中的 UIView 上的特定点开始淡入淡出动画

转载 作者:可可西里 更新时间:2023-11-01 00:58:36 24 4
gpt4 key购买 nike

我在动画淡入和淡出方面遇到问题。

我有一个简单的设置,在 Storyboard上我有一个名为 myViewUIView

我想做的是当用户触摸屏幕时淡入myView,当用户停止触摸屏幕时淡出myView。我可以用下面的代码来实现。淡入淡出超过五秒钟。

但是,当用户在淡入或淡出动画中途开始触摸或停止触摸屏幕时,就会出现问题。

例如,当用户停止触摸屏幕时,淡入动画不是优雅地停止 myView 在其当前位置淡入并以淡出动画反转,实际上是什么发生的是 myView alpha 在开始淡出之前跳转到 1.0

问题:

What changes to the code below need to be made to ensure that each new animation, fade-in or fade-out, interrupts the current fade animation occurring on myView and begins from the current alpha of the previous fade animation?

代码:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var myView: UIView!

override func viewDidLoad() {
super.viewDidLoad()
self.myView.alpha = 0
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
UIView.animateWithDuration(5, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
self.myView.alpha = 1
}, completion: nil)
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
UIView.animateWithDuration(5, delay: 0, options: UIViewAnimationOptions.CurveEaseInOut, animations: {
self.myView.alpha = 0
}, completion: nil)
}

}

最佳答案

问题是您的 touchesEnded 动画打断了您的 touchesBegan 动画,导致 touchesBegan 动画被删除并瞬间显示您已将 alpha 设置为 1。

为防止这种情况,请在您的选项中包含: .beginFromCurrentState 选项。

关于ios - 从 Swift 中的 UIView 上的特定点开始淡入淡出动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39083196/

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