Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS中是否有暂停UIImageView动画的功能?_Ios_Swift_Uiimageview - Fatal编程技术网

iOS中是否有暂停UIImageView动画的功能?

iOS中是否有暂停UIImageView动画的功能?,ios,swift,uiimageview,Ios,Swift,Uiimageview,iOS中是否有任何函数可以暂停UIImageView动画,如startAnimating()和stopmanimating()函数,以分别开始和停止动画 let imageView: UIImageView = UIImageView(image: "Chicken")) let playButton: UIButton = UIButton(type: .system) let stopButton: UIButton = UIButton(type: .system) override f

iOS中是否有任何函数可以暂停UIImageView动画,如startAnimating()和stopmanimating()函数,以分别开始和停止动画

let imageView: UIImageView = UIImageView(image: "Chicken"))
let playButton: UIButton = UIButton(type: .system)
let stopButton: UIButton = UIButton(type: .system)

override func viewDidLoad() {
    super.viewDidLoad()

    view.backgroundColor = UIColor.cyan

    let imagesArray = [UIImage(named: "Chicken"), UIImage(named: "Burger"), UIImage(named: "Fruits"), UIImage(named: "Pizza"), UIImage(named: "Sandwich")]

    imageView.frame = CGRect(x: 80, y: 80, width: 200, height: 200)
    view.addSubview(imageView)
    imageView.animationImages = imagesArray as? [UIImage]
    imageView.animationDuration = 2
    imageView.animationRepeatCount = 4

    playButton.frame = CGRect(x: 80, y: 320, width: 40, height: 40)
    playButton.setTitle("play", for: .normal)
    playButton.setTitleColor(UIColor.black, for: .normal)
    playButton.backgroundColor = UIColor.green
    playButton.addTarget(self, action: #selector(playButtonAction), for: .touchUpInside)
    view.addSubview(playButton)

    stopButton.frame = CGRect(x: 140, y: 320, width: 40, height: 40)
    stopButton.setTitle("stop", for: .normal)
    stopButton.setTitleColor(UIColor.black, for: .normal)
    stopButton.backgroundColor = UIColor.green
    stopButton.addTarget(self, action: #selector(stopButtonAction), for: .touchUpInside)
    view.addSubview(stopButton)
}

func playButtonAction(sender: UIButton){
    imageView.startAnimating()
}

func stopButtonAction(sender: UIButton){
    imageView.stopAnimating()
}

您可以通过恢复和暂停图层来使用它

override func viewDidLoad() {
    super.viewDidLoad()


    let images = [UIImage]()
     let  imageView =  UIImageView()
    imageView.animationImages = images //images is your array
    imageView.startAnimating()

    self.pauseLayer(layer: imageView.layer)

}

func pauseLayer(layer: CALayer) {
    let pausedTime = layer.convertTime(CACurrentMediaTime(), from: nil)
    layer.speed = 0.0
    layer.timeOffset = pausedTime
}

func resumeLayer(layer: CALayer) {
    let pausedTime = layer.timeOffset
    layer.speed = 1.0
    layer.timeOffset = 0.0
    layer.beginTime = 0.0
    let timeSincePause = layer.convertTime(CACurrentMediaTime(), from: nil) - pausedTime
    layer.beginTime = timeSincePause
}

您可以通过恢复和暂停图层来使用它

override func viewDidLoad() {
    super.viewDidLoad()


    let images = [UIImage]()
     let  imageView =  UIImageView()
    imageView.animationImages = images //images is your array
    imageView.startAnimating()

    self.pauseLayer(layer: imageView.layer)

}

func pauseLayer(layer: CALayer) {
    let pausedTime = layer.convertTime(CACurrentMediaTime(), from: nil)
    layer.speed = 0.0
    layer.timeOffset = pausedTime
}

func resumeLayer(layer: CALayer) {
    let pausedTime = layer.timeOffset
    layer.speed = 1.0
    layer.timeOffset = 0.0
    layer.beginTime = 0.0
    let timeSincePause = layer.convertTime(CACurrentMediaTime(), from: nil) - pausedTime
    layer.beginTime = timeSincePause
}
举个例子

NSArray *myAnimationFrames = [NSArray arrayWithObjects:
  [UIImage imageWithName:@"myImageFirst.png"],
  [UIImage imageWithName:@"myImageSecond.png"], 
  [UIImage imageWithName:@"myImageThird.png"], 

  nil];
//Create imageview object
UIImageView *animatedImageView = [[UIImageView alloc] init];
animatedImageView.animationImages = myAnimationFrames;
[animatedImageView setAnimationRepeatCount:1];
[animatedImageView startAnimating];
快速版本

  let arrayImages = [UIImage(named: "myImageFirst.png"), UIImage(named: "myImageSecond.png"),UIImage(named: "myImageThird.png")]

    let imageView = UIImageView()
    imageView.animationImages = arrayImages as? [UIImage]
    imageView.animationRepeatCount = 1
    imageView.startAnimating()

    //for stopping 
    imageView.stopAnimating()
举个例子

NSArray *myAnimationFrames = [NSArray arrayWithObjects:
  [UIImage imageWithName:@"myImageFirst.png"],
  [UIImage imageWithName:@"myImageSecond.png"], 
  [UIImage imageWithName:@"myImageThird.png"], 

  nil];
//Create imageview object
UIImageView *animatedImageView = [[UIImageView alloc] init];
animatedImageView.animationImages = myAnimationFrames;
[animatedImageView setAnimationRepeatCount:1];
[animatedImageView startAnimating];
快速版本

  let arrayImages = [UIImage(named: "myImageFirst.png"), UIImage(named: "myImageSecond.png"),UIImage(named: "myImageThird.png")]

    let imageView = UIImageView()
    imageView.animationImages = arrayImages as? [UIImage]
    imageView.animationRepeatCount = 1
    imageView.startAnimating()

    //for stopping 
    imageView.stopAnimating()
没有。 无法暂停动画以阅读此文档

UIImageView没有任何处理pauseAnimation操作的函数/属性成员

没有。 无法暂停动画以阅读此文档

UIImageView没有任何处理pauseAnimation操作的函数/属性成员



请详细说明,解释您面临的问题。显示您的代码,您已经尝试过……请说明,根据您对答案的评论,这个问题非常模糊。你真的应该用有助于他人的细节更新你的问题。请详细说明,解释你面临的问题。展示你的代码,你已经尝试过了…请,这个问题非常模糊-基于你对答案的评论。你真的应该用有助于他人的细节更新你的问题。OP要求暂停动画,而不是删除动画。它没有实现暂停操作@AbdelahadDarwish@shashank再次检查此解决方案,我们可以使用layer@DivyeshGondaliya检查一下,我们可以用图层来做暂停图层效果很好。谢谢。OP要求暂停动画而不是删除动画。它没有实现暂停操作@AbdelahadDarwish@shashank再次检查此解决方案,我们可以使用layer@DivyeshGondaliya检查一下,我们可以用图层来做暂停图层效果很好。谢谢。你看到问题上的Swift标签了吗?添加了Swift代码。我假设开发人员了解Swift以及客观CW,当只添加其中一个标记时,您为什么会做出这样的假设?我看不到使用此代码@Gagan_实现任何暂停操作。您是否看到问题上的Swift标记?添加了Swift代码。我假设开发人员了解Swift和客观CW,当只添加了其中一个标记时,您会做出这样的假设吗?我看不到使用此代码@Gagan_iOS实现任何暂停操作