问题
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.
回答1:
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];
回答2:
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];
来源:https://stackoverflow.com/questions/12344617/transformation-unable-to-perform-twice