2012-11-14 53 views
0

虽然工作的一个项目,我做了一个类我在另一个类实例没什么特别的,但如果我尝试调用这个例子中,我得到这个方法:广东话调用方法

终止,由于应用程序未捕获的异常 'NSInvalidArgumentException',原因是:“ - [CALayer的动画]:无法识别的选择发送到实例

这里是类.H:

#import <QuartzCore/QuartzCore.h> 

@interface SFStripe : CALayer { 

     NSTimer *animation; 

} 

- (id)initWithXPosition:(NSInteger)positionX yPosition:(NSInteger)positionY; 
- (id)initEndedWithXPosition:(NSInteger)positionX yPosition:(NSInteger)positionY; 
- (void)animate; 
- (void)reduceSize; 
- (void)logSomething; 

@property NSTimer *animation; 


@end 

这里的.M:

#import "SFStripe.h" 

@implementation SFStripe 

@synthesize animation; 

- (id)initWithXPosition:(NSInteger)positionX yPosition:(NSInteger)positionY { 

    self = [CALayer layer]; 
    self.backgroundColor = [UIColor blackColor].CGColor; 
    self.frame = CGRectMake(positionX, positionY, 5, 20); 
    self.cornerRadius = 5; 

    return self; 
} 

- (id)initEndedWithXPosition:(NSInteger)positionX yPosition:(NSInteger)positionY { 

    self = [CALayer layer]; 
    self.backgroundColor = [UIColor grayColor].CGColor; 
    self.frame = CGRectMake(positionX, (positionY + 7.5), 5, 5); 
    self.cornerRadius = 2; 
    self.delegate = self; 
    return self; 
} 

- (void) logSomething { 
    NSLog(@"It did work!"); 
} 

- (void)reduceSize { 

    if (self.frame.size.width > 0 && self.frame.size.height >= 5) { 
     self.frame = CGRectMake(self.frame.origin.x, (self.frame.origin.y - 0.5), self.frame.size.width, (self.frame.size.height - 0.5)); 
     [self setNeedsDisplay]; 
     NSLog(@"Width: %d", (int)self.frame.size.width); 
     NSLog(@"Height: %d", (int)self.frame.size.height); 
    } else { 
     self.backgroundColor = [UIColor grayColor].CGColor; 
     self.frame = CGRectMake(self.frame.origin.x, (self.frame.origin.y + 7.5), 5, 5); 
     [animation invalidate]; 
    } 

} 

- (void)animate { 

    animation = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(reduceSize) userInfo:nil repeats:YES]; 
} 


@end 

我进口该类到一个新的项目只是为了看看它是否在那里工作,但得到了同样的错误。这里是我如何调用的类的实例的方法之一(我得到了所有的方法,同样的错误)

在mainview.h(清洁工程):

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

@interface STViewController : UIViewController { 
    SFStripe *stripe; 
} 
- (IBAction)animate:(id)sender; 

@end 

我创建的实例,并在的.mi让动作调用该实例的方法:

#import <QuartzCore/QuartzCore.h> 
#import "STViewController.h" 
#import "SFStripe.h" 

@interface STViewController() 

@end 

@implementation STViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    stripe = [[SFStripe alloc] initWithXPosition:30 yPosition:30]; 
    [self.view.layer addSublayer:stripe]; 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 


} 

- (IBAction)animate:(id)sender { 

    [stripe animate]; 

} 
@end 

对不起,所有的代码,但我不能找到这个问题,帮助,希望有人能帮助我一个答案!

+0

我做了方法“logSomething”只是为了看看我的方法出了什么问题,但得到相同的错误! –

+0

尝试合成“条纹”。此外,只需在'animate'方法 – user427969

回答

0

这不是写一个初始化的正确方法:

- (id)initWithXPosition:(NSInteger)positionX yPosition:(NSInteger)positionY 
{ 
    self = [CALayer layer]; 
    ... 

你分配self是一个普通的老CALayer的实例,而不是你的子类的实例。

改为使用self = [super init],然后检查self是否为零。

+0

....中放入'NSLog'语句并检查返回值。 – bbum

+0

好吧,看到我的错误后:D非常感谢你,我正在寻找几个小时! –