Swift UIImage Animation - Fly in from outside of frame into frame - swift

I am currently trying to see if there are any tutorials or some reference on how to animate UIImages to fly in from outside of frame into frame. I am currently trying to use the bottom code but it isn't producing what I would like.
I am creating a DC Universe intro page where the main characters will fly in one at a time and form a nice collection of characters which I think would be really cool.
UIView.animate(withDuration: 0.5, animations: {
self.superManImage.frame.origin.x -= 200 }, completion: nil)

This so far is working for me. Batman image flys in from top and Superman image flys in from left.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
superManImage.center.x -= view.bounds.width
batManImage.center.y -= view.bounds.width
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIView.animate(withDuration: 0.8, delay: 1.0, options: [], animations: {
self.superManImage.center.x += self.view.bounds.width
}, completion: nil)
UIView.animate(withDuration: 0.5, delay: 0.3, options: [], animations: {
self.batManImage.center.y += self.view.bounds.width
}, completion: nil)
}

Related

Constraint animation doesnt animate y position change

This code is to expand a label by disabling the constraint which is keeping it at a max height of 54 to a full size. It works as it should but it jumps to the new y position straight away (without animating) and changes frame height after which makes it look like it jumps before animating
#IBOutlet var descriptionHeightConstraint: NSLayoutConstraint!
#IBAction func toggleFullDescription(_ sender: Any) {
if descriptionHeightConstraint.isActive {
self.layoutIfNeeded()
descriptionHeightConstraint.isActive = false
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseInOut, animations: {
self.layoutIfNeeded()
}, completion: nil)
//seeMoreButton.setTitle("See less", for: .normal)
} else {
self.layoutIfNeeded()
descriptionHeightConstraint.isActive = true
UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseInOut, animations: {
self.layoutIfNeeded()
}, completion: nil)
//seeMoreButton.setTitle("See more", for: .normal)
}
}
Video of the issue

UICollectionViewCell Custom Animation On Tap

My goal is to build a custom cell that, when tapped, animates similar to Snapchat's. I have gotten the animation to work, but as a scroll more (after tap), the last selected cell continues to animate as I tap around the screen. Here's a video example
To get this animation, I am overriding the isSelected value in a custom cell class like so:
override var isSelected: Bool {
didSet {
if isSelected {
UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {
self.transform = CGAffineTransform(scaleX: 0.95, y: 0.95)
}, completion: { _ in
UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut, animations: {
self.transform = CGAffineTransform(scaleX: 1, y: 1)
}, completion: nil)
})
}
}
}
Please help me figure out why the last selected cell continues to animate even after the correct animation is completed.

Trying change Button Title and label.center.x in UIView.animate when press button but stuck. Why? Swift 4 Xcode 9.4

My Problem
I'm trying do some animation with label using UIView.animate(..) when touchInside button . Everything still be OK until I added a line: "self?.confirm.setTitle("Đăng nhập", for: .normal). The animation doesn't work.
My Will
I want the yellow underline below Đăng Ký switch to below Đăng nhập.
It good when code is
#IBAction func signUpAction(_ sender:Any?){
if (signup == false){
signup = true
UIView.animate(withDuration: 0.4, delay: 0, options: [.curveEaseIn], animations: {[weak self] in
self?.underlineSignup.center.x -= (self?.underlineSignup.bounds.width)!
self?.view.layoutIfNeeded()
}, completion: nil)
}
}
#IBAction func signInAction(_ sender:Any?){
if (signup == true){
signup = false
UIView.animate(withDuration: 0.4, delay: 0, options: [.curveEaseIn], animations: {[weak self] in
self?.underlineSignup.center.x += (self?.underlineSignup.bounds.width)!
self?.view.layoutIfNeeded()
}, completion: nil)
}
}
It work
But when I add .setTitle
#IBAction func signUpAction(_ sender:Any?){
if (signup == false){
signup = true
UIView.animate(withDuration: 0.4, delay: 0, options: [.curveEaseIn], animations: {[weak self] in
self?.underlineSignup.center.x -= (self?.underlineSignup.bounds.width)!
self?.confirm.setTitle("Đăng ký", for: .normal)
self?.view.layoutIfNeeded()
}, completion: nil)
}
}
#IBAction func signInAction(_ sender:Any?){
if (signup == true){
signup = false
UIView.animate(withDuration: 0.4, delay: 0, options: [.curveEaseIn], animations: {[weak self] in
self?.underlineSignup.center.x += (self?.underlineSignup.bounds.width)!
self?.confirm.setTitle("Đăng nhập", for: .normal)
self?.view.layoutIfNeeded()
}, completion: nil)
}
}
It stuck, only the title of button confirm change, the underline doesn't move
Please anyone can explain this situation.
EDIT:
The animation working but it's destination is always the first place which under Đăng ký ( animation come from left or right of it, the result always first place )
I think I knew how it is. Because in setTitle has updateFrame Constraintlayout of view. I have a contraintlayout when build button.
When I setTitles it reset the view using constraint.
That's what I think about this.
Try putting 'setTitle' into completion.
UIView.animate(withDuration: 0.15, delay: 0, options: .curveLinear, animations: {
self?.underlineSignup.center.x -= (self?.underlineSignup.bounds.width)!
self?.view.layoutIfNeeded()
}){
//completion
self?.confirm.setTitle("Đăng ký", for: .normal)
}

