I am making a Karaoke app that a user needs to be able to start-over by pressing a button.
I have a UIScrollView that contains a large block of text in a UITextView.
I am using UIView.animateWithDuration to scroll the text for a certain duration. I need to be able to interrupt/stop the animation and reset it back to the beginning.
Unfortunately, for me, whenever I stop the animation, the UITextView disappears and I can't seem to re-add it.
let bottomOffset:CGPoint = CGPoint(x: 0, y: self.textView.frame.size.height)
UIView.animateWithDuration(NSTimeInterval(duration),
delay: NSTimeInterval(0.0) ,
options: UIViewAnimationOptions.CurveLinear,
animations: {
self.scrollView.setContentOffset(bottomOffset, animated: false)
},
completion: { finished in
reset()
}
)
I am stopping the animation by calling:
UIView.animateWithDuration(0.0, animations: {
self.scrollView.scrollRectToVisible(CGRect(x: 0, y: 0, width: 0, height: 0), animated: true)
})
I need to figure out how to:
Stop the animation
Keep the UITextView visible
Scroll the UIScrollView back to the top
Start scrolling again
My codebase is in Swift, but I will gladly accept answers in Objective-C.
Related
I want to have my tableview increase in size and moves up when scrolled down while also keeping the constraint to the bottom layout. I thought I would try CGAffineTransform.
func MoveUP() {
// pop up login screen
let bottom = CGAffineTransform(translationX: 0, y: -30)
UIView.animate(withDuration: 0.7, delay: 0.2, options: [], animations: {
// Add the transformation in this block
// self.container is your view that you want to animate
self.numberOfProperties.transform = bottom
self.tableview.transform = bottom
}, completion: nil)
}
The problem with this is that it moves up the tableview but does not keep the proper constraints to the bottom. Many apps seems to have this behavior, what tool am I missing in order to achieve this result?
After making an animation on a UITextField, if I tap on my UITextField, it disappears. Why does this happen and how can I fix it? I'm not sure how to search for this online. Here's my animation code that I'm doing in viewDidAppear
UIView.animateWithDuration(1.0, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.0, options: [], animations: {
self.userName.center.x += self.view.bounds.width
self.userName.alpha = 1.0
}, completion: nil)
Before this I set the UITextField to be off the screen. So what my animation is supposed to do is start from outside of the screen and animate it to be visibly in the center of the screen. Don't know if that makes a difference in this question though.
The animation works great, it's just that when I try to tap on the UITextField to enter a username, the entire UITextField just disappears and I'm left with just a keyboard. I'm sure it's something simple I'm missing. Can someone lead me in the right direction?
How can I animate a stack view to slide up starting from x=0 up to y=500, I have the following method in the viewDidLoad() which does a growing effect.
StackView.transform = CGAffineTransformMakeScale(0.0, 0.0)
And then I added a growing effect in the viewDidAppear() method
UIView.animateWithDuration(0.4, delay: 0.0, options: [], animations: {
self.StackView.transform = CGAffineTransformIdentity
}, completion: nil)
After the viewDidLoad method executes, the stack view is minimized. When the viewDidLoad method completes, the viewDidAppear method is invoked, and the animation begins and the stack view begins to grow. The animation stops when the stack view reaches it's original size.
Although is a nice effect that's not what I want to accomplish, I want the animation to slide up from x = 0 and stops at y = 500 I tried to add the following code in the viewDidLoad to accomplish this effect, but I still get the same growing effect. Any suggestions on how to accomplish this?
StackView.transform = CGAffineTransformMakeTranslation(0, 500)
You´re almost there just make a few changes
// These values depends on the positioning of your element
let left = CGAffineTransformMakeTranslation(-300, 0)
let right = CGAffineTransformMakeTranslation(300, 0)
let top = CGAffineTransformMakeTranslation(0, -300)
UIView.animateWithDuration(0.4, delay: 0.0, options: [], animations: {
// Add the transformation in this block
// self.container is your view that you want to animate
self.container.transform = top
}, completion: nil)
I am hiding my navigation bar and a UIView under it that acts as a extension bar to it when I scroll my page.
My app is built like:
VC that holds a container view with an embedded table view.
From the table view I have delegates that notify VC1 once a user scrolls up or down.
My problem now is that the animation dont looks that good. What I am trying to do is to animate the extension bar to animate up or down with a fade in or fade out effect as well. When that occurs I also update the top contraint on my container view so that the table view will fill the whole screen. (I am not sure if I use layoutneeded() right or if something else should be used when updating constraints)
My code:
func ContainerTableViewControllerScrolledUp(controller: ContainerTableViewController) {
self.navigationController?.setNavigationBarHidden(false, animated: true)
println("UP")
UIView.animateWithDuration(
1.5,
delay: 0,
usingSpringWithDamping: 0.7,
initialSpringVelocity: 0.5,
options: nil,
animations: {
self.extensionV.alpha = 1
self.tableVConst.constant = 0
}, completion: { finished in
self.view.layoutIfNeeded()
}
)
}
func ContainerTableViewControllerScrolledDown(controller:ContainerTableViewController) {
self.navigationController?.setNavigationBarHidden(true, animated: true)
println("DOWN")
UIView.animateWithDuration(
1.5,
delay: 0,
usingSpringWithDamping: 0.7,
initialSpringVelocity: 0.5,
options: nil,
animations: {
self.extensionV.frame.origin.y = CGFloat(-10)
self.tableVConst.constant = -41
self.extensionV.alpha = 0
}, completion: { finished in
self.view.layoutIfNeeded()
}
)
}
extensionV is the extension view
tableVConst is the top constraint for my container view that holds my table view
So how should I edit my code in order to get the extension view to animate up/down with a fade in/fade out effect?
Instead of calling self.view.layoutIfNeeded() in the completion block, try calling it inside the animation block on the last line before it returns.
I'm making a login screen in iOS 8 using Storyboard (Xcode6 beta7). Here's an image of the basic idea:
Pressing inside any of the textfields brings up the keyboard, and pressing outside any of them dismisses it again using:
self.view.endEditing(true)
So pressing the login button makes the keyboard disappear.
When the user enters invalid login credentials I want the dialog view to increase in height and show some error message:
I'm doing this with an animation using the following function:
func increaseHeight(view: UIView, increment: CGFloat) {
UIView.animateWithDuration(1.0,
delay: 0.0,
usingSpringWithDamping: 0.3,
initialSpringVelocity: 3.0,
options: UIViewAnimationOptions.CurveEaseInOut,
animations: ({
view.frame = CGRect(
x: view.frame.origin.x,
y: view.frame.origin.y,
width: view.frame.width,
height: view.frame.height + increment
)
}),
completion: nil
)
}
If I invoke this function, let's say by pressing the login button, it's all working as I want. The animation is performed and the error message is shown.
To the the problem
However, if I first start to edit any of the textfields (i.e. bringing the keyboard up) and then press the login button, the animation is performed – but it bounces back to its original height.
How can I make the height increment persist after the keyboard has been dismissed?
If you are using auto-layout you need to change constraints not frame of the view.
heightOfViewConstraint.constant += increment
UIView animation....{
self.layoutIfNeeded()
}
This should keep your view stretched, but only if you are using auto-layout and constraints.