How do i make a UIScrollView scroll automatically at a certain speed? - iphone

I am using touchesBegan and touchesMoved to track a user's interaction, so I can see where the user has touched the screen. What I want to do is when they take their finger to the last 20 pixels of the screen, have the UIScrollView scroll1 to scroll down. But how do i define a speed? Surely a while statement would do it far too rapidly, and a UIView animation would move it to a certain place but only the once.

You can do this by the following code :
[UIScrollView beginAnimations:#"scrollAnimation" context:nil];
[UIScrollView setAnimationDuration:REQUIRED_ANIMATION_DURATION];
[scroll setContentOffset:CGPointMake(REQUIRED_DISTANCE_X, REQUIRED_DISTANCE_Y)];
[UIScrollView commitAnimations];
Just set the values of REQUIRED_ANIMATION_DURATION to whatever time interval you want. A smaller time interval will mean faster speed.

Related

iPhone How to get photo taken effect like in camera application?

I am talking about that effect when user presses button to take photo, it shrinks and moves to toolbar? How is this achieved in general?
You can create this by both scaling (applying a transform) and moving (animating the position) of the image.
I wrote about a similar animation (the Open in Background animation from Safari on iPhone) in this blog post. Not all of the code is necessary but some part of it will be useful for the animation you are trying to do.
You should
Calculate the scale factor to make the image the appropriate size
Calculate the path you want to animate the image along.
Animate a scale with the calculated scale factor
Animate a position animation along the path (using a CAKeyframeAnimation)
Since you are doing two animations at once you could benefit from using a CAAnimationGroup.
Since you are animating down to a toolbar which probably is another part of the view hierarchy than where the image is you may need to use method like
- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view
and
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view
To translate coordinates between the different views.
so the shrink animation is achived by
[UIView beginAnimations:#"animationShrink" context:NULL];
[UIView setAnimationDuration:kSlideInAnimationDuration];
flipFlopContainer.transform = CGAffineTransformMakeScale(0.01, 0.01);
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(shrinkAnimationFinished:)];
[UIView commitAnimations];
after this animation in shrinkAnimationFinished method you need to define a path to follow and change the position of your view accroding to that path.
See the following thread for that animation
Resize and move a UIView with Core Animation (CAKeyFrameAnimation)

Scroll programmatically an UITableView with acceleration and deceleration

I'm trying to scroll a UITableView programmatically by tapping a button. It should be the same effect at doing it by hand or even a little bit longer in scrolling time.
First I tried to use scrollToRowAtIndexPath:atScrollPosition:animated: however it too fast and fixed in time. Also it does not have any acceleration and deceleration.
Any ideas how to scroll a UITableView programmatically as it has been scrolled by hand? I was thinking to send some custom touches to it. Do you think it could work?
Any ideas and help are greatly appreciated! Thanks!
UITableview is a subclass of UIScrollview. Like a UIScrollview, you can programmatically set UITableview's scroll position by setting the contentOffset property.
You change this property value inside an animation block so you have more control in the timing. You'll need to calculate the Y point position. Say you want to scroll to the 50th row, it might be 50 * rowHeight (assuming it's uniform height).
For example:
CGFloat calculatedPosY = 50 * rowHeight // do you own calculation of where you'd like it.
[UIView animateWithDuration:0.2
animations:^{theTableview.contentOffset = CGPointMake(0.0, calculatedPosY);}
completion:^(BOOL finished){ }];
There's another method you could use that provides more options as well
Take a look at this method:
+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^)(BOOL finished))completion
Set your button in cell subview and use this code to scroll table programmatically on touch event of button. And if you are not setting button as a subview of table cell then modify this code as needed.
CGPoint point = [button.superview convertPoint:button.frame.origin toView:self.tableView];
CGPoint contentOffset = self.tableView.contentOffset;
contentOffset.y += (point.y - contentOffset.y - self.navigationController.navigationBar.frame.size.height); // Adjust this value as you need
[self.tableView setContentOffset:contentOffset animated:YES];
Well, these are just a couple of "off the top of my head answers," but you could try using this method (in UIScrollView, of which UITableView is a subclass):
-(void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated
Once -(CGRect) rectForRowAtIndexPath: gives you the coordinates of the cell, you can then animate to the location of cell. But rather than calling it once, call it a couple of times with a smaller offset, then with a larger and larger offsets and then a smaller ones again. I am worried that it will be difficult to make it look smooth, though.
Still, if that doesn't work, perhaps changing contentOffset value inside of a UIView animateWithDuration:delay:options:animations:completion block would work. Maybe if you use the UIViewAnimationOptionCurveEaseInOut, you can get the effect you want just calling it once. If not, run several animations each part of way and covering great and greater distances before slowing down.

I want to animate, in fact fade, "within" drawRect

Is there a way to make drawRect animate FROM THE PREVIOUS SCENE to the next one?
(Amazingly) you can animate inside drawRect - try it. You can fade, translate or animate any other property.
However, it starts "from fresh", from blank. So for example if you put a fade-in animation block in drawRect, the previous scene with disappear and the new scene will fade up from white.
I want the screen to fade from the previous image (drawn in the previous cycle of drawRect) to the new image I have just drawn ... err, am drawing.
Is there a way to do that, perhaps trickily by manipulating what's going on with drawRect?
This would seem to be a very common use case - blending from one scene to the next.
Does anyone know the secret?
Of course, obviously this can be done in the core animation milieu or in many other ways, but having drawRect fade from one drawRect to the next is an obvious idea. Cheers.
Astounding update thanks to the genius of WrightCS.....
Thanks only to WrightCS, we now know that drawRect handles animations perfectly. Simply paste this code at the end of any drawRect and try it:
self.alpha = 0.0;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2];
self.alpha = 1.0;
[UIView commitAnimations];
it treats the entire drawRect, no matter how complex, as one enormous block and it wraps it in that animation. Yes, it even includes painted in offscreen areas, bitmap rendering or anything else. Everything gets animated. Who knew?
The problem at hand - how to make it start the animation from the previous scene rather than start from blank?
Drawrect Is invisible. It happens in the 'backbuffer' which the iOS displays on screen only when you're ready with drawRect. So you can definitely not animate while in drawrect. However, you can commit animation instrucions from just about anywhere, including drawrect. But the animation will be performed afterwards.
Animation requires timing and showing different frames to the user.
You CAN do that all by yourself (constantly forcing a redraw and doing something slightly different in drawrect each the time) but that's a lot of work, especialy if you want it done right.
Luckily iOS has many animation effects programmed for you. Either using Core Animation, or the (more simple and basic) animation in UIKit. But since it works by animating certain properties of views (eg the alpha of a whole view, or the rotation of a whole view, ...) you might need to rearrange your views and subviews to make good use of it.
E.g. Each horse limb is separate subview and you animate their transformations (no redraw needed, iOS will do the rest)
E.g. The old and new frame are two separate views and you animate the new frame (which is on top) from alpha 0 to alpha 1.
You can animate the alpha:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2.0];
myObject.alpha = 0.0;
[UIView commitAnimations];
if you are under iOS 5.0+, you can use the following:
...
[UIView animateWithDuration:0.3
delay:0.0
options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
//do your stuff here
}completion:^(BOOL finished) {
//completition stuff go here
}];
...
The best thing you can do is to use two UIView instances and animate between them.
When you are starting the animation you have to know which is the original and final state of the animation. That's difficult if it's only one view.

