When a user makes a flick gesture on a UIScrollView, the UIScrollView gets a momentum and starts moving, then slow down and finally stop.
But how can I make this happen programmatically? I mean without a finger flicking, the UIScrollView just start moving automatically and then slow down to a speed of 0.
In my app I have made my UIScrollView unlike a normal UIScrollView (say it looks like a roller), so I want make a hint to the user that he can scroll it (and then everything get started!)
I have googled a lot but there seemed no way to solve my problem. The setContentOffset just couldn't make the natural "slow down and stop at somewhere ahead" effect.
Any idea would be appreciated.
Thanks in advance.
Try, something like this >
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDelay:.8];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDuration:(abs(1-3)*0.3)];
self.myScroll.contentOffset = CGPointMake(0, 500);
[UIView commitAnimations];
It is not currently what you need, but you can customise this code, and may be all be ok)
or use this code>
[UIView animateWithDuration:2.
delay:0.3
usingSpringWithDamping:1.
initialSpringVelocity:7.
options:UIViewAnimationOptionCurveEaseInOut animations:^{
//Animations
self.myScroll.contentOffset = CGPointMake(0, 500);
}
completion:^(BOOL finished) {
//Completion Block
}];
I think it is like you want(animation with damping like swipe effect)
Related
I have a UIView drawn in Storyboard which holds some buttons and which is called viewHolder.
I get a higher position rectForAnimationBefore
and a lower position rectForAnimationAfter
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:nil completion:^(BOOL finish){
[self.viewHolder setFrame:rectForAnimationAfter];
}];
when this is excuted,the viewHolder do move down.After a second,it comes up as nothing was done.
I want to moveDown,but don't want moveUp Automatically.
Because autolayout in Storyboard?
By the way ,how to move it smoothly?
Thank you guys.
UPDATE:
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self.viewHolder setFrame:rectForAnimationAfter];
}
completion:^(BOOL finish){
}];
when I change code like this ,I can't move it down .
Here is my solution
unlock the autolayout!!!
First thing your animation block is nil. Here you should move your view down. At completion you are setting back you view without using any animation. For smoothness you need to move back your view animatedly.
I have UIScrollView which can zoom an UIView. One time I want to zoom out to the default state.
Here is my code.
[UIView animateWithDuration:0.3
animations:^{
[scroll zoomToRect:self.view.bounds animated:NO];
} completion:nil];
Wow. It jumps like hell. Seems like it sets zoomScale = 1 and then animating the frame.
I'll add a line.
[UIView animateWithDuration:0.3
delay:0 options:UIViewAnimationOptionBeginFromCurrentState
animations:^{
[scroll zoomToRect:self.view.bounds animated:NO];
} completion:nil];
Works just fine. So how UIViewAnimationOptionBeginFromCurrentState helps in this case? From help I know
Start the animation from the current setting associated with an already in-flight animation.
But there is NO in-flight animations.
I found answer, it looks expedient but work fine.
write animation code in other place use method like 'performSelector:'
then it recognize correct current state
I have been trying to build an entry effect for a logo to come from the top of the screen to the bottom and remain there when a new view loads in my application. I have seen all of the tutorials that use NSTimer to bounce an image but once my logo hits the bottom it needs to exit. I'm going to read up on animation block codes to see if my solution resides there.
Apologies I'm a new be and am very grateful for the assistance.
Set logo frame to top and then:
[UIView beginAnimations: #"moveLogo" context: nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:1.0];
[UIView setAnimationCurve: UIViewAnimationCurveLinear];
logoToMove.frame = CGRectMake( final frame at the bottom );
[UIView commitAnimations];
logoToMove is your logo, give it an outlet and hook it in xib.
So you will set the initial frame and in the animation - the final frame. The animation will do the rest of the job.
Change UIViewAnimationCurveLinear to a desired one if you don't like that. Also the duration to speed up or slow down the movement.
To remove the view at the end of your animation, the easiest way would be to use blocks :
logoToMove.frame = topRect;
[UIView animateWithDuration:duration
animations:^{
logoToMove.frame = bottomFrame;
}
completion:^(BOOL finished) {
[logoToMove removeFromSuperview];
}
];
Doing it like that gives you control over the animation and on what to do once it's finished in a single method
I use a UIView animation to randomly animate 5 squares (UIButtons) around the screen. Depending on a user selection, there are anywhere from 2 to 5 squares visible. When only 2 are visible, the other three's hidden values get set to YES, so they are actually still animating (right?), they just aren't visible. But when only 2 are visible, the animation is smooth, but when all five are visible, the animation gets choppy. I'm not really sure how to describe it, because the squares are still moving at the correct speed and moving to the correct points; the choppiness isn't terrible, just bad enough to be noticeable. Is there any way to get rid of it? This is the code I use to animate the squares:
Edit: changed animations to block:
[UIView animateWithDuration:animationSpeed
delay:0
options:UIViewAnimationOptionCurveLinear
animations:^{
view.center = destPoint;
}
completion:^(BOOL finished){
if([view isEqual:squareThree])
[self moveBadGuys];
}
];
/*for(UIButton* button in squareArray) {
if(!shouldMove)
return;
[UIView beginAnimations:#"b" context:nil];
[UIView setAnimationDuration:animationSpeed];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
view.center = destPoint;
[UIView commitAnimations];
}*/
Edit: the view presenting this is the third in a stack of three UIViewController presented with
ViewController* controller = [[[ViewController alloc] init] autorelease];
[self presentModalViewController:controller animated:NO];
Does this way of presenting views eat up memory?
There are a few things that can cause this. It always comes down to how complex the content is. Also, simulator can be really bad about handling animation, so be sure you are testing on real hardware.
Are there large images on the buttons? Are the buttons casting shadows? Those things can slow it down.
Also- use block based animation. Not the old begin-commit methods.
Not exactly sure why it's slow, but have you tried nesting the thing differently?
if(!shouldMove)
return;
[UIView beginAnimations:#"b" context:nil];
[UIView setAnimationDuration:animationSpeed];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
for(UIButton* button in squareArray) {
view.center = destPoint;
}
[UIView commitAnimations];
does (almost - the logic is a bit different in the !shouldMove case, but that's a different story) the same, but in a cleaner way.
I want my text box to blink (like the old LCD clocks).
Right now, I'm calling a myriad of NSTimers and selectors that wait, change the alpha, wait, then change it back. Even with this, it looks really bad, and I'm thinking I have to put an NSTimer to gradually change the alpha, but from what I hear they are not meant for things of that precision.
My thoughts are there must be a way to do this a lot better than how I am currently implementing it. It feels like hack.
Using an animation delegate might make it less "hacky":
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
[myLabel setAlpha:0.0];
[UIView commitAnimations];
And then you can have your didStopSelector restart the animation:
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
[self displayLabel];
}
Depending on the animationID, you could take different actions, etc. Using UIView's setAnimationDelay might come in handy as well.
UIView also has a setDuration call for animations:
[UIView setAnimationDuration:0.1];
If you are building for iOS4, check the documentation since you should be using block-based animation calls rather than these delegate based ones.
you can set the alpha of the lable with annimation just like
to hide lable with animation
[UIView animateWithDuration:0.2 delay:0.1 options:UIViewAnimationOptionCurveEaseOut animations:^{
lblDistance.alpha=0;
} completion:^(BOOL finished) {
if (finished) {
}
}];
to show lable with animation
[UIView animateWithDuration:0.2 delay:0.1 options:UIViewAnimationOptionCurveEaseOut animations:^{
lblDistance.alpha=1;
} completion:^(BOOL finished) {
if (finished) {
}
}];
this is the best way anyone can animate and create a blinking lable........
I would use an NSTimer, but instead of messing with alpha channels i would either not draw the text (if that's even possible with Apple's very attribute-limited SDK), or if that's not possible you could always draw something on top of it (like a rectangle).
Using this approach of drawing something over your text would yield better performance.
Though some (okay most) would consider this a ugly hack, let me just say this, "If it looks right, it is right."
NSTimers and changing the alpha is a perfectly acceptable way of doing it - that's certainly what I do. If you are having problems, perhaps a code sample might help us see where the issue is?