UIScrollView scrollRectToVisible at custom speed - iphone

I have a UIScrollView and I'm calling scrollRectToVisible:animated:YES on it.
I would like to set the speed at which it is animated. Can that be done?

I ended up finding a solution. In my case, the scrolling was animated programmatically after launch, to mimic a slot machine (with 3 horizontal UIScrollViews). Was doing this with the scrollRectToVisible:animated: method.
I got to set a custom speed using UIView's beginAnimation:
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:(abs(rMid-pMid)*0.3)];
scrollMid.contentOffset = CGPointMake(rMid*320, 0);
[UIView commitAnimations];
AnimationDuration depends on the distance the scroller has to move between each "drawing".

A modern version with blocks:
[UIView animateWithDuration:1.0 animations:^{
[self.scrollView scrollRectToVisible:CGRectMake(...) animated:NO];
} completion:^(BOOL finished) {
...
}];

Related

how to hook UIView's animation?

I want to send my own message during UIView's animation process.
For example:
myView.alpha = 1.0;
[UIView animateWithDuration:2.0f animations:^{myView.alpha = 0.0;} completion:^(BOOL isFinished){}];
I want to send the message when iOS render the specific alpha's animation frame.
- (void) alphaChangedTo:(float) val
{
[sendMyOwnMessage:val];
}
as the animation is carried out by the iOS system, can I hook the function to finish my task?
Thanks!
Have you tried animation code block??
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelay:1.0];
[sendMyOwnMessage:val];
[UIView commitAnimations];
I am not sure if this is what you are looking for.. but I hope this helps.. Cheers!! Happy Coding.

UIView animation is leaking

In my viewcontroller i am using animation for changing the frame of UIButton and UIView, going from portrait to landscape user can see views growing but problem is animation is leaking everywhere and showing everything coming from different sides.
Here is the code
[UIView beginAnimations:Nil context:Nil];
[UIView setanimationDuration:1];
[view1 setFrame:CGRectMake(100,100,200,300)];
[UIView commitAnimations];
Thanks
Please elaborate more on what "leaking everywhere" means, and does it make a difference if you use block animation:
[UIView animateWithDuration:1.0 delay:0.0 options:nil animations:^{
[view1 setFrame:CGRectMake(100,100,200,300)];
}completion:^(BOOL done){
if (done) {
NSLog(#"animation complete");
}
}];
According to Apple's documentation:
In iOS 4 and later, use the block-based animation methods.
(Recommended)

Programmatically zoom in and out in iPhone: how can i set animation speed?

I have an app where i would like to dynamically zoom in and out an imageView.
I use [scrollView zoomToRect:CGRectMake(x,y,z,k) animated:YES]; to zoom in but i would like the animation to be slower ... is there a way to set the animation speed?
see my other answer. You can set the animation duration to something like 1.0 second.
instead of:
cursorView.center = locationOfTouch;
you have to set:
[UIView beginAnimations:nil context:NULL]
[UIView setAnimationDuration:1.0];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[scrollView zoomToRect:CGRectMake(x,y,z,k) animated:NO]; // NO is necessary!
[UIView commitAnimations];

iphone - UIScrollview - scrollRectToVisible with slow animation

I'm using UIScrollView and using scrollRectToVisible:animated
This is working fine for me.
But I want to scroll to a location slowly so that user can notice the effect.
Is it possible.
I'm trying the following code, but didn't succeed.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:2.0];
[scrlView scrollRectToVisible:<<some cgrect>> animated:YES];
[UIView commitAnimations];
The solution is actually pretty easy. If you use [scrollView scrollRectToVisible:frame animated:YES] the scrollview will start it's own animation, so in order to animate with your duration you have to use [scrollView scrollRectToVisible:frame animated:NO] within your animation.
In other words: This will work.
[UIView animateWithDuration:3
delay:0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{ [scrollView scrollRectToVisible:frame animated:NO]; }
completion:NULL];

iPhone: transitioning views with Core Animation

On the Mac, the best way for a simple cross-fade transition of views (without any custom keyframe timing) is to do something such as the following excerpt:
[[self animator] replaceSubview:aView with:bView];
Unfortunately the animator property isn't available on the iPhone. What's the best bet of doing this on the iPhone? Is it setting alpha channels on each view? Sample code would be excellent.
Thanks.
The basic way to animate views on the iphone is to use the UIView beginAnimations and commitAnimations calls. They allow you to modify the animatable properties of a view and have those changes animated.
For instance I have a custom view that is hidden and shown using this approach:
- (void) showAView:(CustomAView *)aView
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
aView.frame = CGRectMake(0.0f, 110.0f , aView.frame.size.width, aView.frame.size.height);
[UIView commitAnimations];
}
- (void) hideAView:(CustomAView *)aView
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
aView.frame = CGRectMake(0.0f, self.view.frame.size.height, aView.frame.size.width, aView.frame.size.height);
[UIView commitAnimations];
}
By wrapping the frame property change in the UIView beginAnimations/commitAnimations the change has a standard animation applied to it.
You can add additional properties to the animation by using UIView animation class methods eg.
[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self.view cache:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];