Swift: Animation effect when transition between one view to another view - swift

I am doing wow slider animation effect in my app by using swift. I added 3 images. It is sliding one by one. But I need animation like this. I don't know how to do when transition from one image to another. Kindly guide me. Animation effect is below. Not only this animation effect, but also any animation effect.

Not exactly same , but similar animation effects can be achieved using UIViewAnimations. For example try this:
[UIView transitionWithView:textFieldimageView
duration:0.5f
options:UIViewAnimationOptionTransitionCrossDissolve
animations:^{
imageView.oldImage = newImage;
} completion:NULL];
Try this syntax for swift:
Swift 2
UIView.transitionWithView(self.view, duration: 0.5, options: UIViewAnimationOptions.TransitionCrossDissolve, animations: {
}, completion: { (fininshed: Bool) -> () in
})
Swift 3, 4, 5
UIView.transition(with: self.view, duration: 0.5, options: UIView.AnimationOptions.transitionCrossDissolve, animations: {
}, completion: { (fininshed: Bool) -> () in
})

Related

UIDatePicker's Change Year Arrow's Animation Transition

What is the animation transition used in the right-arrow image to the down-arrow image when changing the year in the UIDatePicker control? The animation smoothly rotates the start image to the end image like so:
I am trying to mimic this transition for an image in my code. In viewDidLoad(), I have:
myButton.setImage(UIImage(named: "rightArrow")?.withRenderingMode(.alwaysTemplate), for: .normal)
and then within the function invoked when that button is tapped, I do:
UIView.transition(with: myButton, duration: 0.6, options: .transitionCrossDissolve, animations: {
self.myButton.setImage(UIImage(named: "downArrow")?.withRenderingMode(.alwaysTemplate), for: .normal)
}, completion: nil)
I am using .transitionCrossDissolve here, but obviously it isn't the right transition. I tried each of the seven available transition options, but none of them behave like in UIDatePicker. Is there an easy way to create this "rotating" transition?
I finally figured it out. This will animate the rotation of the arrow:
UIView.transition(with: myButton, duration: 0.2, options: [], animations: {
self.myButton.transform = CGAffineTransform(rotationAngle: .pi/2)
}, completion: nil)
EDIT -- shorter solution:
UIView.animate(withDuration: 0.2) {
self.myButton.transform = CGAffineTransform(rotationAngle: .pi/2)
}

Why is my button being removed from its superview before it completes its animation?

I am trying to move an my button to a certain part of the screen [as a custom button class GameBlock] and then remove it from its superview. However, it is acting weird in the sense that it removes itself from the superview before it completes the animation and I have no clue why. May somebody please help me out? I appreciate it.
UIView.animate(withDuration: movementTime,
delay: 0.0,
options: [],
animations: {
movedBlock.center = gridCoord[hitBlock.x][hitBlock.y]
},
completion: nil)
if movedBlock.center == gridCoord[hitBlock.x][hitBlock.y] {
movedBlock.removeFromSuperview()
}
If I understand your problem, this should works. Your view will be removed after the animation. The completion closure will be called at the end of the animation.
UIView.animate(withDuration: movementTime,
delay: 0.0,
options: [],
animations: {
movedBlock.center = gridCoord[hitBlock.x][hitBlock.y]
},
completion: { _ in
if movedBlock.center == gridCoord[hitBlock.x][hitBlock.y] {
movedBlock.removeFromSuperview()
}
})

Swift contentOffset scroll

I am developing a small application for tvOS where I need to show a UITextView.
Unfortunately sometimes this text can't fit inside the screen, that's why I need a way to scroll to the bottom of it programmatically.
I've tried using the following code:
self.setContentOffset(offset, animated: true)
It's actually working as intended, except for the fact that I need to handle the animation speed, so that the user can actually read; at the moment it's too fast.
This is what I tried to do, but with little success:
let offset = CGPoint(x: 0, y: contentSize.height - bounds.size.height)
UIView.animate(withDuration: 4, animations: {
self.setContentOffset(offset, animated: false)
})
I need a way to keep the text in place and scroll it to show the missing lines.
I am using a UITextView inside an UIScrollView.
Thanks.
If anyone is looking for a solution, this is what I ended up using:
var textTopDistance = 100
//calculate height of the text not being displayed
textNotVisibleHeight = descriptionLabel.contentSize.height - scrollView.bounds.size.height
func scrollText() {
//start new thread
DispatchQueue.main.async() {
//animation
UIView.animate(withDuration: 6, delay: 2, options: UIViewAnimationOptions.curveLinear, animations: {
//set scrollView offset to move the text
self.scrollView.contentOffset.y = CGFloat(self.textNotVisibleHeight - self.textTopDistance)
}, completion: nil)
}
}
It's not perfect I know, but at least it's working as intended.

UIView animations in Swift

I am trying to execute following code.
UIView.animateWithDuration(5.0, animations: {
println("animations")
}, completion: { finished in
println("completion")
})
So Here completion block is called immediately before 5 sec animation duration. Don't know what is wrong here.
Please let me know for proper code.
You should add some view for animation:
UIView.animateWithDuration(5.0, animations: {
someView.alpha = 0
println("animations")
}, completion: { finished in
println("completion")
})
You don't have any animation sequence in your animations block, try animating your current view and check it'll work fine.
UIView.animateWithDuration(5.0, animations: {
self.view.alpha = 0.5;
print("animations")
}, completion: { finished in
self.view.alpha = 1.0;
print("completion")
})

Implementing basic animations with tvOS

I'm hoping someone may be able to help me understand why my tvOS animations are not running.
In my code I have a function like this:
func testAnimation() {
clockLabel.alpha = 0.0
UIView.animateWithDuration(1.0, animations: {
self.clockLabel.alpha = 1.0
})
}
I am calling testAnimation() from within my viewDidLoad(), but no animation ever seems to happen.
I've tested with a few different types of animations, from things like position to opacity, but it seems that no animation ever actually runs in the Simulator.
At this time, my app does not have a focus. All I'm trying to do is load a blank UIView with a label in the middle that fades in.
You're trying to animate your UILabel before it has been displayed. Move your animation from viewDidLoad() to viewDidAppear().
I can confirm this behaviour as this happens for me as well. What I did was setting a delay of 0.1 (afterDelay:), and it is "good enough"
On the other hand, what DID actually work was setting a transform for my views. E.g. (objc though):
CGAffineTransform scaleTransform = CGAffineTransformMakeScale(0.5,0.5);
[UIView animateWithDuration: 0.5 animations:^{
_myView.transform = scaleTransform;
} completion: nil]
Maybe it's a bug in tvOS
Try using UIView Animation, like this (Swift):
clockLabel.alpha = 0
UIView.animateWithDuration(0.5, delay: 0.0, options: .Linear, animations: {
self.clockLabel.alpha = 1.0
}, completion: { finished in
// you may add some code here to make another action right after the animation end, or just delete this comment :)
})
This works on our side.