Prevent animation being intercepted when view is scrolling

Discussion in 'iOS Programming' started by dantastic, Feb 7, 2011.

  1. dantastic, Feb 7, 2011
    Last edited: Feb 7, 2011

    dantastic macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #1
    Caught this one in testing... nasty.

    I've a UITableView that I can add cells to. I've a button up in the navigation bar that calls this function:
    Code:
    - (IBAction)addCell {
    	... create a new object to be added. 
    	// Add it to the array containing the UITableView
    	[myArray addObject:newObject];
    	
    	// Pop the new cell into the UITableview
    	[self.tableView beginUpdates];
    	[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[myArray count] - 1 inSection:3]] withRowAnimation:NO];
    	[self.tableView endUpdates];
    
    	// Scroll down to the newly created cell
    	// Shift the view so the keyboard doesn't cover the cell. 
    	[UIView beginAnimations: @"anim" context: nil];
    	[UIView setAnimationBeginsFromCurrentState: YES];
    	[UIView setAnimationDuration: 0.3f];
    	[self.tableView setContentOffset:CGPointMake(0.0, [myArray count] - 1 * 44)];
    	self.tableView.frame = CGRectOffset(self.tableView.frame, 0, -keyboardOffset);
    	[UIView commitAnimations];		
    }
    
    This all works fine as long as the table view is not scrolling at the time ob the *add* button being pressed. If the UITableView is currently being animated by the scrolling motion the scroll will somewhat pause for 0.1 sec. then my animation to shift the view work for like 0.1 sec. then the scrolling takes over again.

    The problem is that I end up with a view in a state sometimes difficult to recover from.

    I realize these animations are intercepting each other causing this. the preferred option would be if I could cancel the scrolling animation in progress and perform my own per the function above.

    If that's a no-go - any better options of doing this?
     
  2. cnstoll macrumors 6502

    Joined:
    Aug 29, 2010
    #2
    Since UITableView is a subclass of UIScrollView, you can declare yourself as a delegate for the UIScrollViewDelegate protocol and use the scroll view's delegate methods.

    One such method that may interest you is scrollViewDidEndScrollingAnimation: which will notify you when the view is done animating the scrolling animation.

    One way you could go about using that is with some sort of temporary state variable set based on whether or not the dragging property of the table view is YES, meaning it's currently scrolling. Then you could choose not to start that animation in your addCell method, but instead start the animation in scrollViewDidEndScrollingAnimation: (as long as that temporary state variable says you can).

    It's pretty hackish, but it should work.

    http://developer.apple.com/library/...tml#//apple_ref/occ/intf/UIScrollViewDelegate
     
  3. dantastic thread starter macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #3
    Not a bad idea but the user would have to wait until the tableview stops scrolling. I wouldn't be happy with that approach. Can't seem to find a way of actually stopping the scrolling so I'm pondering alternatives.

    cheers!
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    I wonder if setting the scrollEnabled property would stop scrolling immediately.
     
  5. dantastic thread starter macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #5
    Disabling scrolling will 'pause' the animation. It will continue from where it was stopped when you enable it again. I've tested that one already :rolleyes:

    I think I have come up with a workaround though. I'll just put the button to add in a cell of its own down the bottom. That way the textField will be in view already so no scrolling needed.

    It's a workaround but it's better than having a crappy app.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Okay. I guess it would've helped us answer if you perhaps had let us know what you had already tried. If you haven't already, I would suggest reading Mike Ash's excellent blog article: Getting Answers.
     

Share This Page