2010-07-13 2 views
0

iPhone 프로그래밍에 새로운 기능 .. 내 앱에서 타이머가 제대로 작동하지 않습니다. 누구든지 나를 도와주세요. 다음은 터치하거나 스 와이프하여 몇 장의 이미지를 애니메이트하는 코드입니다.NSTimer가 잘 작동하지 않습니다 .. iPhone의 애니메이션

나는 타이머를 사용하여 몇 초 동안 터치를 제한하고 싶습니다 ... 이 작업을 수행하는 또 다른 방법이 ..? (장치의 충돌 타이머 사용하지만, 충돌에 대한 해결책을 시도 simulator..im에서 잘 작동하기 전에 ..) 당신이하지 않는 것

// HomeViewController.m 


#import "HomeViewController.h" 

@implementation HomeViewController 

@synthesize dustImage, myAnimatedView; 

@synthesize _isAnimating = isAnimating; 

float ver_X, ver_Y; 
BOOL rotated; 
NSTimer *touchTimer; 


- (void)viewDidLoad { 

    [super viewDidLoad]; 
    self.view.frame = [[UIScreen mainScreen] bounds]; 
    //[self playAudio]; 

} 

-(void)blowingTheSparkles:(NSSet *) touches { 

    if(!isAnimating){ 
     float a, aa; 
     float b, bb; 

     CGRect frame1; 
     myAnimatedView = [UIImageView alloc]; 

     //NSLog(@"touch count...%d",[touches count]); 
     for (int i=0; i< (int)[touches count]; ++i) { 
      UITouch* touch = [[touches allObjects] objectAtIndex:i]; 
      CGPoint location = [touch locationInView:dustImage]; 

      NSArray *myImages = [NSArray arrayWithObjects: [UIImage imageNamed:@"starburst1.png"], 
           [UIImage imageNamed:@"starburst2.png"], 
           [UIImage imageNamed:@"starburst3.png"], 
           [UIImage imageNamed:@"starburst4.gif"], 
           [UIImage imageNamed:@"starburst5.png"], 
           [UIImage imageNamed:@"starburst6.png"], 
           [UIImage imageNamed:@"starburst7.png"],nil]; 
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200 
      a=(location.x)-80; 
      b=(location.y)-80; 
      ver_X=768; 
      ver_Y=1024;  

      frame1 = CGRectMake(a,b,160,160); 

#else 

      a=(location.x)-40; 
      b=(location.y)-40; 
      ver_X=320; 
      ver_Y=480; 

      frame1 = CGRectMake(a,b,80,80); 

#endif 
      aa= location.x; 
      bb= location.y; 
      [myAnimatedView initWithFrame:frame1]; 
      myAnimatedView.animationImages = myImages; 
      myAnimatedView.animationDuration = 0.25; 
      myAnimatedView.animationRepeatCount = 0; 

      [dustImage addSubview:myAnimatedView]; 


      CABasicAnimation *fadeOutAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"]; 
      [fadeOutAnimation setToValue:[NSNumber numberWithFloat:0.3]]; 
      fadeOutAnimation.fillMode = kCAFillModeForwards; 
      fadeOutAnimation.removedOnCompletion = NO; 

      //Set up scaling 
      CABasicAnimation *resizeAnimation = [CABasicAnimation animationWithKeyPath:@"bounds.size"]; 
      [resizeAnimation setToValue:[NSValue valueWithCGSize:CGSizeMake(0.0f, frame1.size.height * (1.0f /1.0))]]; 
      resizeAnimation.fillMode = kCAFillModeForwards; 
      resizeAnimation.removedOnCompletion = NO; 

      // Set up path movement 
      CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"]; 
      pathAnimation.calculationMode = kCAAnimationPaced; 
      pathAnimation.fillMode = kCAFillModeForwards; 
      pathAnimation.removedOnCompletion = NO; 

      CGPoint endPoint = CGPointMake(aa ,ver_Y); 
      CGMutablePathRef curvedPath = CGPathCreateMutable(); 
      CGPathMoveToPoint(curvedPath, NULL, aa, bb); 
      CGPathAddCurveToPoint(curvedPath, NULL, endPoint.x, bb,endPoint.x, bb,endPoint.x,endPoint.y); 
      pathAnimation.path = curvedPath; 
      CGPathRelease(curvedPath); 

      CAAnimationGroup *group = [CAAnimationGroup animation]; 
      group.fillMode = kCAFillModeForwards; 
      group.removedOnCompletion = NO; 
      [group setAnimations:[NSArray arrayWithObjects:fadeOutAnimation, pathAnimation, resizeAnimation, nil]]; 
      group.duration = 4.0f; 
      group.delegate = self; 
      [group setValue:myAnimatedView forKey:@"myAnimatedView"]; 

      [myAnimatedView.layer addAnimation:group forKey:@"savingAnimation"]; 
      [myAnimatedView startAnimating]; 


     } 
    } 
} 


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    if(!isAnimating) 
     [self blowingTheSparkles:touches]; 

} 

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{ 
} 


-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

    isAnimating=TRUE; 
    touchTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(sleeping) userInfo:nil repeats:NO]; 

} 

-(void)sleeping{ 

    NSLog(@"....Hiiii"); 
    isAnimating = FALSE; 
    [touchTimer invalidate]; 
    touchTimer = nil; 

} 

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ 

    if(!isAnimating){ 
     [self blowingTheSparkles:touches]; 

     } 

} 



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 

    return NO; 
} 


- (void)didReceiveMemoryWarning { 

    [super didReceiveMemoryWarning]; 

} 

- (void)viewDidUnload { 

    [super viewDidUnload]; 

} 


- (void)dealloc { 

    [myAnimatedView release]; 

    [super dealloc]; 

} 

@end 
+0

모든 로그 : 타이머를 해제 자고

변경? – willcodejavaforfood

답변

2

문제가 될 수있는 NSTimer를 올바르게 보관하십시오. 에

변경을 :

touchTimer = [[NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(sleeping) userInfo:nil repeats:NO] retain]; 

이상적으로 당신은 속성으로 touchTimer를 선언하고 합성하지만,이 먼저 시도해야합니다. 충돌에서

-(void)sleeping{ 

    NSLog(@"....Hiiii"); 
    isAnimating = FALSE; 
    [touchTimer invalidate]; 
    [touchTimer release]; 
    touchTimer = nil; 
}