2011-04-30 7 views
2

에서 NSThread와 다시 트리거 CABasicAnimation 나는이 코어 애니메이션 예를 해킹하고 있습니다 : https://github.com/joericioppo/Shape_05아이폰 OS

은 내가 NSThread에 대한 응답으로 애니메이션을 다시 트리거하고 싶다. NSLogs는 예상대로 나타나지만, 애니메이션 자체는 모든 수면이 완료 될 때까지 트리거되지 않습니다. 비록 메소드가 검증 가능하게 호출되었지만. 매 초마다이 애니메이션을 다시 트리거 할 수 있습니까?

그건 그렇고 - 이것은 개념을 이해하기 위해 노력하고있는 예제 패치 일뿐입니다. 저는 애니메이션이 반복되도록 설정할 수 있다는 것을 알고 있습니다. 그러나 이것은 여러 가지 이유로 제가하고 싶은 것이 아닙니다. 여기에 코드가 있습니다. 어떤 도움이라도 대환영입니다. 감사합니다. 다른 Google 트롤 후

#import <UIKit/UIKit.h> 
#import <QuartzCore/QuartzCore.h> 

@interface Shape_05ViewController : UIViewController { 

    CALayer      *rootLayer; 

    CAShapeLayer   *shapeLayer; 

    CGMutablePathRef  pentagonPath; 
    CGMutablePathRef  starPath; 

    int animationCount; 
    CABasicAnimation *animation; 

} 

-(void)startAnimation; 
-(void) viewDidLoad; 
-(void) startSequence; 
-(void) enactSequence; 

@end 
#import "Shape_05ViewController.h" 

@implementation Shape_05ViewController 


- (void)loadView 
{ 

    UIView *appView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    appView.backgroundColor = [UIColor blackColor]; 
    self.view = appView; 
    [appView release]; 
    rootLayer = [CALayer layer]; 
    rootLayer.frame = self.view.bounds; 
    [self.view.layer addSublayer:rootLayer]; 


    //Pentagon Path 

    pentagonPath = CGPathCreateMutable(); 
    CGPoint center = self.view.center; 
    CGPathMoveToPoint(pentagonPath, nil, center.x, center.y - 75.0); 

    for(int i = 1; i < 5; ++i) { 
     CGFloat x = - 75.0 * sinf(i * 2.0 * M_PI/5.0); 
     CGFloat y = - 75.0 * cosf(i * 2.0 * M_PI/5.0); 
     CGPathAddLineToPoint(pentagonPath, nil,center.x + x, center.y + y); 
    } 
    CGPathCloseSubpath(pentagonPath); //This seems to be fixed in 4.0 

    //Star Path 

    starPath = CGPathCreateMutable(); 
    center = self.view.center; 
    CGPathMoveToPoint(starPath, NULL, center.x, center.y + 75.0); 
    for(int i = 1; i < 5; ++i) { 
     CGFloat x = 75.0 * sinf(i * 4.0 * M_PI/5.0); 
     CGFloat y = 75.0 * cosf(i * 4.0 * M_PI/5.0); 
     CGPathAddLineToPoint(starPath, NULL, center.x + x, center.y + y); 
    }; 
    CGPathCloseSubpath(starPath); 


    //Create Shape 

    shapeLayer = [CAShapeLayer layer]; 
    shapeLayer.path = pentagonPath; 
    UIColor *fillColor = [UIColor colorWithWhite:0.9 alpha:1.0]; 
    shapeLayer.fillColor = fillColor.CGColor; 
    shapeLayer.fillRule = kCAFillRuleNonZero; 
    [rootLayer addSublayer:shapeLayer]; 

} 


-(void) viewDidLoad { 

    NSLog(@"viewDidLoad"); 
    animationCount = 0; 
    [NSThread detachNewThreadSelector:@selector(startSequence) toTarget:self withObject:nil]; 

} 

-(void) startSequence { 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    [self enactSequence]; 
    //[NSThread sleepForTimeInterval:100]; 

    [pool release]; 

} 
-(void) enactSequence { 
    NSLog(@"enactSequence"); 
    for(int i = 0; i < 4; i++) { 
     [NSThread sleepForTimeInterval:1]; 
     [self performSelector:@selector(startAnimation) withObject:nil]; 
     [rootLayer setNeedsDisplay]; 
     NSLog(@"%i", i); 
    }; 

} 


-(void)startAnimation 
{ 
    NSLog(@"startAnimation"); 

    animation = [CABasicAnimation animationWithKeyPath:@"path"]; 
    animation.duration = 0.25; 
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
    animation.repeatCount = 0; 
    animation.autoreverses = NO; 
    animation.fillMode = kCAFillModeForwards; 
    animation.removedOnCompletion = YES; 

    if((animationCount % 2) == 0) { 
     animation.fromValue = (id)pentagonPath; 
     animation.toValue = (id)starPath; 
    } else { 
     animation.fromValue = (id)starPath; 
     animation.toValue = (id)pentagonPath; 
    }; 


    [shapeLayer addAnimation:animation forKey:@"animatePath"]; 

    animationCount++; 

} 


- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 


- (void)dealloc 
{ 
    CGPathRelease(starPath); 
    CGPathRelease(pentagonPath); 
    [super dealloc]; 
} 


@end 

답변

0

