如何分配initWithCoder或initWithFrame

Joh*_*ato 2 cocoa objective-c

    #import "DotAnimation.h"


@implementation DotAnimation
DoodlePad* dp;

-(void)setupView {
    Ball = CGRectMake(10, 10, 10, 10);
    [NSTimer scheduledTimerWithTimeInterval:(1.0f / 30.0f) target:self selector:@selector(traceBall) userInfo:nil repeats:YES];
}

-(id)initWithCoder:(NSCoder *)aDecoder {
    if (self == [super initWithCoder:aDecoder]){
        [self setupView];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.*/
- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [[UIColor yellowColor] CGColor]);
    CGContextFillEllipseInRect(context, Ball);

}
-(void)traceBall{
    vector<CGPoint>::iterator L = dp.doodlePoints->begin();
    while(L != dp.doodlePoints->end()){
        Ball.origin.x += (*L).x;
        Ball.origin.y += (*L).y;
        ++L;
    }
    [self setNeedsDisplay];
}

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

@end
Run Code Online (Sandbox Code Playgroud)

我有这个动画我试图在另一个文件中使用.所以我想我做的事情

trace = [[DotAnimation alloc]initWithCoder];
Run Code Online (Sandbox Code Playgroud)

要么

trace = [[DotAnimation alloc]initWithFrame];
Run Code Online (Sandbox Code Playgroud)

我不确定使用哪一个,或者我是否正确地写这个.

我希望能够使用:

    while(k != dp.doodlePoints->end()){
    Ball.origin.x += (*k).x;
    Ball.origin.y += (*k).y;
    ++k;
}
Run Code Online (Sandbox Code Playgroud)

在另一个文件中但我不知道如何从DotAnimation调用Ball.origin

如果你能把我与关于理解这一点的好信息联系起来,那也很棒.

Oli*_*ver 15

initWithCoder当您将对象嵌入到XIB中时,框架会调用它.就像ViewController一样.所以你不应该自己打电话.

initWithFrame如果你想设置框架(这似乎在这里完成),调用可能是一个更好的解决方案.但是你可以给一个框架,并添加一个你自己创建的子视图.

如果您不想自己创建子视图,那么initWithNibName:bundle:您可以调用/覆盖.创建包含视图的XIB.但是您仍然需要将该视图作为子视图添加到显示动画视图的主视图中.