Transformation unable to perform twice - objective-c

I have this function,
-(void)transitionstar{
star.hidden = NO;
star2.hidden = NO;
star3.hidden = NO;
star4.hidden = NO;
star5.hidden = NO;
star6.hidden = NO;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4f];
[UIView animateWithDuration:0.0 animations:^{
CGAffineTransform scale = CGAffineTransformMakeScale(1, 1);
CGAffineTransform rotate = CGAffineTransformMakeRotation(360.0);
CGAffineTransform rotate2 = CGAffineTransformMakeRotation(-360.0);
CGAffineTransform rotate3 = CGAffineTransformMakeRotation(-720.0);
CGAffineTransform rotate4 = CGAffineTransformMakeRotation(720.0);
CGAffineTransform rotate5 = CGAffineTransformMakeRotation(1080.0);
CGAffineTransform rotate6 = CGAffineTransformMakeRotation(-1080.0);
CGAffineTransform translate = CGAffineTransformMakeTranslation(-800, -800);
CGAffineTransform translate2 = CGAffineTransformMakeTranslation(600, -600);
CGAffineTransform translate3 = CGAffineTransformMakeTranslation(400, 400);
CGAffineTransform translate4 = CGAffineTransformMakeTranslation(-200, 200);
CGAffineTransform translate5 = CGAffineTransformMakeTranslation(900, -300);
CGAffineTransform translate6 = CGAffineTransformMakeTranslation(-200, 500);
CGAffineTransform transform = CGAffineTransformConcat(translate, scale);
transform = CGAffineTransformConcat(transform, rotate);
CGAffineTransform transform2 = CGAffineTransformConcat(translate2, scale);
transform2 = CGAffineTransformConcat(transform2, rotate2);
CGAffineTransform transform3 = CGAffineTransformConcat(translate3, scale);
transform3 = CGAffineTransformConcat(transform3, rotate3);
CGAffineTransform transform4 = CGAffineTransformConcat(translate4, scale);
transform4 = CGAffineTransformConcat(transform4, rotate4);
CGAffineTransform transform5 = CGAffineTransformConcat(translate5, scale);
transform5 = CGAffineTransformConcat(transform5, rotate5);
CGAffineTransform transform6 = CGAffineTransformConcat(translate6, scale);
transform6 = CGAffineTransformConcat(transform6, rotate6);
star.transform = transform;
star2.transform = transform2;
star3.transform = transform3;
star4.transform = transform4;
star5.transform = transform5;
star6.transform = transform6;
}
completion:^(BOOL finished){
if (finished) {
star.hidden = YES;
star2.hidden = YES;
star3.hidden = YES;
star4.hidden = YES;
star5.hidden = YES;
star6.hidden = YES;
}
}];
[UIView commitAnimations];
}
When i call it first time, it did worked.
However, i call again within same view, it cannot perform and stuck there.
-hidden work
-transition not work
-rotation not work
-nslog work
Why second time will become like this?
Update
if(!positionrepeat)
{
//Display Correct IMAGE;
[isrepeat addObject:[NSNumber numberWithInt:positionvalue]];
//soundeffect = [self createSoundID: #"correct"];
//AudioServicesPlaySystemSound(soundeffect);
[self displayresulttext:#"correct"];
[self.view setNeedsDisplay];
[self transitionstar];
correct++;
completed.text = [NSString stringWithFormat:#"%d", correct];
[self result];
}
This is how I call the function. However, it still same.

in this bit of code your changed the transform
star.transform = transform;
star2.transform = transform2;
star3.transform = transform3;
star4.transform = transform4;
star5.transform = transform5;
star6.transform = transform6;
for eg consider your having the star1 initial transform is x ,
now your doing some calculation for transform....
CGAffineTransform rotate = CGAffineTransformMakeRotation(360.0);
CGAffineTransform translate = CGAffineTransformMakeTranslation(-800, -800);
CGAffineTransform transform = CGAffineTransformConcat(translate, scale);
transform = CGAffineTransformConcat(transform, rotate);
then
your changed the transform of star x to y...
star.transform = transform;//say this is y
now your star transform will become y okay...
again doing this code by call again so again your initial star transform is y and your equating transform to y so no need and u didn't see any of the result...
y=y so no result....
So need to change some logic like this or some what you want but this is an idea...
1st change: you need to store the transform of each star so you need some set of variables of type transform...
CGAffineTransform star1Initial,star2Initial,star3Initial,star4Initial,star5Initial,star6Initial;
2nd change:you need to save the initial transform of stars in the view did load....
star1Initial=star1.transform;
star2Initial=star2.transform;
star3Initial=star3.transform;
star4Initial=star4.transform;
star5Initial=star5.transform;
star6Initial=star6.transform;
-(void)transitionstar{
star1.hidden = NO;
star2.hidden = NO;
star3.hidden = NO;
star4.hidden = NO;
star5.hidden = NO;
star6.hidden = NO;
star1.transform=star1Initial;
star2.transform=star2Initial;
star3.transform=star3Initial;
star4.transform=star4Initial;
star5.transform=star5Initial;
star6.transform=star6Initial;
// [UIView beginAnimations:nil context:NULL];
// [UIView setAnimationDuration:0.4f];
[UIView animateWithDuration:1.0 animations:^{
CGAffineTransform scale = CGAffineTransformMakeScale(1, 1);
CGAffineTransform rotate = CGAffineTransformMakeRotation(360.0);
CGAffineTransform rotate2 = CGAffineTransformMakeRotation(-360.0);
CGAffineTransform rotate3 = CGAffineTransformMakeRotation(-720.0);
CGAffineTransform rotate4 = CGAffineTransformMakeRotation(720.0);
CGAffineTransform rotate5 = CGAffineTransformMakeRotation(1080.0);
CGAffineTransform rotate6 = CGAffineTransformMakeRotation(-1080.0);
CGAffineTransform translate = CGAffineTransformMakeTranslation(-800, -800);
CGAffineTransform translate2 = CGAffineTransformMakeTranslation(600, -600);
CGAffineTransform translate3 = CGAffineTransformMakeTranslation(400, 400);
CGAffineTransform translate4 = CGAffineTransformMakeTranslation(-200, 200);
CGAffineTransform translate5 = CGAffineTransformMakeTranslation(900, -300);
CGAffineTransform translate6 = CGAffineTransformMakeTranslation(-200, 500);
CGAffineTransform transform = CGAffineTransformConcat(translate, scale);
transform = CGAffineTransformConcat(transform, rotate);
CGAffineTransform transform2 = CGAffineTransformConcat(translate2, scale);
transform2 = CGAffineTransformConcat(transform2, rotate2);
CGAffineTransform transform3 = CGAffineTransformConcat(translate3, scale);
transform3 = CGAffineTransformConcat(transform3, rotate3);
CGAffineTransform transform4 = CGAffineTransformConcat(translate4, scale);
transform4 = CGAffineTransformConcat(transform4, rotate4);
CGAffineTransform transform5 = CGAffineTransformConcat(translate5, scale);
transform5 = CGAffineTransformConcat(transform5, rotate5);
CGAffineTransform transform6 = CGAffineTransformConcat(translate6, scale);
transform6 = CGAffineTransformConcat(transform6, rotate6);
star1.transform = transform;
star2.transform = transform2;
star3.transform = transform3;
star4.transform = transform4;
star5.transform = transform5;
star6.transform = transform6;
}
completion:^(BOOL finished){
if (finished) {
star1.hidden = YES;
star2.hidden = YES;
star3.hidden = YES;
star4.hidden = YES;
star5.hidden = YES;
star6.hidden = YES;
}
}];
// [UIView commitAnimations];
}
i hope this will help you....
Hi try this code for spin animation.....
- (void) runSpinAnimationWithDuration:(CGFloat) duration;
{
CABasicAnimation* rotationAnimation;
int rotations=1;
rotationAnimation = [CABasicAnimation animationWithKeyPath:#"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * rotations * duration ];
rotationAnimation.duration = duration;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 1.0;
rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];

I am assuming that the method you wrote above is working within a class that is derived from UIView.
If this assumption is correct, then your method may be called when the view has to be redrawn.
To redraw your view, you have to call the setNeedsDisplay method, as such:
[myCustomView setNeedsDisplay];

Related

Some problems with View animation

I'm trying create animation on my View. Now I have any trouble. Animation is work only with _pageViewController
_pageViewController.view.frame = frame;
_pageViewController.view.alpha = alpha;
but _bottomImage.frame = frame1; doesn't work. I don't understand Why?
If I comment this lines
//_pageViewController.view.frame = frame;
//_pageViewController.view.alpha = alpha;
_bottomImage.frame = frame1 begin work.
What am I doing wrong?
- (IBAction)share:(id)sender {
CGRect frame =self.pageViewController.view.frame;
CGRect frame1 = _bottomImage.frame;
float alpha = 1.0;
if (frame.origin.y==65.0) {
frame.origin.y = 240;
frame1.origin.y = 650;
alpha = 0.5;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:5.5];
[UIView setAnimationDelay:0.1];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
_pageViewController.view.frame = frame;
_pageViewController.view.alpha = alpha;
_bottomImage.frame = frame1;
[UIView commitAnimations];
}else{
[self closeShare];
}
}

Animation in for loop compounds everytime view appears

I am trying to get a fresh set of random animations each time the view loads, but instead I am getting the animations multiplying and leading to the inevitable application meltdown! Any help on how to get a fresh start each time the view appears
- (void)startAnimation
{
int n;
for (n=0; n<150; n++){
UIBezierPath *bubblePath = [UIBezierPath bezierPath];
[bubblePath moveToPoint:P(arc4random()%768, arc4random()%1024)];
[bubblePath addCurveToPoint:P(arc4random()%768, arc4random()%1024)
controlPoint1:P(arc4random()%768, arc4random()%1024)
controlPoint2:P(arc4random()%768, arc4random()%1024)];
CALayer *bubble = [CALayer layer];
bubble.bounds = CGRectMake(0, 0, 10.0, 10.0);
bubble.position = CGPointMake(arc4random()%768, arc4random()%1024);
bubble.contents = (id)([UIImage imageNamed:#"Bubble.png"].CGImage);
[self.view.layer addSublayer:bubble];
CAKeyframeAnimation *bubbleAnim = [CAKeyframeAnimation animationWithKeyPath:#"position"];
bubbleAnim.path = bubblePath.CGPath;
bubbleAnim.rotationMode = kCAAnimationRotateAuto;
bubbleAnim.duration = 100;
bubbleAnim.repeatCount = HUGE_VALF;
[bubble addAnimation:bubbleAnim forKey:#"bubble"];
}
int i;
for (i=0; i<8; i++){
UIBezierPath *tinyPath = [UIBezierPath bezierPath];
[tinyPath moveToPoint:P(arc4random()%400+840, arc4random()%75+125)];
[tinyPath addCurveToPoint:P(-400, arc4random()%75+150)
controlPoint1:P(arc4random()%450, arc4random()%200+250)
controlPoint2:P(arc4random()%300, arc4random()%200+150)];
CALayer *tinyFish = [CALayer layer];
tinyFish.bounds = CGRectMake(0, 0, 150.0, 150.0);
tinyFish.position = CGPointMake(868, 800);
tinyFish.contents = (id)([UIImage imageNamed:#"Fish3.png"].CGImage);
[self.view.layer addSublayer:tinyFish];
CAKeyframeAnimation *tinyAnim = [CAKeyframeAnimation animationWithKeyPath:#"position"];
tinyAnim.path = tinyPath.CGPath;
tinyAnim.rotationMode = kCAAnimationRotateAuto;
tinyAnim.duration = 15;
tinyAnim.repeatCount = HUGE_VALF;
[tinyFish addAnimation:tinyAnim forKey:#"fish"];
}
}
[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationCurveLinear animations:^
{
//do your animations here
}completion:^(BOOL finished)
{
//remove all layers here. Eventually add here another animation where you fade out all layers and in that animation's completion block remove them from the view
}];
Hope this helps. Cheers!

Objective C UIImageView rotation with animation not working good

I am creating compass in a table cell so I have problem with the rotation of the image view it start all over again from the position which is the image in the resources.
this is part of my code:
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationDelegate:self];
if(cellValue.direction == 0){
cell.compass.transform = CGAffineTransformRotate(cell.compass.transform,DegreesToRadians([cellValue.fixedDirection floatValue]));
} else {
cell.compass.transform = CGAffineTransformRotate(cell.compass.transform,DegreesToRadians([cellValue.direction floatValue]));
}
[UIView commitAnimations];
cell.compass is UIimageView and I have image in it that is arrow vertical. if the direction is 0 then it should get to that degree and after it get angle from compass it goes to the else clause. So the image starst to rotate from that vertical position not from the last rotated position.
how to get the last rotated position and continue from there
Thanks!
Try to rotate view's layer with removedOnCompletion = NO instead of rotate UIView
CALayer *layer = [cell.compass layer];
NSNumber *initRotation = [layer valueForKeyPath:#"transform.rotation"];
//CATransform3D rotationTransform = CATransform3DRotate(layer.transform, angle, 0.0, 0.0, 1.0);
//layer.transform = rotationTransform;
CABasicAnimation *animation =[CABasicAnimation animationWithKeyPath:#"transform.rotation"];
animation.duration = 0.5;
animation.fromValue = initRotation ;
animation.toValue = [NSNumber numberWithFloat:([initRotation floatValue] + angle)];
animation.removedOnCompletion = NO;
[layer addAnimation:animation forKey:#"transform.rotation"];
I found a solution to the problem like this:
NSLog(#"old dir %d: new dir: %d", [cellValue.oldDirection intValue], [cellValue.direction intValue]);
CABasicAnimation *animation =[CABasicAnimation animationWithKeyPath:#"transform.rotation"];
animation.duration = 0.5;
int oldDir = [cellValue.oldDirection intValue];
int newDir = [cellValue.direction intValue];
/// in didUpdateHeading
/// int result = 0;
/// result = fixedDir - orientation;
/// place.oldDirection = place.direction;
/// place.direction = [NSNumber numberWithInt:result];
fixed dir is the direction in degree from north to the place! and orientation is the current orientation in degree from the compass or trueHeading
if (oldDir*newDir < 0 && abs(abs(oldDir) - abs(newDir)) < 180) {
oldDir = (360 + oldDir) % 360;
newDir = (360 + newDir) % 360;
}
animation.fromValue = [NSNumber numberWithFloat:DegreesToRadians(oldDir)];
animation.toValue = [NSNumber numberWithFloat:DegreesToRadians(newDir)];
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeForwards;
[cell.compass.layer addAnimation:animation forKey:#"transform.rotation"];
float differenceAngle = [animation.toValue floatValue] - [animation.fromValue floatValue];
CATransform3D rotationTransform = CATransform3DMakeAffineTransform(CGAffineTransformRotate(cell.compass.transform,differenceAngle));
cell.compass.layer.transform = rotationTransform;
I hope I helped :)

CGAffineTransformMakeRotation before a CABasicAnimation

I'm performing a rotation of an UIImageView in place first before performing a rotation animation:
// rotate to the left by 90 degrees
someView.transform = CGAffineTransformMakeRotation((-0.5)*M_PI);
Then calling a rotation on the view by 180 degrees... but it seems like it is rotating the image starting from the original position as if it was not initially rotated.
- (void)rotateIndicatorToAngle: (UIView *)view angle: (NSNumber *)angleInDegrees
{
CALayer *layer = view.layer;
CGFloat duration = 5.0;
CGFloat radians = [self ConvertDegreesToRadians: [angleInDegrees floatValue]];
CABasicAnimation* rotationAnimation;
rotationAnimation = [CABasicAnimation animationWithKeyPath:#"transform.rotation.z"];
rotationAnimation.toValue = [NSNumber numberWithFloat: radians];
rotationAnimation.duration = duration;
rotationAnimation.cumulative = YES;
rotationAnimation.repeatCount = 1.0;
rotationAnimation.removedOnCompletion = NO;
rotationAnimation.fillMode = kCAFillModeForwards;
rotationAnimation.timingFunction = [CAMediaTimingFunction
functionWithName:kCAMediaTimingFunctionEaseOut];
[layer addAnimation: rotationAnimation forKey: #"rotationAnimation"];
}
What gives?
rotationAnimation.cumulative = YES;
Have you tried using rotationAnimation.additive = YES; instead of cumulative?
You could use the class methods from UIView to easily animate your view:
[UIView beginAnimation: #"Rotate" context: nil];
[UIView setAnimationDuration: 5.0f];
[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];
CGFloat radians = [self ConvertDegreesToRadians: [angleInDegrees floatValue]];
view.transform = CGAffineTransformMakeRotation(radians);
[UIView commitAnimation];
That's what I'm using most of the time, no need to use layers.
UPDATE: I mean, I find it easier not to use layers in that case, no pejorative value on using layers.

Using Block Completion Handler in iOS 4 for animation

I would like to animate my subviews movement when rotating the device, changing the alpha to 0, move the view to the new position and reset the alpha to 1.
Using this code in didRotateFromInterfaceOrientation causes the view to flash and fade very quickly and then reappear. I would like to avoid this behaviour.
[UIView animateWithDuration:kAnimationDuration animations:^{
anObject.alpha = 0.0;
CGRect newFrame = anObject.frame;
newFrame.origin.x = newX;
newFrame.origin.y = newY;
newFrame.size.width = newWidth;
newFrame.size.height = newHeight;
anObject.frame = newFrame;
} completion:^ (BOOL finished){
if (finished) {
anObject.alpha = 1.0;
}
}];
Is there a way around this flashing?
Thanks
Maybe actually animate alpha on completition ? rather than flash it ? :)
[UIView animateWithDuration:kAnimationDuration animations:^{
anObject.alpha = 0.0;
CGRect newFrame = anObject.frame;
newFrame.origin.x = newX;
newFrame.origin.y = newY;
newFrame.size.width = newWidth;
newFrame.size.height = newHeight;
anObject.frame = newFrame;
} completion:^ (BOOL finished){
if (finished) {
[UIView animateWithDuration:kAnimationDuration
animations:^{
anObject.alpha = 1;}
}
}];
Cheers,
Krzysztof Zabłocki