How do I make a label slide onto the screen when a button is pressed? SWIFT 2 - ios

I'm building an iOS app that takes some text from a website and displays it in a label when a button is pressed.
I'd like the label to move onto the screen from below when the button is pressed, how is this done in Swift 2?
I've started off using this code to move the label off the screen when the view appears, but I can't find a way to animate it back onto the screen
override func viewDidLayoutSubviews() {
label.center = CGPointMake(label.center.x, label.center.y - 400)
}

Very simple approach. Set the CGPoints to where you want to label positioned.
UIView.animateWithDuration(1, animations: {
label.center = CGPointMake(label.center.x, label.center.y+400)
})

Related

UIScrollView: How to kick start keyboard dismiss interactively operation (Like WhatsApp) before the drag down operation touching the keyboard?

In UIScrollView, there is a feature named "Keyboard Dismiss interactively"
By using such option, this enables me to implement the following drag down to hide keyboard
However, the keyboard dismiss operation only kick start, when the UIScrollView drag action touches keyboard edge.
What I would like to achieve is, the keyboard dismiss operation kick start, when the UIScrollView drag operation touches the bottom toolbar edge.
What I wish to achieve (Same as WhatsApp)
As you can see from the video, the keyboard dismiss operation will kick start, when the drag operation touches the bottom bar edge, even before touching keyboard edge.
May I know, what technique WhatsApp is using, to achieve such behavior?
Side note
You may notice our bottom toolbar does move along with keyboard. This is because there is a bottom constraint for bottom toolbar's bottom with Safe Area's bottom.
We adjust the bottom constraint's constant value, by installing a gesture recognizer in global Window. This is the code snippet to achieve such technique.
#objc private func didPan(_ sender: UIPanGestureRecognizer){
if keyboardHeight > 0 {
let mainScrollView = editable.mainScrollView
let isScrolling = (mainScrollView.isDragging || mainScrollView.isDecelerating)
if isScrolling {
if let mainScrollViewGlobalOrigin = mainScrollView.globalOrigin {
let point = sender.location(in: sender.view!)
// Take safe area into consideration, like iPhone 12 Pro Max.
let key = UIWindow.key
let bottomSafeArea = key?.safeAreaInsets.bottom ?? 0
let dy = point.y - (
mainScrollViewGlobalOrigin.y +
mainScrollView.frame.height +
toolbarHeightLayoutConstraint.constant +
bottomSafeArea -
bottomLayoutConstraint.constant -
self.keyboardHeight
)
if dy > 0 {
bottomLayoutConstraint.constant = -(keyboardHeight - dy)
}
}
}
}
}
The reason that WhatsApp behaves like this is that their view is considered to be part of the keyboard, so when the swipe gesture reaches their custom view it will begin interactive dismissal.
To achieve this yourself all you need to do is provide the toolbar view as the inputAccessoryView for your view controller. You won't need the constraints for positioning as the keyboard window would then control your toolbar's position.
There is also inputAccessoryViewController for the times where your toolbar may not be a UIView, but instead an entire UIViewController.
The views in either of these properties will only be visible when the keyboard is visible, so to get around that you'll still want to put it into your view hierarchy, but remove/add it based on becoming/resigning first responder.
EDIT: Also, you should be using UIApplication.keyboardDidChangeFrameNotification to detect when the keyboard changes size/position/etc and allow you to adjust insets/positions of views appropriately. In modern iOS there are plenty of ways the keyboard can change size while open, and observing that notification is the correct way to handle the keyboard size.

How to animate a button to show a view to right and bounce to back when it is tapped

