UIView.animateWithDuration immediately after app launch - ios

I have this weird problem, I call UIView.animateWithDuration in viewDidAppear of my app's initial View Controller, but the animation seems to jump (it becomes visible somewhere halfway). The only workaround I found is to add a delay before starting the animation. I'd love to understand why this happens and if there's a better solution than the delay.
To try it yourself, create a single view app, then add the method below in ViewController.swift (Xcode debugger slows the app a bit, to simulate real conditions, stop and launch the app from the device or simulator):
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
let redSquare = UIView(frame: CGRectMake(0, 0, 50, 50))
redSquare.backgroundColor = UIColor.redColor()
redSquare.center = view.center
view.addSubview(redSquare)
UIView.animateWithDuration(0.5) { () -> Void in
var newFrame = redSquare.frame
newFrame.origin.y -= 200
redSquare.frame = newFrame
}
}
The red square is supposed to start translating from the center of the screen to somewhere up top, but it's as if the animation starts but the app is not ready to display it yet, so you see the red square starting it's translation above the center.
EDIT: Added call to viewDidAppear in example (does not solve the problem though).

Related

Change UIWindow frame animated

Is there any way to change UIWindow animated while being visible and key?
I want to implement such behaviour when network connection disappeared. I want to squeeze content that is displayed at that moment a bit and add a notification between controllers and the status bar. Something similar to what happened in iOS 12 and before, when you have active call or geolocation usage.
I can't transform my controller, because it must be above transition stack in the iOS 13.
Or I should discuss it with designers if it completely impossible.
What I found that you can't change the frame after you inited and presented window by setting new frame. As well as setting Transform.
var frame = UIScreen.main.bounds
frame.origin.y = 60
frame.size.height -= 60
(UIApplication.shared.delegate as! AppDelegate).window?.frame = frame
(UIApplication.shared.delegate as! AppDelegate).window?.transform = CGAffineTransform(translationX: 0, y: 60)

Swift How can I bring my Image after moving it back to the Middle ? Or rotate it back to the Origin position

I don't understand. how can I bring back my ImageView to the origin position (which is the Middle) of the view?
I want to use a TapGesture.
Or I have a second option to rotate it back to the original position. Currently I am trying to rotate it back but it doesn't work.
#IBAction func backToOrigin(recognizer:UITapGestureRecognizer){
if let view = recognizer.view{
self.view.transform = CGAffineTransform(rotationAngle: CGFloat(Double.pi / 2))
}
}
If you just want to undo a previous CGAffineTransform that you used to move or rotate your view you can use view.transform = CGAffineTransform.identity. (Also, delete the use of self from your code because self.view and the view inside your func are different things.)

I put UIView.animateWithDuration in viewWillAppear but it runs only during first entrance to the panel - why?

I try to animate a background in my swift ios app. I have an UIImageView with UIImage and I have the following method:
func slideImage(){
// Changes constant to be equal to the image width,
// this will move the image off-screen on the left-hand side.
backgroundImageConstraint.constant = backgroundPhoto.size.width
UIView.animateWithDuration(5, delay: 0, options: [.CurveLinear, .Repeat], animations: {
// Animates the constant change
self.view.layoutIfNeeded()
}, completion: {
done in
// Resets the image view back to its original position before starting a new round of the animation
self.backgroundImageConstraint.constant = 0
self.view.setNeedsUpdateConstraints()
})
}
I call this method in the viewWillAppear function, so I expected to run it every time when user enters my panel. However that's not how it works here - I see that it is invoked only once, when I enter the panel for the 2nd time the image in the background is not scrolling. This is a weird behavior. In my viewDidLoad I set all the things related to the UIImage:
override func viewDidLoad() {
super.viewDidLoad()
backgroundImage.image = backgroundPhoto
backgroundImage.frame = CGRect(x: backgroundImage.frame.origin.x, y: backgroundImage.frame.origin.y, width: backgroundPhoto.size.width, height: backgroundPhoto.size.height)
backgroundImage.contentMode = .Center
}
Does anyone knows why my photo is sliding only when user enters the panel for the first time and in other cases it is a static image?
Okay I'm gonna answer my own question - I found the information somewhere that if something interrupts the animations, the closure gets called automatically. So I moved calling the function slideImage() from viewWillAppear to viewDidAppear and now it works like a charm.
The animateWithDuration method is changing the frame of your background image moving it from the frame you set in viewDidLoad to the frame you have set (probably in your storyboard, with constraints). After your backgroundImage has been animated, it's frame changes and the next time you execute slideImage() method, the frame will be already changed, so it won't animate.
Try putting this two lines
backgroundImage.frame = CGRect(x: backgroundImage.frame.origin.x, y: backgroundImage.frame.origin.y, width: backgroundPhoto.size.width, height: backgroundPhoto.size.height)
backgroundImage.contentMode = .Center
before calling slideImage() in viewWillAppear and let us know the result.