OK


: 나는 NSTimer라는이 일에 대해 알게 http://forums.macrumors.com/showthread.php?t=749518&highlight=cabasicanimation

! 이 사이트의 또 다른 검색 좋은 정보 공개 : How do I use NSTimer?

을 그리고 자신의 스레드에서 NSTimer 실행에 대한 : Keep an NSThread containing an NSTimer around indefinitely? (iPhone)

을 이제 내가 사람 하나에게 유용 할 수있는 희망이 업데이트 된 코드 예제를 제공 할 수 있습니다 일. 그런데, 나는 아직 그것을 시도하지 못했지만, 분명히 다음은 타이머를 중지 할 수 있습니다 :이 사이트에 여기에 모든

[myTimer invalidate]; 
myTimer = nil; 

감사합니다, 웹의뿐만 아니라 가장 아름답게 디자인 코너하지만, 총 생명의 은인!

#import <UIKit/UIKit.h> 
    #import <QuartzCore/QuartzCore.h> 

    @interface Shape_05ViewController : UIViewController { 

     CALayer      *rootLayer; 

     CAShapeLayer   *shapeLayer; 

     CGMutablePathRef  pentagonPath; 
     CGMutablePathRef  starPath; 

     int animationCount; 
     CABasicAnimation *animation; 
     NSTimer *repeatingTimer; 

    } 

@property (assign, readwrite) NSTimer *repeatingTimer; 

    -(void) startAnimation: (NSTimer*)theTimer; 
    -(void) viewDidLoad; 
    -(void) startSequence; 

    @end 

#import "Shape_05ViewController.h" 

    @implementation Shape_05ViewController 

    @synthesize repeatingTimer; 


    - (void)loadView 
    { 

     UIView *appView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
     appView.backgroundColor = [UIColor blackColor]; 
     self.view = appView; 
     [appView release]; 
     rootLayer = [CALayer layer]; 
     rootLayer.frame = self.view.bounds; 
     [self.view.layer addSublayer:rootLayer]; 


     //Pentagon Path 

     pentagonPath = CGPathCreateMutable(); 
     CGPoint center = self.view.center; 
     CGPathMoveToPoint(pentagonPath, nil, center.x, center.y - 75.0); 

     for(int i = 1; i < 5; ++i) { 
      CGFloat x = - 75.0 * sinf(i * 2.0 * M_PI/5.0); 
      CGFloat y = - 75.0 * cosf(i * 2.0 * M_PI/5.0); 
      CGPathAddLineToPoint(pentagonPath, nil,center.x + x, center.y + y); 
     } 
     CGPathCloseSubpath(pentagonPath); //This seems to be fixed in 4.0 

     //Star Path 

     starPath = CGPathCreateMutable(); 
     center = self.view.center; 
     CGPathMoveToPoint(starPath, NULL, center.x, center.y + 75.0); 
     for(int i = 1; i < 5; ++i) { 
      CGFloat x = 75.0 * sinf(i * 4.0 * M_PI/5.0); 
      CGFloat y = 75.0 * cosf(i * 4.0 * M_PI/5.0); 
      CGPathAddLineToPoint(starPath, NULL, center.x + x, center.y + y); 
     }; 
     CGPathCloseSubpath(starPath); 


     //Create Shape 

     shapeLayer = [CAShapeLayer layer]; 
     shapeLayer.path = pentagonPath; 
     UIColor *fillColor = [UIColor colorWithWhite:0.9 alpha:1.0]; 
     shapeLayer.fillColor = fillColor.CGColor; 
     shapeLayer.fillRule = kCAFillRuleNonZero; 
     [rootLayer addSublayer:shapeLayer]; 

    } 


    -(void) viewDidLoad { 

     NSLog(@"viewDidLoad"); 
     animationCount = 0; 

     [NSThread detachNewThreadSelector:@selector(startSequence) toTarget:self withObject:nil]; 

    } 

    -(void) startSequence { 
     NSLog(@"startSequence"); 
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

     NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 
                  target:self 
                 selector:@selector(startAnimation:) 
                 userInfo:nil 
                 repeats:YES]; 
     [[NSRunLoop currentRunLoop] run]; 
     self.repeatingTimer = timer; 
     [pool release]; 

    } 

-(void)startAnimation: (NSTimer*)theTimer { 

     NSLog(@"startAnimation"); 

     animation = [CABasicAnimation animationWithKeyPath:@"path"]; 
     animation.duration = 0.25; 
     animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]; 
     animation.repeatCount = 0; 
     animation.autoreverses = NO; 
     animation.fillMode = kCAFillModeForwards; 
     animation.removedOnCompletion = NO; 

     if((animationCount % 2) == 0) { 
      animation.fromValue = (id)pentagonPath; 
      animation.toValue = (id)starPath; 
     } else { 
      animation.fromValue = (id)starPath; 
      animation.toValue = (id)pentagonPath; 
     }; 
     [shapeLayer addAnimation:animation forKey:@"animatePath"]; 
     animationCount++; 

    } 


    - (void)didReceiveMemoryWarning 
    { 
     [super didReceiveMemoryWarning]; 
    } 


    - (void)dealloc 
    { 
     CGPathRelease(starPath); 
     CGPathRelease(pentagonPath); 
     [super dealloc]; 
    } 

    @end