I want a button to expand the view to right when the button is tapped and bounce back when the view is tapped? can anyone suggest me?
Here is the screen shot for reference
Create a view that is the same size as the button behind the button, set the width of the view based on the right constraint and use the animation in the click response of the button, which should achieve the effect you want;
i think that you should use CoreGraphics, there is many simple tutorials you can find on youtube
Example
button.transform = CGAffineTransform(scaleX: 0, y: 0)
this method is what you are looking for in order to extend your custom view/button
Example like this: hope it helps.
let contraint:CGFloat = 0.0
if backViewWidthContraint.constant == 0 {
contraint = 300.0
}else{
contraint = 0.0
}
UIView.animate(withDuration: 3) {
self.backViewWidthContraint.constant = contraint
self.view.layoutIfNeeded()
}

How to get the animate affect of keyboard of iPhone in UIbuttons in swift?

Hai am trying to animation effect of iOS keyboard into the buttons of my application. In the image on pressing O it became Bigger and displayed the alphabet chosen, i am trying to get the same effect into one of UIbuttons in swift. On clicking the button there should also appear just like picture a bigger frame and display the text it.
Thanks
Increase the size of UIButton with
UIView.animate(withDuration: DURATION, animations: {
btn.frame.width += 20
btn.frame.height += 20
}, completion: { (success) in
})
or you can also try this answer
Scale UIButton Animation- Swift

Swift Animation - Splash screen logo to slide up into logo on login screen

I am an animation noob, but I found a really great transition animation. The Discover mobile app on iOS has their logo centered on the splash screen. The logo then slides up and scales down a bit and the login screen slowly appears during the animation. I have a start and end position for my logo, and I know the sizes I need it to start with and end with. I am not sure how to animate it sliding up and revealing the login screen.
Since you want the logo first to appear centered you should use NSLayoutConstraints.
Create a ViewController that looks like your LaunchScreen and set a #IBOutlet connection to your logo's size and position, and to your loginView. The simple perform something like:
override func viewDidAppear(animated: Bool)
{
super.viewDidAppear(animated)
self.view.layoutIfNeeded()
self.logoHeightFromTopConstraint.constant = 100
self.logoWidthConstraint.constant = 80
self.loginView.alpha = 0.0
UIView.animateWithDuration(3.0, animations:
{
self.loginView.alpha = 1.0
self.view.layoutIfNeeded()
})
}
You might have to remove/add some NSLayoutConstraints to achieve what you want, but this is the general idea.

Animate UITextField

I need to make it so that when I click a button, the UITextField transforms left onto the view from outside the view. However, when I execute the following code, the UITextField starts off in the middle of the viewcontroller, and then when the button is clicked it transforms left onto the view from outside the view. How can I make it so that when the view loads initially, it is not seen until the button is clicked, using swift.
#IBAction func joinCircleButton(sender: AnyObject) {
let button = sender as UIButton
joinTextField.frame.origin.x=500
joinTextField.frame.origin.y=100
if (button.frame.origin.x - 75>0){
UIView.animateWithDuration(0.5, animations:{
button.frame = CGRectMake(button.frame.origin.x - 125, button.frame.origin.y,button.frame.size.width, button.frame.size.height)
button.transform = CGAffineTransformMakeScale(0.5, 0.5);
self.joinTextField.frame=CGRectMake(self.joinTextField.frame.origin.x - 325, self.joinTextField.frame.origin.y,self.joinTextField.frame.size.width, self.joinTextField.frame.size.height)
})
}
}
You need to put the UITextField, or any object that you wish to hide, outside of the view by using a function called:
func viewDidLayoutSubviews()
This function is called just before the screen is loaded. It gets the sub-views ready but doesn't show them on the screen yet. So this is an opportunity to hide the UITextField from sight like so:
func viewDidLayoutSubviews(){
// Here is just an EXAMPLE of what I'd do with my text field, you can change this however you wish
// The point is that I am putting it away so no one can see it at first, and then later it will show
joinTextField.center = CGPointMake(joinTextField.frame.origin.x-500, joinTextField.frame.origin.y)
}
Now the textfield should be hidden when the view loads and then it can be animated later on.I hope this helped.

Resources