Swift: change UIImageView image midway during animation

Multi tool use
Multi tool use


Swift: change UIImageView image midway during animation



I have this code to create animation imageView:


imageView


imageView.layer.anchorPoint = CGPoint(x: 0, y: 0.5)

var transform = CATransform3DIdentity
transform.m34 = -0.001
imageView.layer.transform = transform
UIView.animate(withDuration: 3) {
self.imageView.layer.transform = CATransform3DRotate(transform, .pi, 0, 1, 0)

self.imageViewCenterConstraintX.constant = 0

self.view.layoutIfNeeded()
}



In this code imageView rotate with 180 degrees. I want to change image after imageView rotate with 90 degrees.


imageView


imageView





pi/2 u can do in params
– Abhishek Thapliyal
Jul 1 at 10:46





@AbhishekThapliyal And how do I write this in the code? How to let the device know that the imageView rotate with 90? I should write check?
– 111
Jul 1 at 12:08




3 Answers
3



You should chain the two animations, each rotating with pi/2.


imageView.layer.anchorPoint = CGPoint(x: 0, y: 0.5)

var transform = CATransform3DIdentity
transform.m34 = -0.001
imageView.layer.transform = transform
UIView.animate(withDuration: 1.0, animations:
{
self.imageView.layer.transform = CATransform3DRotate(transform, .pi/2, 0, 1, 0)
self.imageViewCenterConstraintX.constant = 0
})
{
(bCompleted) in
if (bCompleted)
{
self.imageView?.layer.transform = CATransform3DRotate(transform, .pi/2, 0, 1, 0)
}

UIView.animate(withDuration: 1.0, animations:
{
self.imageView.layer.transform = CATransform3DRotate(transform, .pi*0.999, 0, 1, 0)
self.imageView.image = UIImage.init(named:"anotherImage")
},
completion:
{
(bFinished) in
//Whatever
})
}





issue: Braces here form a trailing closure separated from its callee by multiple newlines
– 111
Jul 1 at 17:04





Edited. It's just a warning, should not restrict you from trying it. Did it work?
– Nirav Bhatt
Jul 1 at 17:13





No. imageView rotate only 90 degrees.
– 111
Jul 1 at 17:19





Edited. Can you try now?
– Nirav Bhatt
Jul 1 at 17:24





Ok. Doesn't work.
– 111
Jul 1 at 17:28



Use the one below. Use code to change image in the completion handler field.



UIView Animation with completion





And how do I write this in the code? How to let the device know that the imageView rotate with 90?
– 111
Jul 1 at 12:08



You could use DispatchQueue.main.asyncAfter.


DispatchQueue.main.asyncAfter


UIView.animate(withDuration: 3) {
DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
// Change your image here
}
self.imageView.layer.transform = CATransform3DRotate(transform, .pi, 0, 1, 0)

self.imageViewCenterConstraintX.constant = 0

self.view.layoutIfNeeded()
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

tzAZty1 s4i5Qa7q6rI 09MLs,Sy7xCIoXHPQKLlHL,hSnBx8aSG1,kHQNw,0
4V69y9iQLzQYK,uz4IpCWXnGOXL7 ZGs mKtCag,ML7i2QM2RV0tpCbPv

Popular posts from this blog

Rothschild family

Cinema of Italy