I`m wondering if is possible to write different UIView animation at the same time.
I trying to animate two Apple style flip counters using sprite animation simultaneously.
Each flip counter has his own animation.
The problem is that the second flip counter start to run when the first counter finish.
EDITED Twice: Here is the code: https://dl.dropbox.com/u/1348024/MultipleFlipCounters.zip
There are two simple classes “FlipCounterView.m” and “CounterViewController”.
Tapping the screen “CounterViewController” will start the animation.
Thanks!
EDITED:
Relevant code added.
Apple style flip counter sprite animation is done FlipCounterView class.
- (void)viewDidLoad{
[super viewDidLoad];
flipCounter1 = [[FlipCounterView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
[flipCounter1 add:10];
[self.view addSubview:flipCounter1];
flipCounter2 = [[FlipCounterView alloc] initWithFrame:CGRectMake(0, 120, 200, 200)];
[flipCounter2 add:10];
[self.view addSubview:flipCounter2];
}
Tapping screen:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[UIView animateWithDuration:0.5
animations:^{
//this method made an sprite animation on flipCounter1
[flipCounter1 distributedAdd:150 overSeconds:0 withNumberOfIterations:3];
}];
[UIView animateWithDuration:0.5
animations:^{
//this method made an sprite animation on flipCounter2
[flipCounter2 distributedAdd:150 overSeconds:0 withNumberOfIterations:3];
}];
}
Your code crashes, but give this a try after fixing the crash:
- (void)animateNow1
{
[UIView animateWithDuration:0.5
animations:^{
[flipCounter1 distributedAdd:150 overSeconds:0 withNumberOfIterations:3];
}];
}
- (void)animateNow2
{
[UIView animateWithDuration:0.5
animations:^{
[flipCounter2 distributedAdd:150 overSeconds:0 withNumberOfIterations:3];
}];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self performSelector:#selector(animateNow1) withObject:nil afterDelay:0.00];
[self performSelector:#selector(animateNow2) withObject:nil afterDelay:0.00];
}
The crash in your code seems to be unrelated to the animation (Xcode 4.5.1), and if you want both animations performing simultaneously you can just do this in one animation block (since the animations attributes are the same).
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[UIView animateWithDuration:0.5 animations:^{
[flipCounter1 distributedAdd:150 overSeconds:0 withNumberOfIterations:3];
[flipCounter2 distributedAdd:150 overSeconds:0 withNumberOfIterations:3];
}];
}
Related
I have a little blue circle that fades in and out to two different locations until the user touches the screen. Then I want the circle to fade out in the position it is at.
- (void)fadePowerUpOut {
[UIView animateWithDuration:.5 delay:2 options:UIViewAnimationOptionCurveLinear animations:^{//the power up will stay in its position until after 2 seconds it will fade out which is because of the delay
self.powerUp.alpha=0;
} completion:^(BOOL finished) {
if (finished) {//I put if finished here because i don't want this method to be ran if the user touches the screen within the 2 second delay
if (self.powerUp.frame.origin.x==self.rectPowerUp.origin.x) {//if its in the first location go to the second
self.powerUp.frame=self.rectPowerUp2;
}else{//if its in the second location go to the first
self.powerUp.frame=self.rectPowerUp;
}
[self fadePowerUpIn];
}
}];
}
- (void)fadePowerUpIn {
[UIView animateWithDuration:.5 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
self.powerUp.alpha=1;
} completion:^(BOOL finished) {
if (finished) {
[self FadePowerUpOut];
}
}];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[UIView animateWithDuration:.5 delay:0 options:UIViewAnimationCurveLinear|UIViewAnimationOptionBeginFromCurrentState animations:^(){ self.powerUp.alpha=0;} completion:^(BOOL completion){
}];
}
What is happening is, when the user touches the screen the circle just does not fade out, but only fades out after the 2 seconds (the delay I put on the fadePowerUpOut method).
As ACB indicated the first thing to do is set the Delay to zero. To be able to start the
delayed fadeout I would suggest using a timer.
i have two views and one viewcontroller: one contains UIImageView with image, second have touchesBegan and touchesEnded method:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[viewcontroller moveMethod];
}
How to make moveMethod method to make the animation (eg. moving the x-axis) repeat smoothly until I stop to push the view?
try this code to move your view on x-axis
CGRect frame=view.frame;
frame.origin.x=frame.origin.x+200;
[UIView beginAnimations:#"" context:nil];
[UIView setAnimationDuration:2];
[UIView setAnimationDelegate:self];
view.frame=frame;
[UIView commitAnimations];
Hello All I want to rotate UIView on single finger touch and it still rotate untill finger moves on screen of iPhone and it stops rotation when I stop the finger moving or remove it from screen.
Thanks in Advance.
Try similar code
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
CGAffineTransform rr=CGAffineTransformMakeRotation(5);
yourView.transform=CGAffineTransformConcat(yourView.transform, rr);
[UIView commitAnimations];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
}
Pradeepa's code is nice, however, that animation system is getting deprecated (if not already). Try something like this instead:
CABasicAnimation *rotation = [CABasicAnimation animationWithKeyPath:#"transform.rotation"];
double startRotationValue = [[[yourView.layer presentationLayer] valueForKeyPath:#"transform.rotation.z"] doubleValue];
rotation.fromValue = [NSNumber numberWithDouble:startRotationValue];
rotation.toValue = [NSNumber numberWithDouble:startRotationValue+5];
rotation.duration = 0.2;
[yourView.layer addAnimation:rotation forKey:#"rotating"];
I took Pradeepa's numbers to make them comparable, but i believe Apple prefers you using this or block-based animation instead of the old system.
rotating a view on touch may help you
I`m using a simple Textview in an tabbased application to display some text :) I figerd out how to change the text animated using the following code:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self];
// To be a swipe, direction of touch must be horizontal and long enough.
if (fabsf(startTouchPosition.x - currentTouchPosition.x) >= HORIZ_SWIPE_DRAG_MIN &&
fabsf(startTouchPosition.y - currentTouchPosition.y) <= VERT_SWIPE_DRAG_MAX)
{
// It appears to be a swipe.
if (startTouchPosition.x < currentTouchPosition.x){
NSString *string = [[NSString alloc]initWithFormat:#"..."];
self.text = string;
}
else{
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
//[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:[self superview] cache:YES];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:[self superview] cache:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.5];
[[self superview] exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
[UIView commitAnimations];
[self displayNewText];
}
}
However there is now "slide transition" defined in UIViewAnimationTransition. I know about paging, but I don`t want to use this. Is there an other possibility to have an horizontal slide animation when detecting a horizontal swipe?
I think you are almost there. Intercept touchesMoved:withEvent method just like in your current code and update the visible area of the UIScrollView object.
I hope you don't have to detect touches for your requirement use UIScrollView as parent view for UITextView.
scroll=[[UIScrollView alloc]initWithFrame:CGRectMake(0,40, 320,200)];
[scroll setContentSize:CGSizeMake(640, 200)];
Or if you want to animate the view you can use affine transforms.
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1];
someview.transform=CGAffineTransformMakeTranslation(-100, 0);
[UIView commitAnimations];
I rotate a view by 45 degree on every dragging. Here is my code. Problem here is that the view is rotated only once. After that there is no rotation. What will be the reason? Any help
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0];
dialView.transform = CGAffineTransformMakeRotation(45* M_PI/180);
[UIView commitAnimations];
}
You can also use CGAffineTransformRotate to rotate from its present transform, as in:
dialView.transform = CGAffineTransformRotate(dialView.transform, 45 * M_PI / 180);
The rotation is always relative to 0. If you rotate to x several times it will rotate only the first time.
If you want to rotate 45 everytime you call that code, you will need to have counter and it would look like this:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
step++;//You are supposed to keep this variable global.
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0];
dialView.transform = CGAffineTransformMakeRotation(45*step* M_PI/180);
[UIView commitAnimations];
}