TableView animation when the cell is tapped - ios

I would like to animate the tableView cell when the user put is finger on it like in this image
and when the user stop pressing, the cell come back in the original size (my tableView is not using a custom cell but the default UItableViewCell). I looked around the net to find some tutorial or something useful but nothing, someone can tell me how can i do?

You can do it like this:
On Touch Down Event for UITableViewCell:
UIView.animateWithDuration(0.2, delay: 0.1, usingSpringWithDamping: 0.9, initialSpringVelocity: 5, options: [],animations: {
self.contentView.transform = CGAffineTransformMakeScale(0.95, 0.95)
}, completion: { finished in
})
And On Touch Up Event for UITableViewCell:
UIView.animateWithDuration(0.2, delay: 0, usingSpringWithDamping: 0.9, initialSpringVelocity: 5, options: .CurveEaseIn,animations: {
self.contentView.transform = CGAffineTransformMakeScale(1, 1)
},completion: { finished in
})
Hope this will help you.

Related

Change UIView's super view with animation in Swift

I have a VideoView (it is a child view of UIView).
By default, it is added to a UIView which is small view in the corner of the screen (I called it ParrentView1).
I have a button to zoom out VideoView. This button performs an action that removes VideoView from ParentView1 and adds it to a bigger view (called ParrentView2).
When I perform the code below, it works but the animation is weird. All I need is a zoom out animation from ParrentView1 to ParrentView2.
Here is my code:
VideoView.removeFromSuperview()
ParrentView2.addSubview(VideoView)
UIView.animate(withDuration: 0.8, delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 1,
options: .curveEaseOut,
animations: {
VideoView.frame = ParrentView2.bounds
}, completion: nil)
thanks for helping
The likely cause is that when adding it to the other view, it gets assigned a different frame. The solution is to make sure the animation starts at the original location. Something like:
CGRect originalRect = ParrentView2.convert(VideoView.frame, from:VideoView.superView);
VideoView.removeFromSuperview()
ParrentView2.addSubview(VideoView)
VideoView.frame = originalRect;
UIView.animate(withDuration: 0.8, delay: 0,
usingSpringWithDamping: 1,
initialSpringVelocity: 1,
options: .curveEaseOut,
animations: {
VideoView.frame = ParrentView2.bounds
}, completion: nil)
An improvement point: note that it is customary in Swift to start variable names with a lower case letter. It gets confusing when they don't.

UIView is not animating properly

so i'm trying to move my UIView out of frame, but instead of getting out of view it's just Animating to the center of ViewController
My code:
UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 4, initialSpringVelocity: 1.0, options: [], animations: ({
self.myTextField.center.x = 1300 // just a temporary value
}), completion: nil)
print(self.passwordTextField.center.x)//---> 1300
anyone knows why is it happening? and how can i fix it?

Perfect Swift3 Boing

To animate a bar opening...
#IBOutlet var barHeight: NSLayoutConstraint!
barHeight.constant = barShut?30:100
self.view.layoutIfNeeded()
t = !barShut?30:100
UIView.animate(withDuration: 0.15,
delay: 0,
options: UIViewAnimationOptions.curveEaseOut,
animations: { () -> Void in
self.barHeight.constant = t
self.view.layoutIfNeeded()
},
completion: {_ in
Screen.barShut = !Screen.barShut
}
)
That's great ...
But how would you make it boing like this?
(The only way I'd know to do this is, use CADisplayLink, with a few lines of code for a spring decaying.) Is this available in UIKit?
You can use the spring animation method that is built in to UIView:
func toggleBar() -> Void {
self.view.layoutIfNeeded()
let newHeight:CGFloat = !barShut ? 30:100
barShut = !barShut
barHeightConstraint.constant = newHeight
UIView.animate(withDuration: 1.5, delay: 0, usingSpringWithDamping: 0.2, initialSpringVelocity: 3, options: [], animations: {
self.view.layoutIfNeeded()
}, completion: nil)
}
You will want a longer animation duration than 0.15 of a second in order for the bounce to seem realistic; I think the values I have look pretty good, but you can play with them to get the exact effect you are after.
Since the animation duration is longer, I found that I could tap the button the triggered the open/shut while the previous animation was still running. Setting barShut in the completion block meant that the bar didn't react to all taps. I moved the toggle outside of the animation to address this.

Animate row of tableView to show actions (swipe left)

Have you seen on the iOS Facebook application, when sometimes you go check your notifications, the row of the tableView bounce in order to show that you can access actions? (Swipe left)
I would like to do the same, I succeeded to bounce the row left then right, but the actions behind the row don't show up.
I guess this is because nobody calls the method editActionsForRowAt.
My current code to bounce is:
let indexPath = IndexPath(item: 0, section: 0)
let contentView = tableView.cellForRow(at: indexPath)?.contentView
let original = contentView?.frame
var bounceOffset = original
bounceOffset?.origin.x -= 100
UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseOut, animations: {
contentView?.frame = bounceOffset!}) { _ in
UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.75, initialSpringVelocity: 0, options: .curveEaseOut, animations: {
contentView?.frame = original!
}, completion: nil)}
Do you have an idea of how I can do the same as Facebook?
A way to set [UITableViewRowAction]?

MKMapView doesn't adhere to animation delay

I'm fiddling with my MKMapView in Playground, and I'm having trouble animating it's frame.size.height. Here's my Playground code:
let vc = UIViewController()
vc.view.backgroundColor = UIColor.whiteColor()
XCPlaygroundPage.currentPage.liveView = vc
let map = MKMapView(frame: vc.view.frame)
map.backgroundColor = UIColor.redColor()
map.autoresizesSubviews = false
vc.view.addSubview(map)
UIView.animateWithDuration(2.0, delay: 1.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1.0, options: .CurveEaseInOut, animations: {
map.frame.size.height = 200
}, completion: nil)
You can tell when the actual animation is occurring by observing the map's red background color. The visible map's height decreases before the MKMapView's height. Setting autoresizesSubviews = false doesn't seem to do anything. So why does it seem as if there are two distinct animations?
UPDATE In my Playground, I removed all my MKMapView subviews, and the _MKMapContentView, one of the map's two subviews (the other being the "Legal" MKAttributionLabel), was removed, rendering the MKMapView with just it's red background. So the _MKMapContentView is being resized as a subview, but calling map.autoresizesSubviews = false isn't doing the trick. What gives?
This is a bit of a hack, if you will. I tried animating the map's height using animateWithDuration:delay:usingSpringWithDamping and a nonzero delay. My problem was that the _MKMapContentView wouldn't adhere to the delay, which is what explained the distinct animations.
So, with a little help from #Matt's famous delay solution, I scrapped my function delay and put the entire method in a delay block, so that any (sub)views would animate concurrently.
delay(3.5) {
UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 1.0, options: .CurveEaseInOut, animations: {
map.frame.size.height -= 200
}, completion: nil)
}
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
),
dispatch_get_main_queue(), closure)
}
Bit of a workaround, but it works. Still eager to learn why map's subviews didn't adhere to the function delay, if anyone has an idea.
I think you maybe to user this to avoid:
map.backgroundColor = UIColor.whiteColor()
vc.view.addSubview(map)
UIView.animateWithDuration(2.0, delay: 1.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 1.0, options: .CurveEaseInOut, animations: {
map.frame.size.height = 200
}, completion:
map.backgroundColor = UIColor.redColor()
)

Resources