I have an UIView animation to move an object on the screen and I want the animation to be stopped (and the object to keep the position when the animation is stopped) when the object goes off the screen.
I want my object to always be visible, and to stop moving when it meets an edge of the screen. The target point may be anywhere.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.MyObject.center = targetPoint;
[UIView commitAnimations];
I tried to fire a scheduled timer which check if the object is off screen but it seems that the position my object does not change.
Thanks for your help.
Below is the code sample if you are looking to animate the object from left to right edge of the screen.
CGRect objectFrame = self.MyObject.frame;
objectFrame.origin.x = self.view.frame.size.width-objectFrame.size.width;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[self.MyObject setFrame:objectFrame];
[UIView commitAnimations];
Hope it helps.
Cheers.
Related
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)
I had an IPhone application in which i want to transform my image view to its double size without changing the position of the image view.(i.e. the centre need not be changed)I am doing like this
CGAffineTransform firstTransform = bg.transform;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 0.5];
bg.transform = CGAffineTransformScale(bg.transform,2,2);
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration: 0.5];
bg.transform = firstTransform;
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
But when transforming its position gets changed.so
Now i am doing like this to do not change the position of the image view.i want the image view to be transformed to the double size on click with an animation like this
animation on the red button when clicking next
can anybody guide me on this?
Make sure the anchor point is set to the centre, bearing in mind when setting the anchor point the value has to be between 0 and 1: 0 being the left/top side and 1 being the right/bottom side. So to set the anchor point to the middle, set it to 0.5 on both the X and Y axis:
[itemName.layer setAnchorPoint:CGPointMake(0.5, 0.5)];
Then when you scale the object, it should scale up from around the centre point.
I also suggest using animateWithDuration:completion. The code will become much simpler:
[UIView animateWithDuration:1.0f animations:^{
[bg setTransform:CGAffineTransformScale(bg.transform, 2.0, 2.0)];
} completion ^{
[UIView animateWithDuration:1.0f animations:^{
[bg setTransform:CGAffineTransformScale(bg.transform, 0.5, 0.5)];
];
}];
Hope this helps you achieve the desired effect!
Make sure that you are not using auto layout. To disable that open your xib, click on first tab in the right panel, uncheck the Use Auto Layout under Interface Builder Document. Screen shot is attached for help.
What is the simplest way to create a custom UIView animation transition? Most of the tutorials that pop up in a Google search are not recent. (I'm using XCode 4.5 version) In my current iPhone app I have the following code for my screen transitions:
[UIView beginAnimations:#"View flip" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:1.25];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
[UIView commitAnimations];
I want something unique instead of the 5 or 6 built-in transitions. Any ideas or suggestions on where to start?
I think this is what you are looking for:
[UIView animateWithDuration:0.3 animations:^{
//Move frame or transform view
}];
Inside that block you can put arbitrary animation code.
I have a custom UIView. It initializes two rectangular UIBezierPaths, and in drawRect it simply fills these two paths.
This view is placed on a button. When the button is clicked, a rotation is applied to it inside an animateWithDuration block:
icon.transform = CGAffineTransformMakeRotation(M_PI*-0.25);
This works great.
When the button is clicked again, I am trying to make the view rotate back to it's original orientation (reverse the rotation). I have tried:
icon.transform = CGAffineTransformInvert(icon.transform);
icon.transform = CGAffineTransformMakeRotation(M_PI*0.25);
icon.transform = CGAffineTransformIdentity;
... and a number of other variations, but none of them seem to work. Instead, the view is distorted until it's no longer visible -- sorta stretched to nothing. Sometimes, reapplying the first transform brings it back but with other reversals it doesn't.
What am I doing wrong?
In ios the transform is always relative, so once transformation is done, the next transformation should be relative to the previous transformation. You can simply change the frame of the view and revert it back to normal on second button click. On even clicks you will have to revert back the frame. Implement the following code. You can use the block level code if you feel comfortable with it as it is the new norm.
int noOfClicks = 0;
-(IBAction)yourBtnAction:(id)sender
{
noOfClicks++;
if(noOfClicks %2 != 0)
{
[UIView beginAnimations:#"Rotate" context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
CGRect frame=yourView.frame;
frame.origin.y+=20;
yourview.frame=frame;
[UIView commitAnimations];
}
else
{
[UIView beginAnimations:#"Rotate" context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationDelegate:self];
CGRect frame=yourView.frame;
frame.origin.y-=20;
yourview.frame=frame;
[UIView commitAnimations];
}
}
If you don't want to make so many change you can use following to revert back but employing same logic:
[UIView beginAnimations:#"Rotate back" context:nil];
[UIView setAnimationDuration:1.0];
yourView.transform = CGAffineTransformIdentity;
[UIView commitAnimations];
I have this code:
when image change alpha from 0.00 to 1.00 it is imediately and not in 3 seconds, why?
- (void) startAnimation{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:3];
[successView setAlpha:1.00];
[UIView commitAnimations];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:3];
[successView setAlpha:0.00];
[UIView commitAnimations];}
Both animations are run in the same run cycle (that's how UIView animations work—all animations in one run cycle are "concatenated." You'll need to use
[UIView setAnimationDelay:3.0];
On the second animation.
Example Code
There are two ways to do this—using the standard begin/commit animations, or using the blocks method. This example uses the begin/commit animations code, which is what you have. The issue is because the animations are being concatenated, and without going into CA, the standard animation behavior is for one to be "forced" to end before the other runs. It has to do with run loops; Building Animation-Driven Interfaces from WWDC 2010 goes into depth about this. But the code to do what you want looks like this:
- (void)fadeOut {
[UIView beginAnimations:#"Animation2" context:NULL];
[UIView setAnimationDuration:3];
[self.testView setAlpha:0.00];
[UIView commitAnimations];
}
- (IBAction)animate {
[UIView beginAnimations:#"Animation1" context:NULL];
[UIView setAnimationDuration:3];
[self.testView setAlpha:1.00];
[UIView commitAnimations];
[self performSelector:#selector(fadeOut) withObject:nil afterDelay:3.0];
}
You have to force it to break up the run loop, basically. The key thing to remember is that animations are not executed sequentially, as you might expect code to be.
The blocks-based code looks like this. Note that I'm using the autorepeat option, which automatically repeats the animation. Note though that in the animation, you are setting a property, so by default after the animation completes the view will go back to being visible. Therefore, you have the set the property again to zero in the completion block.
- (IBAction)animate {
[UIView transitionWithView:self.testView duration:3.0 options:UIViewAnimationOptionAutoreverse animations:^{self.testView.alpha = 1.0;} completion:^(BOOL finished) {self.testView.alpha = 0.0;}];
}
Hopefully this helps!