How to increase animation transition seconds on ios uitableview deleteRowsAtIndexPaths - ios

How can we change animation transition speed when deleting a row from uitableview, i checked documentation i didn't find any argument where i can specify seconds for transition speed
[self.tableViewMeeting deleteRowsAtIndexPaths:deleteIndexPaths withRowAnimation:UITableViewRowAnimationLeft];
Let me know if there is any workaround to this.

Try this:
self.tableView.layer.speed = 0.3
Documentation: https://developer.apple.com/library/prerelease/ios/documentation/GraphicsImaging/Reference/CAMediaTiming_protocol/index.html

[CATransaction begin];
[tableView beginUpdates];
//...
[CATransaction setCompletionBlock: ^{
// Code to be executed upon completion
}];
[tableView insertRowsAtIndexPaths: indexPaths
withRowAnimation: UITableViewRowAnimationAutomatic];
[tableView endUpdates];
[CATransaction commit];

Why don't you try UIView animation.
[UIView animateWithDuration:2
delay:0.2
options:UIViewAnimationOptionCurveEaseInEaseOut
animations:^{
[self.tableView beginUpdates];
[self.tableView endUpdates];
}
completion:^(BOOL finished) {
// code
}
];

Related

Inserting UIView into TablewView with InsertRowsAtIndexPaths using ContentView

I lost a bit nerves on this case. I have UIView with few images as buttons. I would like to insert it with insertRowsAtIndexPaths:
I have no idea how to start with that, and I really searched every site on google. I even found something that works like a charm ( http://jackkwok.github.io/JKExpandTableView/ ) but I still cannot make it right.
Here is my didSelectRowAtIndexPath method:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
int selectedRow = indexPath.row;
NSLog(#"touch on row %d", selectedRow);
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
PrototypeCell *prototypeCell =(PrototypeCell *)[tableView cellForRowAtIndexPath:indexPath];
if (prototypeCell.stretched == NO){
[UIView animateWithDuration:1 animations:^{
prototypeCell.View.frame = CGRectMake(0, 0, 320, 120);}
completion: nil];
prototypeCell.stretched = YES;}
else {
[UIView animateWithDuration:1 animations:^{
prototypeCell.View.frame = CGRectMake(0, 0, 15, 120);}
completion: nil];
prototypeCell.stretched = NO;
}
// Start a new core animation transaction
[CATransaction begin];
// Set a completion block for the animation
[CATransaction setCompletionBlock:^{
//
// Update the data model to add a new section
// [_data addObject:[DetailView init] alloc];
NSInteger item = self.thingList.count-1;
// Animate the new section apperance
[tableView beginUpdates];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForItem:item inSection:0], nil] withRowAnimation:UITableViewRowAnimationFade];
NSLog(#"Array is: %#", _data);
// [tableView endUpdates];
}];
[CATransaction commit];
}
My head is already numb from that problem. I cannot even understand basic inserting, how I write what I would like to insert here?
Any Help appreciated!
Based on your described intent, I'm not entirely sure you need or want to insert table rows. It looks like what you are trying to achieve is to simply expand the height of a given row to reveal some extra buttons when that row is selected.
Changing the height of row is covered How to dynamically resize UITableViewCell height
Essentially you can have your PrototypeCell have some extra content (the buttons) that is usually clipped. When the view is selected, you simply change the height to make sure all of the buttons would be visible and you indicate to the tableview to reload the row that was selected.
Should you need to insert rows, this was covered in how to properly use insertRowsAtIndexPaths?
Good luck!

How to postion tableView to the last inserted section SMOOTH

