I'm performing the following animation on a UIButton called "view":
UIView.animateWithDuration(duration, animations: {
self.view.transform = CGAffineTransformMakeScale(0.5, 0.5)
})
When the animation runs, it immediately doubles the size of the UIButton, then shrinks it back to its original size. So the animation does work.
But what I would like to have happen is for the animation to start with the UIButton at its original size and then shrink it to half of that size.
Basically, I want the original size of the UIButton to appear at the start of the animation, not the end. How can I do this?
(I tried changing the view.frame.size.height and width properties but that didn't seem to change the appearance of the UIButton or the animation on it.)
You may have conflicting animations.
UIView.animateWithDuration(
duration,
delay: 0,
options: .BeginFromCurrentState,
animations: { () -> Void in
self.view.transform = CGAffineTransformMakeScale(0.5, 0.5)
}) { (completed:Bool) -> Void in
self.view.transform = CGAffineTransformMakeScale(0.5, 0.5)
// or, to reset:
// self.view.transform = CGAffineTransformIdentity
}
Starting this animation from .BeginFromCurrentState may reduce or remove glitches entirely, and taking action upon completion (which logic you can base upon completed:Bool) will also tell Core Animation what to do when an animation is interrupted.
Related
I have a custom full screen alert with a low alpha background. The default modal animation slides from bottom to top. I would like the alert to fade-in and fade-out. I am UIViewControllerTransitioningDelegate + UIViewControllerAnimatedTransitioning. In my animateTransition(using:) method I have something similar to this:
func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView
if let toView = transitionContext.view(forKey: .to) {
toView.frame = containerView.frame
containerView.addSubview(toView)
UIView.animate(withDuration: duration, delay: 0,
usingSpringWithDamping: 0.5,
initialSpringVelocity: 0,
options: UIViewAnimationOptions.allowUserInteraction,
animations: {toView.alpha = 1})
transitionContext.completeTransition(true)
}
}
The problem is that the fade effect looks good at around 1-1.5 seconds; however, the user cannot tap any buttons such as "yes" or "no" during this transition animation. I attempted the following:
Move the call transitionContext.completeTransition(true) outside the completion handler for the animation so it happens as soon as the animation starts
Adding the UIViewAnimationOptions.allowUserInteraction option
Starting the alpha at 0.1 (and 1.0) as was suggested in another post
I did notice UIViewControllerInteractiveTransitioning but it seems this is only allowing interaction with the animation itself
The only way I can see this working is if I do the fade-in effect in viewDidAppear in the VC and the fade-out effect in UIViewControllerAnimatedTransitioning. Feels bad man. What is the correct way to do this?
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)
How do you animate only the contents of a UIImageView? If the image view is centered in the middle of the screen how do you animate the image to slide in from the left but only be shown within the frame of the image view?
As if you are looking at a wall with a window and someone walks by. You don't see them until they are in the frame of the window.
The code below certainly does not do it. I had it a few days ago with ease and erased it and now I can't remember how I did it. It was pretty simple but now it's driving me crazy.
self.lockImages[button.tag].center.x -= self.lockImages[button.tag].bounds.width
UIView.animateWithDuration(2.0, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1.0, options: nil, animations: { () -> Void in
self.lockImages[button.tag].center.x += self.lockImages[button.tag].bounds.maxX
}, completion: { (Bool) -> Void in
})
UIImageView a subclass of UIView, inherits the .clipsToBounds() method, which will create the desired effect.
I am making a wac a mole game in swift and need to know how to make a button (Mole image) move up and down popping out and back into its hole (Above and back behind a imageview).
Change the button's frame to incrementally move up and down with animation. Here is some sample code (yAxisMovement is a positive number to move the button up or a negative number to move the image down):
var duration: NSTimeInterval = 1.0
UIView.animateWithDuration(duration, animations: { () -> Void in
button.frame = CGRectMake(
button.frame.origin.x,
button.frame.origin.y - yAxisMovement,
button.frame.size.width,
button.frame.size.height)
})
The answer above doesn't work anymore due to deprecated methods
let duration: TimeInterval = 1.0
UIView.animate(withDuration: duration, animations: {
self.yourImage.frame.origin.y = -50
}, completion: nil)
use -50 to make it move downwards and 50 to make it move upwards
I am attempting to animate a tab bar to move from below the bottom of the screen to the top while simultaneously adjusting a view's height to shrink by the height of the tab bar. Essentially, I have a "hidden" tab bar that when it unhides should animate into view and the displayView should adjust for the space the tab bar now takes up.
However, the animation is jumpy for the display view. It seems that the display view animates fine, but the subviews automatically adjust their height without any animation. Any direction on fixing this would be appreciated.
I will accept aid in either objective-c or swift, as the translation is fairly easy.
//Displays tab bar with slide up animation. If animated is false, all other params are unused
func displayTabBar(animated:Bool, duration:NSTimeInterval = 0.5, delay:NSTimeInterval = 0, options:UIViewAnimationOptions = UIViewAnimationOptions.CurveLinear, completion:((Bool) -> Void)? = nil){
if(animated){
UIView.animateWithDuration(duration, delay: delay, options: options, animations: {
self.adjustTabBarDisplayed()
}, completion: completion)
UIView.animateWithDuration(duration, delay: delay, options: options, animations: {
self.adjustDisplayViewTabDisplayed()
}, completion: nil)
}
else{
self.adjustTabBarDisplayed()
self.adjustDisplayViewTabDisplayed()
}
}
//Adjusts frame of tab bar to display tab bar
private func adjustTabBarDisplayed(){
self.tabBar.frame = CGRectMake(0,UIScreen.mainScreen().bounds.height - self.tabBar.bounds.height, self.tabBar.bounds.width, self.tabBar.bounds.height)
}
//Adjusts frame of display view to match displayed tab bar
private func adjustDisplayViewTabDisplayed(){
self.displayView.frame = CGRectMake(0,0,self.displayView.bounds.width, UIScreen.mainScreen().bounds.height - self.tabBar.bounds.height)
}
When you modify a view's size, it doesn't lay out its subviews immediately. Instead, it sets a flag indicating that it needs layout. Later, after the system has finished dispatching the event that ended up calling displayTabBar, it runs the display refresh code. The display refresh code finds views that have the needs-layout flag set and tells them to lay themselves out (by sending them layoutSubviews).
Here, you are changing your display view's size inside an animation block. Therefore change to your display view's frame will be animated. But the frames of its subviews are changing outside the animation block; they're changing later during the layout phase. You need to make them change inside the animation block.
Lucky for you, that's easy. Just call self.displayView.layoutIfNeeded() inside the animation block. Also, you only need one animation block, since all of the animation parameters are identical:
func displayTabBar(animated:Bool, duration:NSTimeInterval = 0.5, delay:NSTimeInterval = 0, options:UIViewAnimationOptions = UIViewAnimationOptions.CurveLinear, completion:((Bool) -> Void)? = nil){
if(animated){
UIView.animateWithDuration(duration, delay: delay, options: options, animations: {
self.adjustTabBarDisplayed()
self.adjustDisplayViewTabDisplayed()
// ADD THIS LINE
self.displayView.layoutIfNeeded()
}, completion: completion)
}
else{
self.adjustTabBarDisplayed()
self.adjustDisplayViewTabDisplayed()
}
}
Use the below line of code in animation block
scrollView.layoutIfNeeded()