我怎样才能在swift中产生类似这样的效果:
我希望动画永远循环。
0vvn1miw1#
适用于iOS
UIViewAnimationOptions集合提供了不同的方便选项来实现漂亮和复杂动画的组合。对于您的特定场景,您将需要其中的两个选项。
UIViewAnimationOptions.Repeat UIViewAnimationOptions.AutoReverse
请查看下面的代码以实现。
代码:
class MyViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() let view = UIView(frame: CGRect(x: 0, y: 0, width: 200, height: 200)) view.backgroundColor = UIColor.blueColor() self.view.addSubview(view) UIView.animateWithDuration(1, delay: 0, options: [UIViewAnimationOptions.Autoreverse, UIViewAnimationOptions.Repeat], animations: { view.backgroundColor = UIColor.clearColor() }, completion: nil) } }
**说明:**我创建了一个带有特定框架的视图,用于演示目的。
您感兴趣的部分是UIView.animateWithDuration方法,请注意,我在options参数中提供了一个数组[UIViewAnimationOptions.AutoReverse, UIViewAnimationOptions.Repeat]。这两个选项足以实现一个平滑和永远循环的动画,如下图所示。https://s3.amazonaws.com/uploads.hipchat.com/37040/1764070/6Iow7n7WiWf6Naz/autoReverse.gif如果不想反转动画,只需从options参数的数组中删除UIViewAnimationOptions.AutoReverse即可。您将得到如下所示的动画。https://s3.amazonaws.com/uploads.hipchat.com/37040/1764070/8fyRUlzqNHSQI47/noreverse.gif
UIView.animateWithDuration
options
[UIViewAnimationOptions.AutoReverse, UIViewAnimationOptions.Repeat]
UIViewAnimationOptions.AutoReverse
f0brbegy2#
设viewSquare为问题中蓝色正方形的名称。
viewSquare
UIView.animate(withDuration: 0.5, delay: 0, options: [.repeat,.autoreverse], animations: { viewSquare.alpha = 0.0 }, completion: nil)
vsnjm48y3#
雨燕5.1
let duration = 0.5 func fadeIn(finished: Bool) { UIView.animate(withDuration: self.duration, delay: 0, options: [.curveEaseInOut], animations: { self.tftMap.alpha = 1 }, completion: self.fadeOut) } func fadeOut(finished: Bool) { UIView.animate(withDuration: self.duration, delay: 0, options: [.curveEaseInOut], animations: { self.tftMap.alpha = 0 }, completion: self.fadeIn) }
igetnqfo4#
我想你是为iOS编程的。试用一下duration,看看哪款最适合您:
duration
class ViewController: UIViewController { @IBOutlet weak var myView: UIView! let duration = 0.5 override func viewDidLoad() { super.viewDidLoad() self.fadeOut(true) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } func fadeIn(finished: Bool) { UIView.animateWithDuration(self.duration, delay: 0, options: [.CurveEaseInOut], animations: { self.myView.alpha = 1 } , completion: self.fadeOut) } func fadeOut(finished: Bool) { UIView.animateWithDuration(self.duration, delay: 0, options: [.CurveEaseInOut], animations: { self.myView.alpha = 0 } , completion: self.fadeIn) } }
zaq34kh65#
"雨燕5"这对我来说很有效。我想制作一个标签的连续淡入淡出动画,我把它放在了“cardHeaderView”UIView中。
@IBOutlet weak var cardHeaderView: UIView!
把这个放在“viewDidAppear”里面。我用了零的延迟,这样动画就会马上开始。
fadeViewInThenOut(view: cardHeaderView, delay: 0)
下面是函数。
func fadeViewInThenOut(view : UIView, delay: TimeInterval) { let animationDuration = 1.5 UIView.animate(withDuration: animationDuration, delay: delay, options: [UIView.AnimationOptions.autoreverse, UIView.AnimationOptions.repeat], animations: { view.alpha = 0 }, completion: nil) }
6jygbczu6#
如果你想要可重复的淡入淡出动画,你可以使用CABasicAnimation,如下所示:
CABasicAnimation
extension UIView { enum AnimationKeyPath: String { case opacity = "opacity" } func flash(animation: AnimationKeyPath ,withDuration duration: TimeInterval = 0.5, repeatCount: Float = 5){ let flash = CABasicAnimation(keyPath: AnimationKeyPath.opacity.rawValue) flash.duration = duration flash.fromValue = 1 // alpha flash.toValue = 0 // alpha flash.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut) flash.autoreverses = true flash.repeatCount = repeatCount layer.add(flash, forKey: nil) } }
// You can use it with all kind of UIViews e.g. UIButton, UILabel, UIImage, UIImageView, ... imageView.flash(animation: .opacity, withDuration: 1, repeatCount: 5) titleLabel.flash(animation: .opacity, withDuration: 1, repeatCount: 5)
2vuwiymt7#
雨燕5
func startAnimation() { UIView.animateKeyframes(withDuration: 6.0, delay: 0, options: [.repeat, .autoreverse, .calculationModeLinear]) { UIView.addKeyframe(withRelativeStartTime: 0.0, relativeDuration: 0.165) { [weak self] in self?.view1.alpha = 0.0 } UIView.addKeyframe(withRelativeStartTime: 0.165, relativeDuration: 0.165) { [weak self] in self?.view2.alpha = 1.0 } UIView.addKeyframe(withRelativeStartTime: 0.66, relativeDuration: 0.165) { [weak self] in self?.view2.alpha = 0.0 } UIView.addKeyframe(withRelativeStartTime: 0.825, relativeDuration: 0.165) { [weak self] in self?.view1.alpha = 1.0 } } }
7条答案
按热度按时间0vvn1miw1#
适用于iOS
UIViewAnimationOptions集合提供了不同的方便选项来实现漂亮和复杂动画的组合。对于您的特定场景,您将需要其中的两个选项。
请查看下面的代码以实现。
代码:
**说明:**我创建了一个带有特定框架的视图,用于演示目的。
您感兴趣的部分是
UIView.animateWithDuration
方法,请注意,我在options
参数中提供了一个数组[UIViewAnimationOptions.AutoReverse, UIViewAnimationOptions.Repeat]
。这两个选项足以实现一个平滑和永远循环的动画,如下图所示。https://s3.amazonaws.com/uploads.hipchat.com/37040/1764070/6Iow7n7WiWf6Naz/autoReverse.gif
如果不想反转动画,只需从
options
参数的数组中删除UIViewAnimationOptions.AutoReverse
即可。您将得到如下所示的动画。https://s3.amazonaws.com/uploads.hipchat.com/37040/1764070/8fyRUlzqNHSQI47/noreverse.giff0brbegy2#
适用于iOS
设
viewSquare
为问题中蓝色正方形的名称。vsnjm48y3#
雨燕5.1
igetnqfo4#
我想你是为iOS编程的。
试用一下
duration
,看看哪款最适合您:zaq34kh65#
"雨燕5"
这对我来说很有效。我想制作一个标签的连续淡入淡出动画,我把它放在了“cardHeaderView”UIView中。
把这个放在“viewDidAppear”里面。我用了零的延迟,这样动画就会马上开始。
下面是函数。
6jygbczu6#
如果你想要可重复的淡入淡出动画,你可以使用
CABasicAnimation
,如下所示:首先创建方便的UIView扩展:
使用方法:
2vuwiymt7#
雨燕5