UIScrollView cancel/stop setContentOffset:<CGPoint>animate:YES animation - ios

I have a UIScrollView that I'm scrolling using setContentOffset:animate:YES but if the device rotates I want to be able to stop it. Is this possible? Or how would I go about writing my own implementation of the setContentOffset with animation? thanks!

scrollview.contentOffset = scrollView.contentOffset should stop the scrollView at its last scroll position.
Do this in willAnimateRotationTo..... in your viewController
If you have a UIScrollView sub-class you could do it in setFrame: , this is assuming the frame of the scrollview changes upon orientation change.

try to use this instead:
[UIView animateWithDuration:.25 animations:^
{
// replacement for setContentOffset:animated:
self.scrollView.contentOffset = scrollPositionBeforeKeyboardAdjustments;
}];
if you are struggling with the keyboard, see my answer
How to make a UITextField move up when keyboard is present?

Related

Stop a UITableView from automatically scrolling

I have a UITableView that I'm autoscrolling with setContentOffset. Like so:
CGFloat point = self.table.tblMinutes.contentSize.height - self.table.tblMinutes.bounds.size.height;
[self.table.tblMinutes setContentOffset:CGPointMake(0, point) animated:false];
[self.table.tblMinutes layoutIfNeeded];
[UIView animateWithDuration:20.0 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
[self.table.tblMinutes setContentOffset:CGPointMake(0, point - 500) animated:false];
} completion:nil];
What I want to achieve is for the scrolling to smoothly slow down and stop. I haven't been able to achieve that.
Calling [self.table.tblMinutes.layer removeAllAnimations] stops the animation, but moves the contentOffset for some reason, not achieving what I want.
I tried using the UIViewAnimationOptionBeginFromCurrentState option in a animation but that did nothing.
Any suggestions?
This is a subset of the classic problem of interrupting an existing animation and replacing it with a different animation.
The problem here is that if you merely remove the existing animation, you will jump to the end of that animation.
The solution to that is to consult the presentation layer and set the layer to that position before removing the existing animation. Now you are starting from midstream and can apply another animation.
Take a look at Kyle Truscott's excellent blog post about this for UIScrollView. UITableView inherits from UIScrollView, so in theory, it should work.

iOS - Cancel constraints animation

I'm animating constraints the usual way:
centerXsettingsBtnConstraint.constant = aValue;
[UIView animateWithDuration:.5
animations:^{
[self.view layoutIfNeeded];
} completion:^(BOOL finished) {
isAnimating = NO;}];
When I'm rotating the device I want to cancel this animation and update constraint with new values.
I tried to call
[self.view.layer removeAllAnimations];
but the animation still goes on till the rotation of the device is over. Only then I can update with new constraints, but during the rotation it's all a mess on the screen.
Is there a way to cancel constraints animation?
You must call the remove.. method on each subview:
[self.view.layer removeAllAnimations];
I'll use Swift:
for eachView in self.view.subviews {
eachView.layer.removeAllAnimations()
}
You need to remove animation on the specified view whose constraint or property is being changed in that particular animation.
Call removeAllAnimations() on the view on whom centerXsettingsBtnConstraint is applied.
You do not need to call removeAllAnimations() on each subview.
If you are not able to find the particular view, try to print someView.layer.animationKeys(), you will find the view on which animation is applied.
For Swift:
This will work to stop the animation of the constraint
yourComponent.layer.removeAnimation(forKey: "position")

UIView animateWithDuration... not working in iOS 8

I have a problem with [UIView animationWithDuration] when trying to animate a button when the keyboard come up. I have a notification tell me when the keyboard comes up then I have the ViewController call this method in the view. The button animates but not correctly. It just animates from a random position on the screen to the position it was at previously. The reason I bring this up again is because I've seen answers from other posts saying that setting the view's translatesAutoresizingMaskIntoConstraints property to YES in the ViewController will fix the problem, but it doesn't fix the problem.
[self.view setTranslatesAutoresizingMaskIntoConstraints:YES];
The animation is below:
- (void)animateForKeyboardAppearanceWithKeyboardSize:(CGSize)keyboardSize; {
[UIView animateWithDuration:0.3f
animations:^{
[self.sendSMSButton setFrame:CGRectMake(0,
self.frame.size.height - keyboardSize.height - self.sendSMSButton.frame.size.height,
self.frame.size.width,
self.sendSMSButton.frame.size.height)];
}];
}
Anyone know what's happening? Thanks in advance.
Have you ensured that this method isn't being called repeatedly? Also try subclassing the button, override -setFrame and double check to see if something else is setting the frame of your button while you are trying to animate it.

