에서 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