0

在处理一个项目时,我创建了一个类,我在另一个类中实例化(没什么特别的),但是如果我尝试为这个实例调用一个方法,我会得到:

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

这是类 .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

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

4

1 回答 1

0

这不是编写初始化程序的正确方法:

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

您分配self为一个普通的旧 CALayer 实例,而不是您的子类的实例。

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

于 2012-11-14T04:34:55.670 回答