Is there a way to programmatically update frames (Swift)

Is it possible to update the frames like in the interface builder but programmatically? Some of my objects get misplaced because of an animation (which I'll want to fix anyway, I guess..) but it brought to mind the aforementioned question
Edit: I have done some googling as well as looking on stackoverflow and not found what I'm looking for.
I want to update the frames of some buttons back to the constraints I set in the interface builder.
Sorry if this is too simple of a question, but I'm just looking for a simple answer: yes or no + line of code or the method to call. it probably won't even end up in my final project; I'm just curious.
Here's some code, since I guess that would help:
#IBAction func optionsButtonPushed(sender: AnyObject) {
UIView.animateWithDuration(2, animations: {
var theFrame = self.optionsMenu.frame
theFrame.size.height += 100
self.optionsMenu.frame = theFrame
})
}
The buttons located in the view I'm rolling in (from a height of zero) disappear because of the animation, I guess, so there's probably a better way than what I'm thinking
(sorry this question is so crazy; it's my first one!)
You can disable auto-layout. from storyboard. IF you still want to use AutoLayout then use -viewDidLayoutSubviews method.
self.yourButton.translatesAutoresizingMaskIntoConstraints = YES;
self.yourButton.frame = CGRectMake(16, 133, 130, 130);
UIView has a property called frame which is of type CGRect. CGRect is a struct.
You can update it by creating a new CGRect and assigning it to the property.
CGRect newFrame = CGRect(x:0, y:0, width:100, height:100)
yourView.frame = newFrame
UIView Documentation

UIView atop the Keyboard similar to iMessage App

currently I'm attempting to basically implement and exact copy of Apples iMessage App.
That means I need a UITextView that is docked at the bottom of the screen, and moves up when it becomes firstResponder. - That's pretty easy actually. There's a bazillion ways to do that and two of the most common are of course animating the view upwards or downwards if a notification was received. The other is to do it via the inputAccessoryView. Sadly some of the features the one has, the other doesn't. And they seem to be mutually exclusive.
The big problem is rotation.
I've digged through roughly at least seven different github projects, all of them re-implementing the same functionality/behavior, that I'm trying to achieve, but literally all of them failing miserably.
HPGrowingTextView for instance, which the official Facebook/FacebookMessenger/(and possibly WhatsApp) Apps makes use of, is one big piece of junk-code. Take your iDevice, open the Facebook App, go the the Chat, pop the keyboard and rotate your device. If you pay attention you'll notice the input-bar jumping slightly and leaving some blank space between the keyboard's frame and its own. Then take a look at Apples implementation in iMessage when the keyboard is shown. It's perfect.
Other than that the contentOffset and EdgeInset-hacking that the HPGrowingTextView library makes use of gives me nightmares.
So I wanted to do it myself and start from scratch.
Right now I've got a very slick, elegant and hack-less implementation of a growing UITextView, but one part is missing.
Elegant rotation.
When I simply adjust the frames to their respective new positions in the willRotateToInterfaceOrientation:duration: method, everything ends up working perfectly BUT I have the same problem that HPGrowingTextView(see Facebook App) has. A litte bit of space between the inputview and the keyboard while the rotation takes place.
I found out that when rotating the device to landscape, the portrait keyboard which is currently shown does not "morph" but rather disappears (sends the 'willHide' notification) and a landscape version reappears (sending the 'willShow' notification). The transition is a very subtle fade and possibly some resizing.
I re-implemented my project using the inputAccessoryView to see what happens then and I was pleasantly surprised. The inputAccessoryView rotates in perfect sync with the keyboard. There's no space/gap between the two.
Sadly I have yet to come up with an idea how to have the inputAccessoryView dock to the bottom of the screen and NOT disappear/move out of it alongside the keyboard...
What I don't want are hack-y solutions like,..."lowering the frame slightly in the toInterfaceOrientation's CoordinateSystem and then moving it back up when the didRotateFrom... was called."
I know of one other app that has managed to implement such behavior and it's the "Kik Messenger".
Does anyone have an idea, advice or a link that I haven't seen yet covering that topic?
Thanks a bunch!
Note: Once this problem is solved I will open source the project for everyone to profit because almost every implementation I was able to find over the course of the past few days, is a mess.
I recently ran into the same problem, and had to build out a custom solution as I wasn't entirely happy with the available 3rd party libraries. I've split out this implementation into it's own GitHub project:
MessageComposerView
From some simple testing on iOS 6.1 7 & 8 simulators the rotations seem to properly follow the keyboard. The view will also grow with text and resize automatically on rotation.
You can use a very basic init function like so to create it with screen width and default height e.g.:
self.messageComposerView = [[MessageComposerView alloc] init];
self.messageComposerView.delegate = self;
[self.view addSubview:self.messageComposerView];
There are several other initializers that are also available to allow you to customize the frame, keyboard offset and textview max height. See readme for more!
I have been successful at solving the problem in quite an elegant manner (I think,...).
The code will be released on Github next week and linked to in this answer.
--
How it's done: I made the rotation work by choosing the inputAccessoryView-way of doing it.
Nomenclature:
'MessageInputView' is a UIView containing my 'GrowingUITextView' (it also contains a "Send" Button and the background image).
'ChatView' is the view that belongs to the ChatViewController that displays all the Chatbubbles and has my 'MessageInputView' docked at the bottom.
'keyboardAccessoryView' is an empty UIView sized: CGRect(0,0,0,0).
I needed to figure out how to have the MessageInputView stick around on the screen when the keyboard was dismissed. That was the tricky part. I did this by creating another view (keyboardAccessoryView) and had my GrowingUITextView use it as its inputAccessoryView. I retained the keyboardAccessoryView because I'd need the reference to it later on.
Then I remembered some of the stuff I did in my other attempt (animating the MessageInputView's frames around the screen whenever a keyboard notification arrived).
I added my MessageInputView as a subview to my ChatView (at the very bottom). Whenever it is activated and the willShow: methods is called by a keyboard notification, I manually animate the MessageInputView's frame to it's designated position up top. When the animation finishes and the completion block executes I remove the subview from the ChatView and add it to the keyboardAccessoryView. This causes another notification to be fired off because the keyboard is re-loaded EVERY time the inputAccessoryView's frame/bounds are changed!. You need to be aware of that and handle it appropriately!
When the keyboard is about to dismissed, I convert my MessageInputView's frame to my ChatView's coordinate system and add it as a subview. Thus it is removed from my keyboardAccessoryView. I then resize the keyboardAccessoryView's frame back to CGRect(0,0,0,0) because otherwise the UIViewAnimationDuration will not match! Then I allow the keyboard to be dismissed and I have my MessageInputView follow it from above and eventually dock at the bottom of the screen.
This is quite a lot of work for very little gain though.
--
Take care.
PS: If someone figures out an easier way to do it (perfectly) let me know.
Here's a UITextView subclass that is working properly on iOS 9.3.1 and 8.3.1. It takes care of growing and shrinking with limits, while keeping the caret always in the right place and animating smoothly.
Sticking the view over the keyboard is trivial, with many solutions to be found easily, so it's not covered...
I could not find any made-solutions that were production ready so I ended up working on this from scratch. I had to work out a lot of little problems along the way.
Code comments should give you an idea of what's going on.
I have shared this on my Github, Contributions greatly appreciated.
Notes
Not tested to support landscape
Not tested on i6+
Demo
(after max height element becomes scrollable. Forgot to drag the demo, but this is working as expected as well... )
Subclass
class ruuiDynamicTextView: UITextView {
var dynamicDelegate: ruuiDynamicTextViewDelegate?
var minHeight: CGFloat!
var maxHeight: CGFloat?
private var contentOffsetCenterY: CGFloat!
init(frame: CGRect, offset: CGFloat = 0.0) {
super.init(frame: frame, textContainer: nil)
minHeight = frame.size.height
//center first line
let size = self.sizeThatFits(CGSizeMake(self.bounds.size.width, CGFloat.max))
contentOffsetCenterY = (-(frame.size.height - size.height * self.zoomScale) / 2.0) + offset
//listen for text changes
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(textChanged), name: UITextViewTextDidChangeNotification, object: nil)
//update offsets
layoutSubviews()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
//Use content size if more than min size, compensate for Y offset
var height = max(self.contentSize.height - (contentOffsetCenterY * 2.0), minHeight)
var updateContentOffsetY: CGFloat?
//Max Height
if maxHeight != nil && height > maxHeight {
//Cap at maxHeight
height = maxHeight!
} else {
//constrain Y to prevent odd skip and center content to view.
updateContentOffsetY = contentOffsetCenterY
}
//update frame if needed & notify delegate
if self.frame.size.height != height {
self.frame.size.height = height
dynamicDelegate?.dynamicTextViewDidResizeHeight(self, height: height)
}
//constrain Y must be done after setting frame
if updateContentOffsetY != nil {
self.contentOffset.y = updateContentOffsetY!
}
}
func textChanged() {
let caretRect = self.caretRectForPosition(self.selectedTextRange!.start)
let overflow = caretRect.size.height + caretRect.origin.y - (self.contentOffset.y + self.bounds.size.height - self.contentInset.bottom - self.contentInset.top)
if overflow > 0 {
//Fix wrong offset when cursor jumps to next line un explisitly
let seekEndY = self.contentSize.height - self.bounds.size.height
if self.contentOffset.y != seekEndY {
self.contentOffset.y = seekEndY
}
}
}
}
protocol ruuiDynamicTextViewDelegate {
func dynamicTextViewDidResizeHeight(textview: ruuiDynamicTextView, height: CGFloat)
}
How I fix this problem for me:
I have ChatViewController and FooterViewController as UIContainerView. Also, I have contentView outlet in FooterViewController. Then in ChatViewController I have:
override func becomeFirstResponder() -> Bool {
return true
}
override var inputAccessoryView: UIView? {
if let childViewController = childViewControllers.first as? FooterViewController {
childViewController.contentView.removeFromSuperview()
return childViewController.contentView
}
return nil
}
Another way is to create view programmatically and return as inputAccessoryView.
Recently I've wrote a blog post about this exact problem you've described and how to solve it with a short and elegant way by using keyboard notifications but without using the inputAccessoryView. And although this question is pretty old this topic is still relevant so here is the link to the post: Synchronizing rotation animation between the keyboard and the attached view
If you don't want to dive into the long explanation described in the blog post here is a short description with a code example:
The basic principle is to use the same method that everyone uses - observing keyboard notifications to animate the attached view up and down. But in addition to that, you have to cancel these animations when the keyboard notifications are fired as a consequence of interface orientation change.
Rotation example without animation cancellation custom on interface orientation change:
Rotation example with animation cancellation on interface orientation change:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:#selector(adjustViewForKeyboardNotification:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:#selector(adjustViewForKeyboardNotification:)
name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter]
removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]
removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
self.animatingRotation = YES;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
[super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
self.animatingRotation = NO;
}
- (void)adjustViewForKeyboardNotification:(NSNotification *)notification {
NSDictionary *notificationInfo = [notification userInfo];
// Get the end frame of the keyboard in screen coordinates.
CGRect finalKeyboardFrame = [[notificationInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
// Convert the finalKeyboardFrame to view coordinates to take into account any rotation
// factors applied to the window’s contents as a result of interface orientation changes.
finalKeyboardFrame = [self.view convertRect:finalKeyboardFrame fromView:self.view.window];
// Calculate new position of the commentBar
CGRect commentBarFrame = self.commentBar.frame;
commentBarFrame.origin.y = finalKeyboardFrame.origin.y - commentBarFrame.size.height;
// Update tableView height.
CGRect tableViewFrame = self.tableView.frame;
tableViewFrame.size.height = commentBarFrame.origin.y;
if (!self.animatingRotation) {
// Get the animation curve and duration
UIViewAnimationCurve animationCurve = (UIViewAnimationCurve) [[notificationInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
NSTimeInterval animationDuration = [[notificationInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// Animate view size synchronously with the appearance of the keyboard.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];
[UIView setAnimationBeginsFromCurrentState:YES];
self.commentBar.frame = commentBarFrame;
self.tableView.frame = tableViewFrame;
[UIView commitAnimations];
} else {
self.commentBar.frame = commentBarFrame;
self.tableView.frame = tableViewFrame;
}
}

Resources