UIScrollView Jerky Scrolling

I have a big scroll view. I just wish to scroll around the scroll view with fixed steps on every button press. However when I write the following code the scroll view scrolling is very jaggy. Can someone explain why?
CGPoint lstructCurrentPoint = self.objScrollView.contentOffset;
lstructCurrentPoint.x += 400;
[UIView animateWithDuration:1.0f
delay:0.0f
options:UIViewAnimationOptionCurveLinear
animations:^{
[self.objScrollView scrollRectToVisible:CGRectMake(lstructCurrentPoint.x,
lstructCurrentPoint.y,
self.objScrollView.bounds.size.width,
self.objScrollView.bounds.size.height)
animated:NO];
} completion:^(BOOL finished)
{
}];
I do not use the animated:Yes arguement because I need custom scroll speeds between the different contentOffsets.
when you update your scroolview content frame while scrolling with an animation is like you try to update something that is in the past that is already changed... (probably is not the best explanation :D )
try with this in your animation option:
options:UIViewAnimationOptionCurveLinear|UIViewAnimationOptionBeginFromCurrentState
Why don't you just try using
[self.objScrollView scrollRectToVisible:CGRectMake(lstructCurrentPoint.x, lstructCurrentPoint.y,self.objScrollView.bounds.size.width,self.objScrollView.bounds.size.height) animated:YES];
instead of using this within UIView animation function.
Try to use
[self.objScrollView setContentOffset: CGPointMame(lstructCurrentPoint.x, lstructCurrentPoint.y) animated: YES];
instead of scrollRectToVisible.
scrollRectToVisible animated is an animation by itself.
With your current code, you are trying to animate the animation.
Hence it is jerky.
Just use to scroll the rect to visible:
[self.objScrollView scrollRectToVisible:CGRectMake(lstructCurrentPoint.x,lstructCurrentPoint.y,self.objScrollView.bounds.size.width,self.objScrollView.bounds.size.height) animated:YES];
No one can properly answer your question without knowing what content you have in the scrollView.
However, you can find out what is causing this yourself.
Profile the app on your device. This will open Instruments. Select the Core Animation tool.
When the app is running scroll the view so that instruments captures the data.
Now the Time Profiler section will tell you exactly what is taking a long time. Fix this and you'll have smooth scrolling.

UIView animation while scrolling a table

I have a UITableView in an iPad, and its right half side is covered with a view.
What I want is: when the user scroll the UITableView, I want the covering view to slide (with an animation) to the right until its origin.x is at the end of the right side of the table, so the UITableView is clear to see (just like in the Twitter app for iPad).
The problem is: You can scroll the UITableView and animate a view at the same time. When I start scrolling, I use didStartDraging and even DidScroll to create a [UIView animate...] method, but this method stuck my scrolling.
I tired with block animation. I tried with Gestures. I tried with CABasicAnimation, and it goes quite OK, but somehow all the UISubview "Touch area" in my sliding view stay at the old place (what I mean is that the view gets its new frame, but I can scroll a UIScrollView, which is a UISubview of my view, even when my finger is out of the superviews frame S-:, even that there is no view at that point on screen.)
Any thoughts how can I make the animation without damaging the scroll of the table? (and I heard about the timer in the K and the runloop common modes. Not so helping)
Thanks.
scrollview Delegate methods will be invoked for tableView also ............
you can use following code it worked for me and hope will work for you also.......
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[UIView animateWithDuration:0.3 animations:^{
[scrollView setFrame:CGRectMake(50, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height)];
}];
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[UIView animateWithDuration:0.3 animations:^{
[scrollView setFrame:CGRectMake(100, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height)];
}];
}
set x position of scrollView(tableView) as your requirement......
Have a happy coding....

Resources