UIViewAnimation with constraint not working in Swift

I am trying to create an animation where I want to slide a uiview from right to left once parent UIViewController has loaded although I am able to display the view but the animation is not working. My code for animation:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
view.isOpaque = false
UIView.animate(withDuration: 1.0, delay: 1.0, options: .curveEaseOut, animations: {
self.contentXConstraint.constant = 500
self.contentXConstraint.constant = 80
}) { (status) in
}
}
I have displayed the parent view controller as presented viewcontroller:
present(navController, animated: false, completion: nil)
You forgot self.view.layoutIfNeeded()
self.contentXConstraint.constant = 500
self.contentXConstraint.constant = 80
UIView.animate(withDuration: 1.0, delay: 1.0, options: .curveEaseOut, animations: {
self.view.layoutIfNeeded()
}) { (status) in
}
from https://developer.apple.com/documentation/uikit/uiview/1622507-layoutifneeded
Use this method to force the view to update its layout immediately. When using Auto Layout, the layout engine updates the position of views as needed to satisfy changes in constraints
Move your animation code from viewDidAppear to viewDidLayoutSubviews
You need to add layoutIfNeeded() method after updating constraints
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
view.isOpaque = false
UIView.animate(withDuration: 1.0, delay: 1.0, options: .curveEaseOut, animations: {
self.contentXConstraint.constant = 500
self.contentXConstraint.constant = 80
self.view.layoutIfNeeded()
}) { (status) in
}
}

Custom Slide Segue - Xcode 8.0 Swift 3.0

What is the best way to create a custom segue that lets me press a button and then a view controller slides on. I have created a left to right segue but I want to make it go the other way. I have looked at some Youtube videos and this question but they don't show me what I want.
My code:
import UIKit
class SegueFromLeft: UIStoryboardSegue
{
override func perform()
{
let src = self.sourceViewController
let dst = self.destinationViewController
src.view.superview?.insertSubview(dst.view, aboveSubview: src.view)
dst.view.transform = CGAffineTransformMakeTranslation(-src.view.frame.size.width, 0)
UIView.animateWithDuration(0.25,
delay: 0.0,
options: UIViewAnimationOptions.CurveEaseInOut,
animations: {
dst.view.transform = CGAffineTransformMakeTranslation(0, 0)
},
completion: { finished in
src.presentViewController(dst, animated: false, completion: nil)
}
)
}
}
Basically, I want to create a segue that goes from right to left.
Thanks
I am using Xcode 8.0 and Swift 3.0
Try this:
class SegueFromLeft: UIStoryboardSegue{
override func perform(){
let src=sourceViewController
let dst=destinationViewController
let slide_view=destinationViewController.view
src.view.addSubview(slide_view)
slide_view.transform=CGAffineTransform.init(translationX: src.view.frame.size.width, 0)
UIView.animateWithDuration(0.25,
delay: 0.0,
options: UIViewAnimationOptions.CurveEaseInOut,
animations: {
slide_view.transform=CGAffineTransform.identity
}, completion: {finished in
src.present(dst, animated: false, completion: nil)
slide_view.removeFromSuperview()
})
}
}
Swift 3.1:
class ProceedToAppStoryboardSegue: UIStoryboardSegue {
override func perform(){
let slideView = destination.view
source.view.addSubview(slideView!)
slideView?.transform = CGAffineTransform(translationX: source.view.frame.size.width, y: 0)
UIView.animate(withDuration: 0.25,
delay: 0.0,
options: UIViewAnimationOptions.curveEaseInOut,
animations: {
slideView?.transform = CGAffineTransform.identity
}, completion: { finished in
self.source.present(self.destination, animated: false, completion: nil)
slideView?.removeFromSuperview()
})
}
}