EDIT:
I found this SO thread.
UITableView inserting section at top while scrolling
I have a tableView
I load it with 5 sections initially
Then I scroll up and when section 0 and row 0 is displayed, I load the next 5 sections and insert in table view using
[self beginUpdates];
[self deleteSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
[self insertSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
[self insertSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
[self insertSections:[NSIndexSet indexSetWithIndex:2] withRowAnimation:UITableViewRowAnimationNone];
[self endUpdates];
However my problem is the tableView gets position at section 0 and row 0 after the inserts
but I want to position at last row of last new section inserted (which is section 2 in code above)
I tried this both inside and outside begin/end updates
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:2];
[self scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionBottom animated:YES];
But it does it abruptly.
After inserting sections you can also scroll the table view to the intended index path using
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:2];
[UIView animateWithDuration: 1.0
animations: ^{
[tableView scrollToRowAtIndexPath:indexPath
atScrollPosition:UITableViewScrollPositionBottom animated:NO];
}
completion: ^(BOOL finished){
}
];
Note:
1) Insert the above code after [self endUpdates];
2) Please ensure that you keep the animated flag to NO using scrollToRowAtIndexPath: atScrollPosition: animated: while you are using it with UIView's animateWithDuration: animations: completion: API.
3) Also you can calculate the duration based on the distance (content size, height wise for vertical scroll), i.e duration can depend on the distance between source index path and destination index path.
Please try below code.
[self beginUpdates];
/*
Insert your sections
*/
[self endUpdates];
[self performSelector:#selector(scroll) withObject:nil afterDelay:0.1];
-(void)scroll
{
if([Your array count] > 0)
{
[YOURTABLENAME scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:2] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
}
I Hope above code is work for you.
Thanks

uitableview cell delete, custom animation

i am trying to create a custom animation for the deletion of table rows by overriding the deleteRowsAtIndexPaths method. i created the custom insert animation successfully and tried to reverse the animation for the deletion part but i get some errors: Assertion failure in -[UITableView _endCellAnimationsWithContext:]
and
NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.(rows after update must be equal to rows before the update).
If i don't override the deleteRows method and use tableView's default delete method, everything works fine, but not with my own method.
Here is the overriden method written in my tableViewController class:
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths
withRowAnimation:(UITableViewRowAnimation)animation
{
[[super tableView] insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates]; // Populates UITableView with data
[self.tableView beginUpdates];
for (NSIndexPath *indexPath in indexPaths) {
__block UITableViewCell *cell = [[super tableView] cellForRowAtIndexPath:indexPath];
if (cell) { // If indexPath isn't visible we'll get nil here
// Custom animation
CGRect frame = cell.frame;
frame.origin.x = cell.frame.size.width;
cell.frame = frame;
frame.origin.x = 0;
void (^animationsBlock)() = ^{
cell.frame = frame;
};
if ([[UIView class] respondsToSelector:
#selector(animateWithDuration:delay:usingSpringWithDamping:
initialSpringVelocity:options:animations:completion:)]) {
[UIView animateWithDuration:0.9
delay:0
usingSpringWithDamping:0.5
initialSpringVelocity:0
options:0
animations:animationsBlock
completion:NULL];
} else {
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationOptionCurveEaseIn
animations:animationsBlock
completion:NULL];
}
}
}
}
deleteRowsAtIndexPathsMethod:
- (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths
withRowAnimation:(UITableViewRowAnimation)animation
{
[[super tableView] deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates]; // Populates UITableView with data
[self.tableView beginUpdates];
for (NSIndexPath *indexPath in indexPaths) {
__block UITableViewCell *cell = [[super tableView] cellForRowAtIndexPath:indexPath];
if (cell) { // If indexPath isn't visible we'll get nil here
// Custom animation
CGRect frame = cell.frame;
frame.origin.x = 0;
cell.frame = frame;
frame.origin.x = cell.frame.size.width;
void (^animationsBlock)() = ^{
cell.frame = frame;
};
if ([[UIView class] respondsToSelector:
#selector(animateWithDuration:delay:options:animations:completion:)]) {
[UIView animateWithDuration:0.9
delay:0
options:UIViewAnimationOptionCurveEaseIn
animations:animationsBlock
completion:NULL];
} else {
[UIView animateWithDuration:0.9
delay:0
options:UIViewAnimationCurveLinear
animations:animationsBlock
completion:NULL];
}
}
}
}
Edit: this code is inside a method, that runs whenever i modify a cell's object. I modify an object and save it to core data, then i retrieve all the updated objects using [self populateData], then i want to run the animations: delete the old cell and insert the new updated cell somewhere at the bottom of the tableView
[coreDataStack saveContext];
[self populateData];
[self.tableView beginUpdates];
int oldIndex = indexPath.row;
int newIndex = newIndexPath.row;
if(oldIndex<newIndex){
[self deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationRight];
[self insertRowsAtIndexPaths:#[newIndexPath] withRowAnimation:UITableViewRowAnimationTop];
} else if (oldIndex>newIndex) {
[self deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationRight];
[self insertRowsAtIndexPaths:#[newIndexPath] withRowAnimation:UITableViewRowAnimationBottom];
} else {
[self.tableView reloadRowsAtIndexPaths:#[newIndexPath] withRowAnimation:UITableViewRowAnimationMiddle];
}
[self.tableView endUpdates];

Animate UITableViewCells to new position AFTER deletion animation is complete

I've got a UITableView that allows the user to delete rows in it by swiping them left (very much like the app Clear). My issue is that all the UITableViewCells below the one being deleted animate up to their new frames while the animation for the deleted cell is happening. I want it to happen after the animation for the deleted cell finishes.
My code:
//Starts updating the table
[CATransaction begin];
[CATransaction setAnimationDuration:JTTableViewRowAnimationDuration];
[CATransaction setCompletionBlock: ^{
[self.spenderTable reloadData];
}];
[tableView beginUpdates];
if (state == JTTableViewCellEditingStateLeft)
{
[self.list.spendrItems removeObjectAtIndex:indexPath.row];
//!!Animation happens here!!
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
}
[tableView endUpdates];
[CATransaction commit];
[tableView performSelector:#selector(reloadVisibleRowsExceptIndexPath:) withObject:indexPath afterDelay:JTTableViewRowAnimationDuration * 2];
Im also using this great UITableView.
Any ideas on how to achieve this?
Got this figured out....for anyone else interested. You can go two ways with:
Approach #1 (Wrap in animation block):
[UIView animateWithDuration:0.25 animations:^{
[[(JTTransformableTableViewCell *)[self.spenderTable cellForRowAtIndexPath:indexPath] contentView] setX:-320];
}completion:^(BOOL done){
[self.list.spendrItems removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}];
Remember to manipulate the datasource in the completion block to avoid an internal consistency exception.
Approach #2 (inherit from UITableView):
Override the animation like so:
- (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation{
for (NSIndexPath *indexPath in indexPaths)
{
UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath];
[cell setFrame:CGRectMake(320, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height)];
[UIView beginAnimations:NULL context:nil];
[UIView setAnimationDuration:1];
[cell setFrame:CGRectMake(0, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height)];
[UIView commitAnimations];
}
}

Change duration of UITableView animation (Insert/Delete rows with table beginUpdates)

Is there a way to change the duration of [table beginUpdates]/[table endUpdates] animations?
This is what I've tried, with no luck:
Option 1:
[UIView animateWithDuration:5.0 delay:0.0 options:(UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionOverrideInheritedDuration) animations:^{
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithArray:indexPaths] withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];
} completion:^(BOOL finished) {
}];
Option 2:
[CATransaction begin];
[CATransaction setCompletionBlock:^{
NSLog(#"I actually get called!");
}];
[CATransaction setAnimationDuration:5.0]; //but I don't work
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithArray:indexPaths] withRowAnimation:UITableViewRowAnimationTop];
[self.tableView endUpdates];
[CATransaction commit];
Why don't you try UIView animation.
[UIView animateWithDuration:2 delay:0.2 options:UIViewAnimationOptionCurveEaseInEaseOut animations:^{
[self.tableView beginUpdates];
[self.tableView endUpdates];
} completion:^(BOOL finished) {
// code
}];
Here is the Swift version of Gautam Jain's answer 😉:
UIView.animate(withDuration: 2.0, delay: 0.0, options: .curveEaseInOut, animations: {
self.tableView.beginUpdates()
// ...
self.tableView.endUpdates()
}) { isFinished in
// ...
}
#Gautam Jain 's solution is great. However, it has a problem, at least in iOS 9: the completion block will be executed at once but not when the animation completes.
I usually do like below, with a little more code but works better.
[UIView beginAnimations:#"animation" context:nil];
[UIView setAnimationDuration:0.25];
[CATransaction begin];
[CATransaction setCompletionBlock:^{
// completion block
}];
[self.tableView beginUpdates];
// updates
[self.tableView endUpdates];
[CATransaction commit];
[UIView commitAnimations];

Resources