moving a button vertically (Swift) - ios

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

Related

Slot machine animation using UITableView

I need to implement slot-machine animation according to provided design and timings.
It should perform infinite scroll, until some event will be triggered. After that animation, it should slow down and stop on defined position
For this task I have used next solution:
UITableView with fixed-height cell. It is the same cell with the only difference - icon or text (depends on indexPath.row)
Scroll is only down-to-up that's why I'm using last cell as start point in resetScrollPosition method
If first element reached, scroll position resets to start point
Animation performed as contentOffset change with linear option. In completion block, if animation is still needed, it's called again. If don't needed - slowing animation with easeOut option started
var isRolling: Bool = false
func startScroll() {
isRolling = true
UIView.animate(
withDuration: 0.05,
delay: 0,
options: .curveLinear,
animations: {
self.tableView.contentOffset.y -= self.rowHeight
},
completion: { _ in
if self.isRolling {
self.startScroll()
} else {
self.resetScrollPosition
UIView.animate(
withDuration: 0.7,
delay: 0,
options: .curveEaseOut,
animations: {
self.tableView.contentOffset.y -= 8 * self.rowHeight
},
completion: nil
)
}
})
}
private func resetScrollPosition() {
tableView.reloadData()
tableView.contentOffset.y = startOffset
tableView.reloadData()
}
func stopScroll() {
isRolling = false
}
The problems:
After calling resetScrollPosition, in animations completion block, tableviews contentOffset.y value is updated but tableView stays on the same position. I have tried to change direct contentOffset changing to setContentOffset, scrollToRow, scrollToRect, wrap it in main queue - no changes
Slowing animation should scroll 8 items. It's performed but first 6 items aren't visible during animation, only the last two.
Check the issue gif (jump 2 -> 11 is ok):
Replaced UITableView with UIScrollView
Uploaded code to gist - https://gist.github.com/OlesenkoViktor/76845c5448b421ead0a2303af2b1161d
Thanks #Paulw11 for his idea

iOS - How To Fix This Animation In Table View Cell To Go Left and Then Back Again

The goal is to have the first table view cell content move left for a time and then back again.
The bigger goal is that we will bounce the cell's content view slightly to the left and bounce in a red box then return the cell to normal.
Although similar to another SO question, the answer does not reveal how to do this. Plus, this question would apply to anyone who wants to animate moving the cell content to the left temporarily and then back again. Thus, that's why it's a separate question.
The environment is iOS 11+ and iPhone app.
I have a new Table View project created that is animating the contentView moving via transform. However, it doesn't seem to remotely start in the normal position and then move as desired. The content starts off centered and then moves into place instead.
How can I get the contentView to animate moving a little to the left and then back again into its normal position?
Project: https://github.com/mikefinney/peekabooswipe
I rewrote your code a little. You were applying a transform to the content view and the documentation suggests instead animating the center property when shifting the location.
func applyCellAnimations() {
let originalCenter = contentView.center
let offsetCenter = originalCenter.applying(.init(translationX: -44, y: 0))
animateToCenter(offsetCenter) {
DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
self.animateToCenter(originalCenter)
})
}
}
private func animateToCenter(_ center: CGPoint, completionHandler: #escaping () -> Void = { }) {
layoutIfNeeded()
UIView.animate(
withDuration: 1,
delay: 0,
options: [.curveEaseInOut],
animations: {
self.contentView.center = center
}, completion: { didComplete in
if didComplete { completionHandler() }
})
}

decrease speed in upward textview animation (swift3)

My code below scrolls the textview to the top. How can I make the animation half of its current speed. theTextView is the textview i am using.
theTextView.contentOffset = .zero
theTextView.setContentOffset(.zero, animated: true)
How about a simple UIViewAnimation without using the included .setContentOffset method of UiTextView. Could be something like this:
var speed: CGFloat = 0.3 // speed in seconds
UIView.animate(withDuration: speed, animations: {
theTextView.contentOffset = .zero
}, completion: nil)
You can change the animation speed by changing the speed variable.

Swift CGAffineTransform Change Initial Size of Animated View

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.

Slide up animation in Swift

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)

Resources