How to make a dragging UIView decelerating?

I have a UIView that can be dragged with the finger which I've implemented by using a UIPanGestureRecognizer. This allows me to drag a view horizontally ; left or right.
I make my view moving while receiving the following state from the UIPanGestureRecognizer (using the translationInView: method):
UIGestureRecognizerStateBegan
UIGestureRecognizerStateChanged
What happens for now, is the view stops moving as soon as the finger leaves the surface of the screen, which renders a non natural effect.
What I would like, would be that the dragging view decelerates starting from the state UIGestureRecognizerStateEnded by stopping smoothly.
Such a behavior is used by UIScrollView class and subclasses (UIWebView, UITableView, etc.).
I've tried severals approaches by calculating from the velocity, the time the touch occurred and stopped a stop distance, etc. But honestly I have a feeling of going nowhere to get a natural behavior. I even tried to use physics engine like Chipmunk. But I find this to complicated for what I try to achieve, and I'm pretty sure there should be a better (if not easier) solution.
Thanks a lot!
Jérémy
In your gesture recognizer, make it so that when your finger leaves the screen, you begin an animation to continue to scroll a bit, for a short duration, with an "Ease Out" transition.
Maybe something like:
[UIView beginAnimations:#"FingerOff" context:nil];
[UIView setAnimationDuration:0.25f];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationCurve:UIAnimationViewEaseOut];
[myView setFrame:newFrame];
[UIView commitAnimation];
You can substitute setFrame to something else - whatever establishes how you want to scroll or view - maybe even a setCenter. Make it a scroll a bit "further" than the current position.

UIImageView Zoom in and out

I want to change the images depending upon the zoom in or zoom out.
Means if i am zoom upto certain limit then it should show other image.
I added one UIImageView in UIScrollView and zoom in and out functionality is working but i am not getting how to change image during zoom in and zoom out.
And while changing image it should have fade effect in between.
Can any one help me for this?
UIScrollViewDelegate has a method - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale.
In this method you can check the scale and update the UIImageView with the appropriate UIImage.
For animating an image change, you can do:
[UIView beginAnimations:#"imageChange" context:NULL];
yourImageView.image = yourNewImage;
[UIView commitAnimations];
The change will have the fade effect you want.
About the zoom-in and zoom-out functionality I'm not sure, but you should check if the view is zooming (zooming property for your UIScrollView should be true) and then use the zoomScale property. I suppose you could do this with a timer, continuously checking if the view is zooming.